Jump to content
funbinod

update multiple rows using ajax

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...

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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)..... :(

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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));}?>

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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" '

Share this post


Link to post
Share on other sites
echo "UPDATE tran SET ".$col."='".$val."' WHERE ".$w_col."='".$w_val."' AND ".$w1_col."='".$w1_val."'";

Share this post


Link to post
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...

Share this post


Link to post
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.

Share this post


Link to post
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 } ?>

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...