...

View Full Version : Multiple updates



robojob
12-20-2006, 10:53 PM
i have this check box on my page...

<input type="checkbox" name="id[]" value="' . $row['id'] . '">

and when submitted i am running this code...




if (isset($_POST['submit'])) {

$id = $_POST["id"];

$query = "UPDATE updates set status = 'test' where id in ($id)";
$result = @mysql_query ($query); //run the query

echo 'complete';
echo $id;
}

i have echoed out $id to see what it shows and it just outputs the word array and the table is not updating... where am i going wrong?

Tyree
12-20-2006, 11:20 PM
By setting the checkbox name as id[], you are saying you want it to be an array. To echo it back out, you need to tell php which key from the array you want to display. $id[0], for example.

If you DON'T need it to be an array, then drop the brackets...name the checkbox 'id'

Brandoe85
12-20-2006, 11:28 PM
Array is what you want, you just have to loop through the array and build up your id's string separated by commas:


$in = '';
foreach($id as $ids)
{
$in .= $ids . ',';
}
$in = substr($in, 0, strlen($in)-1);

// now use $in...where id in($in)
Good luck;

mlseim
12-20-2006, 11:33 PM
This might not really answer your question, but
$id will be null (non-existent) if the checkbox
is NOT checked. PHP only assigns a value if
the box is checked.

So, $id[0] will be the value of $row[id], only if it's checked,
otherwise, $id is null.

... and because you specified "id[]", it's expecting an array,
so try echoing $id[0] and see if it shows up.

Maybe the array thing is where your problem is?

Tyree
12-20-2006, 11:51 PM
Yes, it really all depends on what you're trying to accomplish. We don't know if you're outputting several rows of data, each having a checkbox...in that case you WOULD want to set them to an array.

As for Brandoe's suggestion on what to do with the $id array once you have it. Brandoe probably knows better. But, I prefer to use the join() function rather than what he did.
Once you have populated the $id array:


if (is_array($id)){
$in = join(',', $id);
}
// now use $in...where id in($in)

robojob
12-21-2006, 11:32 AM
Array is what you want, you just have to loop through the array and build up your id's string separated by commas:


$in = '';
foreach($id as $ids)
{
$in .= $ids . ',';
}
$in = substr($in, 0, strlen($in)-1);

// now use $in...where id in($in)
Good luck;


Thanks, this did it perfectly!

Brandoe85
12-21-2006, 03:08 PM
Yes, it really all depends on what you're trying to accomplish. We don't know if you're outputting several rows of data, each having a checkbox...in that case you WOULD want to set them to an array.

As for Brandoe's suggestion on what to do with the $id array once you have it. Brandoe probably knows better. But, I prefer to use the join() function rather than what he did.
Once you have populated the $id array:


if (is_array($id)){
$in = join(',', $id);
}
// now use $in...where id in($in)


Yeah, I wanted to use implode() but couldn't remember the name while I was typing the post, heh :)

http://us3.php.net/implode

Tyree
12-21-2006, 03:27 PM
Hmmm, I don't know why I never realized join() and implode() were interchangable! Never had to look them up on php.net, I guess! :D

robojob
12-21-2006, 07:20 PM
i have this code now...


if (isset($_POST['submit'])) {

$id = $_POST["id"];
$status = $_POST['status'];

$in = '';
foreach($id as $ids)
{
$in .= $ids . '<br/>';
}
$in = substr($in, 0, strlen($in)-1);

echo $in;
}

and i have a form that has several rows with a checkbox for each row with its id number.

for updating multiple records this now work, what i want to do now is add a new feature that....

when the check boxes are checked it submits the page and inputs each id from the array in to a text field... i have tried echo $in[0]; etc and it only echos a single number not the full id, for example if the first id in the array is 44, and i echo $in[0] all i get is a 4 not 44, any ideas?

Tyree
12-21-2006, 07:35 PM
You'll need to do a for statement to run through the values of $id. Just printing $id[0] will print the value of the first 'member' of the array.



for ($i=0; $i <= count($id); $i++){
print $id[$i];
}


As Brando and I talked about above...use this:


if (is_array($id)){
$in = implode('<br/>', $id);
}
echo $in;


In place of:


$in = '';
foreach($id as $ids)
{
$in .= $ids . '<br/>';
}
$in = substr($in, 0, strlen($in)-1);

echo $in;
}


Does the EXACT same thing, but it's much cleaner.

robojob
12-21-2006, 07:51 PM
if (is_array($id)){
$in = implode('<br/>', $id);
}
echo $in[1];
}

how can i get it to print a single check box. eg, check box number 3... $in[4] prints value 4 in the array not the 4th checkbox id...

robojob
12-21-2006, 07:59 PM
ignore that last post.... had a blond moment!!!

Tyree
12-21-2006, 08:03 PM
Okay, so you want a checkbox beside each entity so you can select which ones to update?

Okay, first, I'm going to assume you know how to get a entities out of a database and put them into an array. We'll call that array $entities.

So, you'd run a foreach on the $entitis array to print out a checkbox for each one.


foreach($entities AS $entity){
print '<input type="checkbox" name="'.id[].'" value="1">'.$entity['name'];
}


So, that prints a checkbox for each entity found in your main db table. I guessed that 'name' may be one of your field names. You can alter that as you see fit.

So, as you check each checkbox and then submit the form the checkboxes are housed in...you'd then use the code we've given you to process the $id array.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum