Page 1 of 2

update multirows

Posted: Thu Jan 04, 2007 2:50 pm
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());

Posted: Thu Jan 04, 2007 3:00 pm
by feyd
new_number_room and new_state_room are unique in the form?

Posted: Thu Jan 04, 2007 3:04 pm
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

Posted: Thu Jan 04, 2007 3:14 pm
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?

Posted: Thu Jan 04, 2007 3:25 pm
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

Posted: Thu Jan 04, 2007 3:34 pm
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.

Posted: Thu Jan 04, 2007 3:51 pm
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());

Posted: Fri Jan 05, 2007 5:51 am
by hrubos
Who would mind showing me how to do for updating row of result,which are more 2 rows???

Posted: Fri Jan 05, 2007 9:35 am
by feyd
My post specifically stated to remove the hidden field, and name both input fields as "foo"

There's a reason for it.

Posted: Fri Jan 05, 2007 9:39 am
by Mordred
Also $_POST['new_id_numberRoom'] is vulnerable to a SQL injection.
Also don't use addslashes but mysql_real_escape_string

Posted: Fri Jan 05, 2007 4:48 pm
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 !!!

Posted: Fri Jan 05, 2007 5:02 pm
by feyd
You don't need the first tag. I've said to remove it now three times.

Posted: Fri Jan 05, 2007 5:37 pm
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

Posted: Fri Jan 05, 2007 6:03 pm
by feyd
Go over all the posts in this thread again, carefully and slowly.

Posted: Sun Jan 07, 2007 1:29 am
by hrubos
So I have tried but actually, I can't make it run!!!