funbinod Posted June 3, 2014 Share Posted June 3, 2014 i learnt how to update single mysql row using jquery and ajax. now i want to learn how can i update multiple rows the same way. this was done to update one row -- var where_val = form.find('#where').val(); var where_col = form.find('#where').attr('name'); input.bind('blur', function(event) { var value = input.val(); $.ajax({ url: action, type: method, data: { val: value, col: column, w_col: where_col, w_val: where_val, }, to try if it works or not for updating multiple rows, i changed this to -- var where_val = form.find('#where').val(); var where_col = form.find('#where').attr('name'); var where1_val = form.find('#where1').val(); var where1_col = form.find('#where1').attr('name'); input.bind('blur', function(event) { var value = input.val(); $.ajax({ url: action, type: method, data: { val: value, col: column, w_col: where_col, w_val: where_val, w1_col: where1_col, w1_val: where1_val }, but somehow its not working. it is updating all the rows with the values of the last row. please guide me... Link to comment Share on other sites More sharing options...
justsomeguy Posted June 3, 2014 Share Posted June 3, 2014 That code isn't updating anything, that is Javascript code to send an ajax request to PHP. PHP is sending the SQL code to do the actual changes. Link to comment Share on other sites More sharing options...
funbinod Posted June 3, 2014 Author Share Posted June 3, 2014 i cant understand what u meant. but the previous one updates the value to the mysql table for single row. and the second one also does but it updates the value of all the rows as the value of the last row (while trying to update multiple rows)..... Link to comment Share on other sites More sharing options...
justsomeguy Posted June 3, 2014 Share Posted June 3, 2014 PHP updates, not Javascript. You're showing Javascript code, not PHP. I can't use that to tell you why the PHP code isn't doing what you expect. Link to comment Share on other sites More sharing options...
funbinod Posted June 3, 2014 Author Share Posted June 3, 2014 oh sorry! here is the phps <?phprequire_once('mysql.php');$result = mysqli_query($connect, "SELECT * FROM tran WHERE svn=1");?><form id="ajax-form" class="autosubmit" method="POST" action="./ajax-update.php"><fieldset><legend>Update user information</legend><?while ($row = mysqli_fetch_assoc($result)) { ?><label>SID:</label><input name="sid" value="<?php echo $row['sid'] ?>" /><label>srate:</label><input name="srate" value="<?php echo $row['srate'] ?>" /><label>sqty:</label><input name="sqty" value="<?php echo $row['sqty'] ?>" /><br /><input id="where" type="hidden" name="svn" value="<?php echo $row['svn']; ?>" /><input id="where1" type="hidden" name="ssn" value="<?php echo $row['ssn']; ?>" /><?php } ?></fieldset></form> and here is ajax-update.php <?phprequire_once('mysql.php');function clean($value) { return $value;}if (count($_POST)) { foreach($_POST as $column => $value) ${$column} = clean($value); $result = mysqli_query($connect, "UPDATE tran SET ".$col."='".$val."' WHERE ".$w_col."='".$w_val."' AND ".$w1_col."='".$w1_val."'") or die('Unable to update row.'.mysqli_error($connect));}?> Link to comment Share on other sites More sharing options...
justsomeguy Posted June 3, 2014 Share Posted June 3, 2014 Your clean function doesn't do anything, but you can print out that query if you want to see what it's doing. It looks like the change you made is to add a second condition to the WHERE clause, that will update any row that matches those conditions. Link to comment Share on other sites More sharing options...
funbinod Posted June 3, 2014 Author Share Posted June 3, 2014 (edited) it gives alert screen with just '1' in it when i print($result). and i cant understand what is it! Â and u r right i wish to add second condition to update each row. but result is not the same as i wish.. Edited June 3, 2014 by funbinod Link to comment Share on other sites More sharing options...
justsomeguy Posted June 3, 2014 Share Posted June 3, 2014 Printing the result will just be true or false if the query succeeded or not. You would need to print the actual SQL string you're building. Link to comment Share on other sites More sharing options...
funbinod Posted June 3, 2014 Author Share Posted June 3, 2014 what could be the other actual SQL string other than $result = mysqli_query($connect, "UPDATE tran SET ".$col."='".$val."' WHERE ".$w_col."='".$w_val."' AND ".$w1_col."='".$w1_val."'") or die('Unable to update row.'.mysqli_error($connect)); ?? Â when i print($result) -- it alerts just '1' when i var_dump($result) -- it alerts 'string(1) "1" ' Link to comment Share on other sites More sharing options...
justsomeguy Posted June 3, 2014 Share Posted June 3, 2014 echo "UPDATE tran SET ".$col."='".$val."' WHERE ".$w_col."='".$w_val."' AND ".$w1_col."='".$w1_val."'"; Link to comment Share on other sites More sharing options...
funbinod Posted June 3, 2014 Author Share Posted June 3, 2014 uhh huh! Â it gives only the value from first row on both the conditions for all rows. i expected the second condition to be different as the different values from different rows. but it is selecting the same value for the second condition also... Link to comment Share on other sites More sharing options...
justsomeguy Posted June 3, 2014 Share Posted June 3, 2014 That entire query is for one group of rows. You are telling it to set the group of rows that match the WHERE conditions to all have the same values. If you want to set different values in different rows then you need multiple update queries. Link to comment Share on other sites More sharing options...
funbinod Posted June 4, 2014 Author Share Posted June 4, 2014 thanks to u and congratulation to me.... i made it with simple change.. i was making while loop for just form elements. but i did it to a entire form and it worked... <?phprequire_once('mysql.php');$result = mysqli_query($connect, "SELECT * FROM tran WHERE svn=1");while ($row = mysqli_fetch_assoc($result)) { ?><form id="ajax-form" class="autosubmit" method="POST" action="./ajax-update.php"><label>SID:</label><input name="sid" value="<?php echo $row['sid'] ?>" /><label>srate:</label><input name="srate" value="<?php echo $row['srate'] ?>" /><label>sqty:</label><input name="sqty" value="<?php echo $row['sqty'] ?>" /><br /><br /><input id="where" type="hidden" name="svn" value="<?php echo $row['svn']; ?>" /><input id="where1" type="hidden" name="ssn" value="<?php echo $row['ssn']; ?>" /></form><?php } ?> Link to comment Share on other sites More sharing options...
funbinod Posted June 4, 2014 Author Share Posted June 4, 2014 but is it good idea? is there any other better idea? can u suggest me? Link to comment Share on other sites More sharing options...
justsomeguy Posted June 4, 2014 Share Posted June 4, 2014 If you're printing multiple forms like that then you should remove the IDs or change them to be unique, you can't have multiple elements on the same page with the same ID. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now