/    Sign up×
Community /Pin to ProfileBookmark

help with php mysql update form please

Hi,

i made a form so i can update database values. So far it works fine if i want to update 2 field values from one single row, but the problem is that i have over 90 rows! so its out of the question to define all the $_POST[”] for every one of them, thats why im trying to define a loop in order to have all those variables generated automatically but i cant get it to work:

[code=php]
$str = “”;
$totalrows=”SELECT * FROM rooms”;
$result=mysql_query($totalrows);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$row_room=mysql_result($result,$i,”room”);
$row_price=mysql_result($result,$i,”price”);
$row_status=mysql_result($result,$i,”status”);
$number = preg_replace(“/[^0-9]/”, ”, $row_room);

$ud_price[$row_room] = $_POST[‘ud_price_’$row_room]; <— this line
$action .= “UPDATE rooms SET price = ‘$ud_price[$row_room]’, status = ‘$ud_status[$row_room]’ WHERE room = ‘$row_room'”; <– and this line

$str .= <<<EOF
<TR>
<TD>$number</TD>
<TD><INPUT type=”text” name=”ud_price_$row_room” size=”3″ value=”$row_price”></TD>
<TD>
<SELECT name=”ud_state_$row_room”>
$state[$row_room]
</SELECT>
<input type=”hidden” name=”ud_room_$row_room” value=”$row_room”>
</TD>
</TR>

[/code]

the code is actually longer but the part that needs to be fixed is marked with “<–“. The form would work (without the marked part) if i do this:

[code=php]
$ud_room_201=$_POST[‘ud_room_201’];
$ud_price_201=$_POST[‘ud_price_201’];
$ud_status_201=$_POST[‘ud_status_201’];

$ud_room_202=$_POST[‘ud_room_202’];
$ud_price_202=$_POST[‘ud_price_202’];
$ud_status_202=$_POST[‘ud_status_202’];

and so on…

$action1 = “UPDATE rooms SET price = ‘$ud_price_201’, status = ‘$ud_status_201’ WHERE room = ‘$ud_room_201′”;

$action2 = “UPDATE rooms SET price = ‘$ud_price_202’, status = ‘$ud_status_202’ WHERE room = ‘$ud_room_202′”;

and so on…

mysql_query($action);
mysql_query($action2);

and so on…[/code]

but i guess there is a much more simple way to do it, isnt it? just the same way i did to generate the <td></td> for each row in the table. Can you please give me so hints?

thank you!

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@gvreJun 07.2011 — [code=php]$ud_rooms = $_POST['ud_room'];
$ud_prices = $_POST['ud_price'];
$ud_status = $_POST['ud_status'];

foreach($ud_rooms as $key => $value)
{
$sql = "UPDATE rooms SET price='" . $ud_prices[$key] . "', status='" . $ud_status[$key] . "' WHERE room='" . $value . "'";
mysql_query($sql);
}[/code]


For security and performance reasons it's better to do it with prepared statements.
Copy linkTweet thisAlerts:
@supercainauthorJun 07.2011 — Hi,

thank you, but seems like i didnt put it in the right place because im getting an error that says: Invalid argument supplied for foreach(). Is that code supposed to be inside the while or outside? if i use that then i wouldnt need the mysql_query($action); at the end of the file?
Copy linkTweet thisAlerts:
@gvreJun 07.2011 — Change this
[CODE]<TR>
<TD>$number</TD>
<TD><INPUT type="text" name="ud_price_$row_room" size="3" value="$row_price"></TD>
<TD>
<SELECT name="ud_state_$row_room">
$state[$row_room]
</SELECT>
<input type="hidden" name="ud_room_$row_room" value="$row_room">
</TD>
</TR>
[/CODE]

to this
[CODE]<TR>
<TD>$number</TD>
<TD><INPUT type="text" name="ud_price[$row_room]" size="3" value="$row_price"></TD>
<TD>
<SELECT name="ud_state[$row_room]">
$state[$row_room]
</SELECT>
<input type="hidden" name="ud_room[$row_room]" value="$row_room">
</TD>
</TR>[/CODE]

and

[code=php]if (is_array($ud_rooms))
{
foreach($ud_rooms as $key => $value)
{
$sql = "UPDATE rooms SET price='" . $ud_prices[$key] . "', status='" . $ud_status[$key] . "' WHERE room='" . $ud_room[$key] . "'";
mysql_query($sql);
}
}[/code]
Copy linkTweet thisAlerts:
@supercainauthorJun 07.2011 — Sorry, where is that if statement supposed to come in? rigth after the html or outside the while?

By the way, i posted the wrong code on my first message, the marked part was supposed to be like this

[code=php]$ud_room[$row_room] = $_POST['ud_room_'.$row_room];
$ud_price[$row_room] = $_POST['ud_price_'.$row_room];
$ud_status[$row_room] = $_POST['ud_state_'.$row_room];[/code]


which i expected to generate this:

[code=php]$ud_room_201=$_POST['ud_room_201'];
$ud_price_201=$_POST['ud_price_201'];
$ud_status_201=$_POST['ud_status_201'];

$ud_room_202=$_POST['ud_room_202'];
$ud_price_202=$_POST['ud_price_202'];
$ud_status_202=$_POST['ud_status_202'];

and so on...[/code]


since i dont get any errors i dont know if its right or not, but either way i cant figure out what to do with this:


[code=php]$action = "UPDATE rooms SET price = '$ud_price', status = '$ud_status' WHERE room = '$ud_room'";
mysql_query($action);[/code]


this comes at the very end of the file.

Thank you.
Copy linkTweet thisAlerts:
@gvreJun 07.2011 — Could you send the php file?
Copy linkTweet thisAlerts:
@supercainauthorJun 07.2011 — I've sent it to you by pm, although i pretty much posted it here already.

Thanks.
Copy linkTweet thisAlerts:
@supercainauthorJun 12.2011 — Sorry to reopen this topic but im having problems again. gvre gave me this code which resolved the issue about having the database values updated

[code=php]if (!empty($_POST['ud_room']) && is_array($_POST['ud_room']))
{
$ud_room = $_POST['ud_room'];
$ud_price = $_POST['ud_price'];
$ud_status = $_POST['ud_state'];

foreach($ud_room as $key => $value)
{
$sql = "UPDATE rooms SET price='" . $ud_price[$key] . "', status='" . $ud_status[$key] . "' WHERE room='" . $ud_room[$key] . "'";
mysql_query($sql);
}
}[/code]


after you press submit, the values are updated and you return to the same form, however, now the problem is that the page wont show the fields with the new values, but will show the ones before you submitted the form unless you reload the page. Why does that happen? i already moved that part of the code to the very beginning of the page and that solved the problem at the beginning, but all of the sudden i got the same problem back. How can i have the page show the values as they really are now that have been updated?

Thank you.
Copy linkTweet thisAlerts:
@NogDogJun 13.2011 — I think I would format the form as :
[code=php]
$str .= <<<EOF
<TR>
<TD>$number</TD>
<TD><INPUT type="text" name="ud[$row_room][price]" size="3" value="$row_price"></TD>
<TD>
<SELECT name="ud[$row_room][state]">
$state[$row_room]
</SELECT>
<input type="hidden" name="ud[$row_room][[room]" value="$row_room">
</TD>
</TR>
[/code]

Your $_POST array would then have $_POST['ud'] as a mutli-dimension array, something like:
[code=php]
$_POST['ud'] = array(
1 => array(
'price' => 'a price value here',
'state' => 'a state value here',
'room' => 'a room value here',
2 => array(
'price' => 'a price value here',
'state' => 'a state value here',
'room' => 'a room value here'
)
[/code]

...where the first level keys (1 and 2 in this example) would be the $row_room values used in the form code. Now in the form-handler code, you can simply foreach() through each row to process it:
[code=php]
foreach($_POST['ud'] as $id => $data {
// just a sample query, not claiming this is anything close to what you want!
$sql = sprintf(
"UPDATE your_table SET price=%02f, state='%s', room='%s' WHERE id=%d",
(float) $data['price'],
mysql_real_escape_string($data['state'],
mysql_real_escape_string($data['room'],
(int) $id
);
mysql_query($sql);
}
[/code]
Copy linkTweet thisAlerts:
@supercainauthorJun 14.2011 — Hi,

does it mean i have to do this for each row i have (rooms)? something like

[code=php]$_POST['ud'] = array(
1 => array(
'price' => 'a price value here',
'state' => 'a state value here',
'room' => 'a room value here',
2 => array(
'price' => 'a price value here',
'state' => 'a state value here',
'room' => 'a room value here'
3 => array(
'price' => 'a price value here',
'state' => 'a state value here',
'room' => 'a room value here'

and so on...
) [/code]


because i have way too many so its out of the question to do so for all of them. Actually my problem is not that values in the database are not being posted and updated, they do, the problem comes right after the form is submitted, the page reloads and shows me the values i have before changing them and submitting them. You need to reload the page in order to see the new values. My objective is that after you submit the form you return to the same page but now showing the new values you just submitted. It was already working but i think it stopped doing it after i inserted a login, now it looks like this:

[code=php]
<?php
session_start();
if(!session_is_registered(username)){
header("location:login.php");
}

include 'db.php';
if (!empty($_POST['ud_room']) && is_array($_POST['ud_room']))
{
$ud_room = $_POST['ud_room'];
$ud_price = $_POST['ud_price'];
$ud_status = $_POST['ud_state'];

foreach($ud_room as $key => $value)
{
$sql = "UPDATE rooms SET price='" . $ud_price[$key] . "', status='" . $ud_status[$key] . "' WHERE room='" . $ud_room[$key] . "'";
mysql_query($sql);
}
}

$time = time();
$str = "";
$totalrows="SELECT * FROM rooms";
$result=mysql_query($totalrows);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$row_room=mysql_result($result,$i,"room");
$row_price=mysql_result($result,$i,"price");
$row_status=mysql_result($result,$i,"status");
$number = preg_replace("/[^0-9]/", '', $row_room);
$str .= <<<EOF
<TR>
<TD class="roomtable">$number</TD>
<TD class="roomtable"><INPUT type="text" name="ud_price[$row_room]" size="3" value="$row_price">000 dls</TD>
<TD class="roomtable">
<SELECT name="ud_state[$row_room]">
$state[$row_room]
</SELECT>
<input type="hidden" name="ud_room[$row_room]" value="$row_room">
</TD>
</TR>
EOF;
switch ($i) {
case 24:
$str .= <<<EOF
<TR>
<TD colspan="3" class="menu2">location 1</TD>
</TR>
EOF;
break;
case 32:
$str .= <<<EOF
<TR>
<TD colspan="3" class="menu2">location 2</TD>
</TR>
EOF;
break;
case 45:
$str .= <<<EOF
<TR>
<TD colspan="3" class="menu2">location 3</TD>
</TR>
EOF;
break;
case 74:
$str .= <<<EOF
<TR>
<TD colspan="3" class="menu2">location 4</TD>
</TR>
EOF;
break;

case 81:
$str .= <<<EOF
<TR>
<TD colspan="3" class="menu2">location 5</TD>
</TR>
EOF;
break;
case 91:
$str .= <<<EOF
<TR>
<TD colspan="3" class="menu2">location 6</TD>
</TR>
EOF;
break;
}
++$i;

}

$content=<<<EOF
<TABLE class="txtcell" cellpadding="0" cellspacing="0" width="100&#37;">

<TR>
<TD align="left" valign="top">
<H1>Control Panel</H1>
</TD>
</TR>
<TR>
<TD align="left" valign="top">
<FORM method="POST" action="$PHP_SELF">
<TABLE class="bordertable" cellpadding="0" cellspacing="1" width="100%">
<TBODY>
<TR>
<TD colspan="3">locations</TD>
</TR>
<TR>
<TD><B>Room</B></TD>
<TD><B>Rent</B></TD>
<TD><B>Status</B></TD>
</TR>
$str
</TBODY>
</TABLE>
</TD>
</TR>
<TR>
<TD align="center"><BR><input type="hidden" name="ud_time" value="$time"><input type="submit" value="UPDATE"></TD>
</FORM>
</TR>
<TR>
<TD align="right"><BR><a href="login.php?logout">Logout<a/></TD>
</TR>

</TBODY>
</TABLE>
EOF;
include('template.htm');
$ud_time = $_POST['ud_time'];
$updatetime = "UPDATE lastdate SET date='$ud_time' WHERE date!= '1'";
mysql_query($updatetime);

?>
[/code]


this is how the full code looks, it works great except for the reload thing. As i said, it was working perfect before i added the login code, so i tried by removing it once but got actually worse because then you cant see the new values even by reloading the page, you have to revisit it in your browser in order for the updated page to show. weird, huh?

thanks.
×

Success!

Help @supercain spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.30,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...