Multiple Update

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Erick020
Forum Newbie
Posts: 22
Joined: Sat Apr 12, 2003 7:11 am

Post by Erick020 »

d11wtq wrote:Ok, take this line of code:

Code: Select all

$q = "UPDATE users SET last_login = NOW() WHERE id = '" . mysql_real_escape_string($userId) . "' LIMIT 1";
It does absolutely nothing other than create a string which we do nothing with.

Now take this:

Code: Select all

$q = "UPDATE users SET last_login = NOW() WHERE id = '" . mysql_real_escape_string($userId) . "' LIMIT 1";
mysql_query($q);
It runs that string as a MySQL query. You're missing the call to mysql_query() in your code.

I'll also take a wild stab in the dark that this bit of code:

Code: Select all

WHERE 

'Aanvraag_ID[$i]' = 'Aanvraag_ID[$i]
Should be:

Code: Select all

WHERE 

Aanvraag_ID = 'Aanvraag_ID[$i]


I think I mis a part of a code in the script where I call and echo the values of Text_ID[0] value=4, Text_ID[1] value=8, Text_ID[3] value=15, etc... I think I need somehow to send these values to the next script, so I can then use these values in the next script where I run the update querry... SET... A=$A, B=$B where Text_ID= Text_ID[$i]
I think I may need a kind of loop but I don't how to do it.

Any idea?

In advance thank you for your help.
Please give a clear exemple so I can better understand the process and reproduce it.

What I try to do is:
1- I call some data from Table_1 and print it on a page (first script, nothing special, SELECT A,B FROM Table_1...)
2- I need to update Table_2 with these data where Text_ID has been selected (second script, update Table_2 SET A=$A, B=$B where Text_ID= Text_ID[$i]).

I hope this is clear.

Thanks a lot.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I've taken the liberty to edit your title for you, per our warnings.
Erick020
Forum Newbie
Posts: 22
Joined: Sat Apr 12, 2003 7:11 am

Post by Erick020 »

Jcart wrote:I've taken the liberty to edit your title for you, per our warnings.
Sure... no problem... thanks... but I would really like to get some ideas/answers about how I coud/should do to solve my problem....
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You have a html form like

Code: Select all

<html>
	<head><title>...<title></head>
	<body>
		<form method="post" action="test2.php">
			<div>
				<input type="hidden" name="Q[0]" value="3" />
				<input type="hidden" name="Q[1]" value="6" />
				<input type="hidden" name="Q[2]" value="9" /> 
				<input type="submit" />
			</div>
		</form>
	</body>
</html>
no take look at what is posted

Code: Select all

<html>
	<head><title>...<title></head>
	<body>
		<pre>POST: <?php print_r($_POST); ?></pre>
	</body>
</html>

Code: Select all

POST: Array
(
    [Q] => Array
        (
            [0] => 3
            [1] => 6
            [2] => 9
        )

)
You see there is an element $_POST['Q'] and it's an array containing the values of all the name="Q[n] elements of the form.
You want to update all records that have one of these values in the field Aanvraag_ID. The IN operator is best suited for that, http://dev.mysql.com/doc/refman/5.1/en/ ... unction_in
... WHERE Aanvraag_ID IN (3,6,9)
First you have to make sure all parameters are numbers to avoid sql injections. I suggest you use array_map and intval for that matter.
Then you have to combine the values of the array to a single string, join should be most helpful.
Erick020
Forum Newbie
Posts: 22
Joined: Sat Apr 12, 2003 7:11 am

Post by Erick020 »

volka wrote:You have a html form like

Code: Select all

<html>
	<head><title>...<title></head>
	<body>
		<form method="post" action="test2.php">
			<div>
				<input type="hidden" name="Q[0]" value="3" />
				<input type="hidden" name="Q[1]" value="6" />
				<input type="hidden" name="Q[2]" value="9" /> 
				<input type="submit" />
			</div>
		</form>
	</body>
</html>
no take look at what is posted

Code: Select all

<html>
	<head><title>...<title></head>
	<body>
		<pre>POST: <?php print_r($_POST); ?></pre>
	</body>
</html>

Code: Select all

POST: Array
(
    [Q] => Array
        (
            [0] => 3
            [1] => 6
            [2] => 9
        )

)
You see there is an element $_POST['Q'] and it's an array containing the values of all the name="Q[n] elements of the form.
You want to update all records that have one of these values in the field Aanvraag_ID. The IN operator is best suited for that, http://dev.mysql.com/doc/refman/5.1/en/ ... unction_in
... WHERE Aanvraag_ID IN (3,6,9)
First you have to make sure all parameters are numbers to avoid sql injections. I suggest you use array_map and intval for that matter.
Then you have to combine the values of the array to a single string, join should be most helpful.
This is indeed what i have... so thank you very much for the help... I gonna take a look later and I will let you know if I could solve the problem.

Again thank you very much.
Post Reply