/    Sign up×
Community /Pin to ProfileBookmark

Updating multiple rows in MySQL table

Hi,

I currently have a small area of my site that allows a user to edit which logos are displayed on the Home Page.

It almost all works. You check the checkbox and submit and the SQL Table is updated. However, if the box is already checked and you want to uncheck (set visible = ‘N’) it isn’t possible.

Can someone help me out with this?

This is my sample code below that I’m using.

[U]Basically when a checkbox is unchecked, I want to set visible = ‘N’[/U]

[code=php]
if (isset($_POST[‘submitted’])) {

if(is_array($_POST[‘current’])) // check whether any of the checkboxes has been ticked, if not – no need to perform the update
{
$id_in = implode(‘,’, $_POST[‘current’]); // from all the boxes that have been ticked, create a string that looks like: 1,2,3,4 so it can fit into MySQL’s IN() function
mysql_query(“update footer_logos SET visible = ‘Y’ where id IN($id_in)”); // run ONE query and update all the records that are specified by the checkboxes.
}

}

$query1 = “SELECT * FROM footer_logos”;
$result1 = @mysql_query($query1); // Run the Query.

echo ‘<p><b>Edit “Footer Logos”</b></p>’;

echo $_POST[‘current’];

echo ‘<form name=”form1″ method=”POST” action=”update_logos.php” class=”book”>’;

echo ‘<table width=”550″ border=”0″ style=”padding:5px”>’;
echo ‘<tr style=”background-color:#eaeaea;”>
<td>Visible</td>
<td>Image</td>
<td>Link</td>
<td>ALT Text</td>
</tr>’;

while ($row1 = mysql_fetch_assoc($result1)) { // use assoc, not array

if ($row1[‘visible’]==’Y’)
$checked=”checked”;
else
$checked=””;

// use id to name each one differently
echo “<tr>
<td><input type=’checkbox’ name=’current[{$row1[‘id’]}]’ id=’current_{$row1[‘id’]}’ value='{$row1[‘id’]}’ class=’maillistadd’ {$checked} /></td>
<td><img src=’../images/{$row1[‘src’]}’ /></td><td>{$row1[‘link’]}</td><td>{$row1[‘alt’]}</td>
</tr>”;
}

echo ‘</table>’;

?>
<br>
<input name=”submit” type=”submit” class=”submit” value=”Update the ‘Logos’ box”>
<br>
<br>
<input type=”hidden” value=”form1″>
<input type=”hidden” name=”submitted” value=”TRUE” />
</p>
</form>

<hr size=”2″ />
[/code]

Many thanks!

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 26.2010 — Not 100% sure if this will work, but how about:
[code=php]
"update footer_logos SET visible = IF(id IN($id_n), 'Y', 'N')"
[/code]
Copy linkTweet thisAlerts:
@invisionauthorJun 26.2010 — Hi Nog,

Thanks for the reply.

Unfortunately that doesn't work, and it seems to stop the script from doing the previous function (checking and visible equals 'Y') correctly.


Any ideas?
Copy linkTweet thisAlerts:
@NogDogJun 26.2010 — How about adding some debugging (and a few other things I thought of)?
[code=php]
<?php
if (isset($_POST['submitted'])) {
$sql = "UPDATE footer SET visible = ";
if (empty($_POST['current'])) { // none checked
$sql.= "'N'";
}
elseif(is_array($_POST['current'])) {
foreach($_POST['current'] as & $id) {
$id = (int)$id; // prevent SQL injection by casting to int

}
$id_in = implode(',', $_POST['current']);
$sql.= "IF(id IN($id_n), 'Y', 'N')";
}
else {
die("How did that happen?");
}
$result = mysql_query($sql);
if (!$result) {
user_error(mysql_error() . "<br />$sql"); // debug false result

}
}
[/code]
Copy linkTweet thisAlerts:
@invisionauthorJun 26.2010 — Hi Nog,

Thanks for your patience with this.

I've tried the above, but get this error:

<i>
</i>Notice: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '), 'Y', 'N')' at line 1
UPDATE footer_logos SET visible = IF(id IN(), 'Y', 'N') in
Copy linkTweet thisAlerts:
@NogDogJun 26.2010 — Looks like a typo: I had [B]$id_in[/B] in one place and [B]$id_n[/B] in another -- they should be the same.
Copy linkTweet thisAlerts:
@invisionauthorJun 26.2010 — And just like that, we're cooking with gas!!!

Many thanks NogDog. I really should have spotted that myself. Whoops.


Thanks again
×

Success!

Help @invision spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...