Jump to content

How to Update


zed420

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/139320-how-to-update/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/139320-how-to-update/#findComment-728796
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.