Reputation: 5132
I have a list of values I am trying to enter into a mysql database with a dynamic number of rows. The variables are stored in $box1, $box2, and so on. I am trying to use UPDATE to put in the right value ($box1 in the first row selected, $box2 in the second row selected, and so on) in each consecutive row of the table. I’m not sure what code I have to type in the “while” loop to make this happen.
<?php
// Retrieve data from Query String
$box1 = $_GET['box1'];
$box2 = $_GET['box2'];
$box3 = $_GET['box3'];
$box4 = $_GET['box4'];
$box5 = $_GET['box5'];
$session = session_id();
//build query
$query = "SELECT * FROM sessionid WHERE sessionid='$session' ";
//Execute query
$qry_result = mysql_query($query) or die(mysql_error());
// Insert the right quantity in each row for each consecutive box
$i=1;
while($row = mysql_fetch_array($qry_result)){
mysql_query("UPDATE `sessionid` SET `qt`='$box' .$i . '' WHERE `sessionid`='$session' ");
$i++;
}
?>
I am now inputting the data from the url on the previous page into an array $box[1]. I keep getting an error saying: Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/content/c/a/s/cashme/html/buylooper/change-qt.php on line 13. Line 13 refers to the line that UPDATE query. Any tips?
//Retrieve data from Query String, input into array, and update table
$count = $_SESSION['id'];
for ($i=1; $i<$count; $i++){
$box[$i] = $_GET['box'. $i .''];
mysql_query("UPDATE `sessionid` SET `qt`='$box["'. $i .'"]' WHERE `sessionid`='$session' AND `id`='$i' ");
echo $box[$i];
}
Upvotes: 0
Views: 800
Reputation: 157919
A most basic thing you have to learn by heart:
All the rows are completely random.
And you cannot rely on their natural order by any means.
To update a certain row you have to identify it with some unique field. Mysql's standard auto incremented id is the best choice.
Always store the row's id in the form, to be able to know which row current value belongs to.
Make your form like this
<input type="text" name"box[1]" value="20">
where 1
is an actual row's id
Also note that all data manipulations have to be performed using POST method, not GET.
So, in the $_POST['box']
you will have an array of pairs id => value
and can easily construct the update query out of them.
Upvotes: 2
Reputation: 16234
U need
"UPDATE `sessionid` SET `qt`="'.$box.$i . '" WHERE `sessionid`="'.$session.'"
Also You should Store the values in Array rather than separate $box
variables
u shld use
$box[]
not
$box1,$box2, etc...
Upvotes: 0
Reputation: 54248
Why don't you store your variables $box1
to $box5` in an array instead of 5 variables?
Then you can use $boxes[$i]
to call the related values.
Upvotes: 1