Jump to content

[SOLVED] MySQL Update problem ( checkbox values )


Recommended Posts

reviewer_research_field table

user 	id
22 	       2
22 	       4
22 	       6

 

when I try to update data which is coming from form with checkbox values

 

<?php foreach ($_POST['state'] as $newstate)
{
	$write = "update reviewer_research_field as r 
	          set r.id='{$newstate}'
	          where r.user='{$_SESSION['id']}'
	          ";
	mysql_query($write) or die(mysql_error());
} ?>

 

im selecting 3 different checkbox 5-8-9

but this query is only adding one number like

 

user id

22       9

22       9

22       9

 

but it must be

 

user id

22       5

22       8

22       9

 

What is the problem? How can I solve :s

Thanks in advance

Well the problem is simple..

lets review whats happening

 

$write = "update reviewer_research_field as r set r.id='{$newstate}' where r.user='{$_SESSION['id']}' ";

the line above repeats 3 times as follows

update reviewer_research_field as r set r.id='5' where r.user='22' 

update reviewer_research_field as r set r.id='8' where r.user='22' 

update reviewer_research_field as r set r.id='9' where r.user='22' 

Each affect 3 records, thus the last one updates all 3 to the last value (9)

 

How so solve this, well the easy way is to do this

<?php
foreach($_POST['state'] as $newstate)
{
$delete = "DELETE FROM reviewer_research_field WHERE user='{$_SESSION['id']}' ";
mysql_query($delete) or die(mysql_error());
#$write = "update reviewer_research_field as r set r.id='{$newstate}' where r.user='{$_SESSION['id']}' ";
#mysql_query($write) or die(mysql_error());
$insert = "INSERT INTO reviewer_research_field (`id` ,`user`) VALUES ('{$newstate}' , '{$_SESSION['id']}');";
mysql_query($insert) or die(mysql_error());
}
?>

 

DELETE is repeating inside of the foreach :)

 

DELETE FROM reviewer_research_field WHERE user='22'

 

insert into reviewer_research_field set user='22', id='1'

 

DELETE FROM reviewer_research_field WHERE user='22'

 

insert into reviewer_research_field set user='22', id='2'

LOL Opps, But i think you got the idea.. if not try this

 

<?php
$delete = "DELETE FROM reviewer_research_field WHERE user='{$_SESSION['id']}' ";
mysql_query($delete) or die(mysql_error());
foreach($_POST['state'] as $newstate)
{
  #$write = "update reviewer_research_field as r set r.id='{$newstate}' where r.user='{$_SESSION['id']}' ";
   #mysql_query($write) or die(mysql_error());
   $insert = "INSERT INTO reviewer_research_field (`id` ,`user`) VALUES ('{$newstate}' , '{$_SESSION['id']}');";
   mysql_query($insert) or die(mysql_error());
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.