Page 1 of 1

How to Update

Posted: Sat Jan 03, 2009 10:01 am
by zed420
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

Code: Select all

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

Re: How to Update

Posted: Sat Jan 03, 2009 10:37 am
by thisismyurl
Zed, I'm not sure I completely understand what you're asking for but if you're trying to update multiple fields in the row, you can do:

Code: Select all

$query = "UPDATE restHotel SET fare1 = '$fare1',fare2 = '$fare2',fare3 = '$fare3'  WHERE id = '$id'";

Re: How to Update

Posted: Sat Jan 03, 2009 2:27 pm
by califdon
zed420 wrote: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.
You're absolutely right, that's your problem: your table is not normalized. This is a fundamental part of the design of every relational database table. What you described violates the very first rule of relational database normalization ("1st Normal Form"), that you must not have "repeating groups" in a record. Each customer and fare pair must be in a separate record in a table related to the appropriate hotel (or whatever your main table represents). I strongly recommend that you read about database normalization. Here are a few references:
http://en.wikipedia.org/wiki/Database_normalization
http://www.databasedev.co.uk/database_n ... asics.html
http://www.miswebdesign.com/resources/a ... r-3-2.html