update multirows

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

hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

update multirows

Post by hrubos »

Hi all, would you mind showing me how do I wrong because I only can update one row, more I couldn't .
page1.php

Code: Select all

...........
echo "<td width = '30'><input type='hidden' name='new_id_numberRoom[]' value='$id_numberRoom'></td>" ;
echo "<td width = '90'><input type='text' name='new_number_room' size='10' align='right' value= '$number_room' size='8' style='font-family:Arial'></td>" ;
echo "<td width = '90'><input type='text' name='new_state_room' size='10' align='right' value= '$state_room' size='8' style='font-family:Arial'></div></td>";
page2.php

Code: Select all

$valId = implode(",",$_POST['new_id_numberRoom']);
$new_number_room = $_POST['new_number_room'];
$new_state_room=$_POST['new_state_room'];

if(!get_magic_quotes_gpc())
{
	$new_number_room  = addslashes($new_number_room);

}
$query = "UPDATE room p,type_room t SET p.number_room = '$new_number_room',
               p.state_room = '$new_state_room'
                     WHERE p.id_numberRoom IN ($valId)";
mysql_query($query) or die(mysql_error());
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

new_number_room and new_state_room are unique in the form?
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

feyd wrote:new_number_room and new_state_room are unique in the form?
Really I don't understand your question, so plz, would you mind explaining me again???

Thank you
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Are the echoes posted above in a loop?
In other words: in the resulting HTML, do the fields corresponding to new_number_room and new_state_room appear more than once?
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

yeah, I use checkbox to chose and so reult can be return more 2 rows.

If query only 1 result, so Ok

But more than 1, so it doesn't run exactly
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Because you have multiples of each, the naming needs to be adjusted.

For example, new_id_numberRoom[] could be removed; new_number_room could become foo[$id_numberRoom][numberRoom]; and new_state_room could become foo[$id_numberRoom][stateRoom].

You would then loop over $_POST['foo'] where the keys will be the original room number and the value will be an array of the numberRoom and stateRoom values. Each iteration over foo will require a new UPDATE query.
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

So i'm sorry if I don't understand you suggest exatly. I have tried follow your advice but I have received that after updating, it would be edited , is described here

(I chose 2 checkbox)-->2/2b| free
2/3b|free
---> 2/3|free
2/4|free ---> submit -->(last step) 2/3 |free
Array|Array

I did that :

Code: Select all

echo "<td width = '30'><input type='hidden' name='new_id_numberRoom' value='$id_numberRoom'></td>" ;
echo "<td width = '90'><input type='text' name='foo[$id_numberRoom][numberRoom]' size='10' align='right' value= '$number_room' size='8' style='font-family:Arial'></td>" ;
echo "<td width = '90'><input type='text' name='foo1[$id_numberRoom][stateRoom]' size='10' align='right' value= '$state_room' size='8' style='font-family:Arial'></div></td>";
page2.php

Code: Select all

$new_id_numberRoom = $_POST['new_id_numberRoom'];
$new_number_room = $_POST['foo'];
$new_state_room=$_POST['foo1'];

if(!get_magic_quotes_gpc())
{
	$new_number_room  = addslashes($new_number_room);

}
$query = "UPDATE room p SET p.number_room = '$new_number_room',
               p.state_room = '$new_state_room'
                  WHERE p.id_numberRoom = '$new_id_numberRoom'";   
mysql_query($query) or die(mysql_error());
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

Who would mind showing me how to do for updating row of result,which are more 2 rows???
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

My post specifically stated to remove the hidden field, and name both input fields as "foo"

There's a reason for it.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

Also $_POST['new_id_numberRoom'] is vulnerable to a SQL injection.
Also don't use addslashes but mysql_real_escape_string
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

feyd wrote:My post specifically stated to remove the hidden field, and name both input fields as "foo"

There's a reason for it.
hej, your advance looks right and I think it is right but when I tried agian, it doesn't run any thing

I did :

Code: Select all

echo "<td width = '30'><input type='text' name='foo' value='$id_numberRoom'></td>" ;
echo "<td width = '90'><input type='text' name='foo[$id_numberRoom][numberRoom]' size='10' align='right' value= '$number_room' size='8' style='font-family:Arial'></td>" ;
echo "<td width = '90'><input type='text' name='foo[$id_numberRoom][stateRoom]' size='10' align='right' value= '$state_room' size='8' style='font-family:Arial'></div></td>";

Code: Select all

$new_id_numberRoom = $_POST['foo'];
$new_number_room = $_POST['foo[$id_numberRoom][numberRoom]'];
$new_state_room=$_POST['foo[$id_numberRoom][stateRoom]'];
$query = "UPDATE room p SET p.number_room = '$new_number_room',
               p.state_room = '$new_state_room'
                  WHERE p.id_numberRoom = '$new_id_numberRoom'";
mysql_query($query) or die(mysql_error());
Thank your advance !!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You don't need the first tag. I've said to remove it now three times.
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

feyd wrote:You don't need the first tag. I've said to remove it now three times.
hic, :cry: what a pity that I tried once more and It doesn't run

Code: Select all

echo "<td width = '90'><input type='text' name= 'foo[$id_numberRoom][number_room]' size='10' align='right' value= '$number_room' size='8' style='font-family:Arial'></td>" ;
echo "<td width = '90'><input type='text' name= 'foo[$id_numberRoom][state_room]' size='10' align='right' value= '$state_room' size='8' style='font-family:Arial'></div></td>";

Code: Select all

$new_number_room = $_POST['foo'];
$new_state_room=$_POST['foo'];
$query = "UPDATE room p SET p.number_room = '$new_number_room',
               p.state_room = '$new_state_room'";
mysql_query($query) or die(mysql_error());
I'm sorry if I have done wrong
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Go over all the posts in this thread again, carefully and slowly.
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

So I have tried but actually, I can't make it run!!!
Post Reply