Jump to content

update multiple rows using ajax


funbinod

Recommended Posts

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

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

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

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

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 by funbinod
Link to comment
Share on other sites

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...