zed420 Posted January 3, 2009 Share Posted January 3, 2009 Hi All Can someone please put me on right direction, I'm trying to Edit a field called $fare from the database, this code below works fine as long as there is only one field called $fare1. My problem is there are 21 fields like this one for each customer e.g $cust1, $fare1, $cust2, $fare2 ... How the devil do I Update them all. If I write multipul rows in the form then they all show. I hope I'm making some sense. Some help will be really appreciated if($_POST['amend']){ $id = $_POST["id"]; $fare1=$_POST["fare1"]; $query = "UPDATE restHotel SET fare1 = '$fare1' WHERE id = '$id'"; $result = mysql_query($query) or die ("Error in Updating the query: $query. ".mysql_error()); } if($_POST['edit']) { foreach($_POST as $id) { $query = "SELECT * FROM restHotel WHERE id = '$id'"; $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); if (mysql_num_rows($result) > 0) { $row = mysql_fetch_array($result); ?> <form action="<?php echo $PHP_SELF ?>" method="POST"> <table width="100%" border="0"> <tr> <td>ID</td> <td><input type="text" name="id" value="<?php echo $row["id"]?>" size="3"/></td> </tr><tr> <td>Date/Time</td> <td><input type="text" name="dateTime" SIZE=25 value="<?php echo $row["dateTime"]; ?>" /> </td> </tr> <tr> <td>Pass type</td> <td><input type="text" name="passType" SIZE=30 value="<?php echo $row["passType"]; ?>"/> </td> </tr><tr> <td>Customer Name</td> <td><input type="text" name="custname1" SIZE=30 value="<?php echo $row["custname1"]; ?>"/> </td> </tr><tr> <td>Destination</td> <td><input type="text" name="des1" SIZE=30 value="<?php echo $row["des1"]; ?>"/> </td> </tr><tr> <td>Fare</td> <td><input type="text" name="fare1" SIZE=10 value="<?php echo $row["fare1"]; ?>"/> </td> </tr><tr><td> <input name="amend" type="Submit" value="Amend" onClick="return confirm('You are about to change this record, Are you sure you want to do this?');"/> </center></td> </table> </center> </form> <?php //Otherwise no rows found } //else echo "No rows found"; exit(); } Thanks Zed Quote Link to comment https://forums.phpfreaks.com/topic/139320-how-to-update/ Share on other sites More sharing options...
laPistola Posted January 3, 2009 Share Posted January 3, 2009 If what i think you trying to do is update every field in one given row then its simple. $query = "UPDATE restHotel SET fare1 = '$fare1' , des1 = '$des1', passType = '$passtype' WHERE id = '$id'"; and so on making sure you write the vars for them to! Quote Link to comment https://forums.phpfreaks.com/topic/139320-how-to-update/#findComment-728693 Share on other sites More sharing options...
zed420 Posted January 3, 2009 Author Share Posted January 3, 2009 Thanks for the reply, What I'm trying to do is to update just $fare field. Its a website for a taxi company, when require 1 car $fare1 could be updated if they require two cars then $fare2, $fare3 can be updated ... Thanks Zed Quote Link to comment https://forums.phpfreaks.com/topic/139320-how-to-update/#findComment-728709 Share on other sites More sharing options...
premiso Posted January 3, 2009 Share Posted January 3, 2009 foreach ($_POST as $key => $val) { if (stistr($key, "fare") !== false) { $sets[] = $key . " = '" . $val . "' "; } } $update = implode(", ", $sets); $query = "UPDATE restHotel SET " . $sets . " WHERE id = '$id'"; $result = mysql_query($query) or die ("Error in Updating the query: $query. ".mysql_error()); Should do the trick. Quote Link to comment https://forums.phpfreaks.com/topic/139320-how-to-update/#findComment-728796 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.