How to Update

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

Post Reply
zed420
Forum Commoner
Posts: 32
Joined: Wed Jun 04, 2008 11:24 am

How to Update

Post 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
User avatar
thisismyurl
Forum Newbie
Posts: 15
Joined: Wed Dec 03, 2008 8:00 am

Re: How to Update

Post 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'";
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How to Update

Post 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
Post Reply