Jump to content

[SOLVED] PHP Update Function


naykidd

Recommended Posts

So basically the last 2 months i've been teaching myself PHP, the following page used to have its own table for a make of car, (I.e. 1 for Ford, 1 for Vauxhall etc) but i recently changed it to 1 Master table, where each edit page drags through the make (Ford drags Ford from "parts" in this case)

Since doing this, i cant seem to get entries to update! I click update, the top fields are filled, and then i change something and hit submit and the address changes to ID=xxxx (xxxx is the id number that appears), but nothing updates it just seems to refresh the listing!

 

Can anyone point out what im missing?

 

<?php
require_once("Connections/connection.php"); // database connection
session_start(); 
include("includes/security.php"); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<style type="text/css">
<!--
.style3 {font-size: 9}
--> 
</style>

</head>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<!-- TemplateBeginEditable name="EditRegion1" --><?php $car = chrysler;
$carmakes = chrysler; ?><!-- TemplateEndEditable --> <?php
error_reporting (E_ALL ^ E_NOTICE);
///////////////////////////////////////////////// variables 
$Product = $_POST['Product'];
$Make = $_POST['Make'];
$Model = $_POST['Model'];
$Year = $_POST['Year'];
$Fitment = $_POST['Fitment'];
$Part = $_POST['Part'];
$Location = $_POST['Location'];
$orgPrice = $_POST['OriginalPrice'];
$Price = $_POST['Price'];
$Submit = $_POST['Submit'];
$del = $_GET['del'];
$upd = $_GET['upd'];
$update_id = $_POST['update_id'];
//////////////////////////////////////
$query = sprintf("SELECT * FROM parts where ID='$upd'"); 
$result = @mysql_query($query); //tells the database
$rowUpdate = mysql_fetch_array($result); //fetches the result
//////////////////////////////////////
/////////////////////////////// The following checks if there is stuff in every field and not in the hidden update field

if ($Submit && $Product && $Make && $Model && $Year && $Fitment && $Part && $Location && $orgPrice && $Price && ! $update_id){

/////////////////////////////////////////////////////// this inserts it
$query 		= sprintf("INSERT INTO parts (Product, Make, Model, Year, Fitment, Part, Location, OriginalPrice, Price) values ('$Product', '$Make', '$Model', '$Year', '$Fitment', '$Part', '$Location', '$orgPrice', '$Price')");
		  mysql_query($query)or die (mysql_error());

}elseif($Submit && $update_id){ ////////////////Otherwise
////////////////////////////////////////// UPDATE
$query = sprintf("UPDATE parts set Product='$Product', Make='$Make', Model='$Model', Year='$Year', Fitment='$Fitment', Part='$Part', Location='$Location', OriginalPrice='$orgPrice', Price='$Price', where ID='$update_id'");
$result = @mysql_query($query);
}

///delete a record if
if ($del){

//////////////////////////////////////////////// This is the code for deleting a line
$query 		= sprintf("DELETE FROM parts where ID='$del'");
		  mysql_query($query)or die (mysql_error());
}

////////////////////////////////////// Then show it
$query = sprintf("SELECT * FROM parts WHERE make='$car'"); //selects all data from the database
$result = @mysql_query($query); //tells the database
$row = mysql_fetch_array($result); //fetches the result
$query = " ORDER BY `Model`";
/////////////////////////////////////

?>
<body>
<p><br />
    <a href="../welcome.php">Back to Makes</a> </p>
<table width="891" border="0">
</table>
<!-- TemplateBeginEditable name="EditRegion2" --><!-- TemplateEndEditable -->
<table width="200" border="0">
  <tr>
    <td width="996"><form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
      <p> </p>
      <table width="103%" height="104" border="1">
        <tr>
          <td width="13%"><div align="center">Product</div></td>
          <td width="7%"><div align="center">Make</div></td>
          <td width="11%"><div align="center">Model</div></td>
          <td width="10%"><div align="center">Year</div></td>
          <td width="15%"><div align="center">Fitment</div></td>
          <td width="6%"><div align="center">Part</div></td>
          <td width="10%"><div align="center">Location</div></td>
          <td width="10%"><div align="center">OriginalPrice</div></td>
          <td width="7%"><div align="center">Price</div></td>
        </tr>
        <tr>
          <td><span class="style3">
            <label>
              <input name="Product" type="text" id="Product" value="<?php echo $rowUpdate['Product']; ?>" size="20" />
              </label>
            </span>
                <label></label></td>
          <td><label>
            <input name="Make" type="text" id="Make" value="<?php echo $rowUpdate['Make']; ?>" size="10" />
          </label></td>
          <td><label>
            <input name="Model" type="text" id="Model" value="<?php echo $rowUpdate['Model']; ?>" size="18" />
          </label></td>
          <td><label>
            <input name="Year" type="text" id="Year" value="<?php echo $rowUpdate['Year']; ?>" size="15" />
          </label></td>
          <td><label>
            <input name="Fitment" type="text" id="Fitment" value="<?php echo $rowUpdate['Fitment']; ?>" />
          </label></td>
          <td><label>
            <input name="Part" type="text" id="Part" value="<?php echo $rowUpdate['Part']; ?>" size="12" />
          </label></td>
          <td><label>
            <input name="Location" type="text" id="Location" value="<?php echo $rowUpdate['Location']; ?>" size="15" />
          </label></td>
          <td><label>
            <input name="OriginalPrice" type="text" id="OriginalPrice" value="<?php echo $rowUpdate['OriginalPrice']; ?>" size="15" />
          </label></td>
          <td><label>
            <input name="Price" type="text" id="Price" value="<?php echo $rowUpdate['Price']; ?>" size="10" />
          </label></td>
        </tr>
        <tr>
          <td colspan="10"><label> </label>
                <div align="center">
                  <label>
                  <input type="reset" name="Reset" id="Reset" value="Reset" />
                  </label>
                  <input type="submit" name="Submit" id="Submit" value="Submit" />
                  <input name="Submit" type="hidden" id="Submit" value="1" />
                  <input name="update_id" type="hidden" id="update_id" value="<?php echo $rowUpdate['ID']; ?>" />
              </div></td>
        </tr>
      </table>
      <br />
      <br />
    </form></td>
  </tr>
</table>
<table width="1194" height="76" border="1">
  <tr>
    <td width="56">Edit</td>
    <td width="40" height="42"><div align="center">ID</div></td>
    <td width="113"><div align="center">Product</div></td>
    <td width="111"><div align="center">Make</div></td>
    <td width="116"><div align="center">Model</div></td>
    <td width="89"><div align="center">Year</div></td>
    <td width="101"><div align="center">Fitment</div></td>
    <td width="82"><div align="center">Part</div></td>
    <td width="67"><div align="center">Location</div></td>
    <td width="87"><div align="center">OriginalPrice</div></td>
    <td width="31"><div align="center">Price</div></td>
        <div align="center"></div></td>
    <td width="57"> </td>
  </tr>
  <?php do {  ?>
  <tr>
    <td><label> </label>
        <div align="center">
          <input type="button" name="Update" id="Update" value="Update" onclick="document.location.href='<?php echo $carmakes?>_staff.php?upd=<?php echo $row['ID'] ?>'" />
      </div></td>
    <td height="25"><?php echo $row['ID']; ?></td>
    <td><?php echo $row['Product']; ?></td>
    <td><?php echo $row['Make']; ?></td>
    <td><?php echo $row['Model']; ?></td>
    <td><?php echo $row['Year']; ?></td>
    <td><?php echo $row['Fitment']; ?></td>
    <td><?php echo $row['Part']; ?></td>
    <td><?php echo $row['Location']; ?></td>
    <td><?php echo $row['OriginalPrice']; ?></td>
    <td><?php echo $row['Price']; ?></td>
    <td><input type="button" name="Button" id="Button" value="Delete" onclick="document.location.href='<?php echo $carmakes?>_staff.php?del=<?php echo $row['ID'] ?>'" />
    </td>
  </tr>
  <?php }while ($row = mysql_fetch_array($result));   ?>
</table>
<p> </p>
<p> </p>
<p> </p>
<p></p>

<p>
  <label></label></p> 
</body>
</html>

 

 

Link to comment
https://forums.phpfreaks.com/topic/171275-solved-php-update-function/
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.