edit problems

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
User avatar
monsta-toast
Forum Newbie
Posts: 16
Joined: Sat Jan 17, 2009 3:21 pm
Location: NEWCASTLE UPON TYNE

edit problems

Post by monsta-toast »

hi im quite new at php and have been creating a catalogue site for a college project, everything has went well until i came to the edit script. i can retreive the info from the db but it simply will not update! i got the original code from spoon0.com and moderfied it to my specs

Code: Select all

 
<?php
 
        include("connection.inc.php");  
        $connection = connect();
 
        $txtUsername = "";
        $txtPassword = "";
 
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["Submit"]))
   {
      $id = $_GET["id"];
      $sql = "SELECT * FROM inventory WHERE id=$id";
      $result = mysql_query($sql);        
      $myrow = mysql_fetch_array($result);
      ?> 
      <form action="edit.php" method="POST">
      <input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
      
      <div id="content1">
<div class="">
<h2>Edit stock:</h2><p><table align="left">
<td nowrap="nowrap" align="right">Title:</td>
<td><input type="text" name="title" value="<?php echo $myrow["title"] ?>" size=30></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Description:</td>
<td><textarea name="desc" rows=10 cols=30><?php echo $myrow["stockword"] ?></textarea></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Product Code:</td>
<td><input type="text" name="prod_code" value="<?php echo $myrow["prod_code"] ?>" size=30></td>
</tr>
<td nowrap="nowrap" align="right">Brand:</td>
<td><input type="text" name"brand" value="<?php echo $myrow["brand"] ?>" size="30" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Colour:</td>
<td><input type="text" name="colour" value="<?php echo $myrow["colour"] ?>" size="30"  /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Guarantee details:</td>
<td><input type="text" name="guarantee" value="<?php echo $myrow["guarantee"] ?>" size="30" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Material:</td>
<td> <input type="text" name="material" value="<?php echo $myrow["material"] ?>" size="30" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Product Type:</td>
<td><input type="text" name="prod_type" value="<?php echo $myrow["prod_type"] ?>" size="30" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Style:</td>
<td><input type="text" name="style" value="<?php echo $myrow["style"] ?>" size="30" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Price:</td>
<td><input type="text" name="price" value="<?php echo $myrow["price"] ?>" size=30></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Quantity</td>
<td><input type="text" name="qty" value="<?php echo $myrow["qty"] ?>" size=30></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Main Category:</td>
<td><input type="text" name="main" value="<?php echo $myrow["main_cator"] ?>" size=30></td>
  </tr>
  <td nowrap="nowrap" align="right">Sub Category:</td>
    <td ><input type="text" name="sub" value="<?php echo $myrow["second"] ?>" size=30></td>
 
  </tr>
 <tr valign="baseline">
<td nowrap="nowrap" align="right">Special Offer:</td>
<td><input type="text" name="special" value="<?php echo $myrow["special"] ?>" size=30></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Special offer info:</td>
<td><input type="text" name="special_deal_info" value="<?php echo $myrow["special_deal_info"] ?>" size=30 /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Additional Infomation:</td>
<td><textarea name="additional_info" rows="10" cols="30"><?php echo $myrow["additional_info"] ?></textarea></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="center">Insert</td>
 
     <td> <input type="hidden" name="cmd" value="edit">
   
      <input type="Submit" name="Submit" value="Submit"></td>
   </tr>
   </table>
      </form>
   
   <? } ?>
   <?
   if ($_POST["Submit"])
   {
     
     
      $title = $_POST["title"];
      $desc = $_POST["stockword"];
      $prod_code = $_POST["prod_code"];
      $brand = $_POST["brand"];
      $colour = $_POST["colour"];
      $guarantee = $_POST["guarantee"];
      $material = $_POST["material"];
      $prod_type = $_POST["prod_type"];
      $style = $_POST["style"];
      $main = $_POST["main_cator"];
      $sub = $_POST["second"];
      $price = $_POST["price"];
      $qty = $_POST["qty"];
      $special = $_POST["special"];
      $special_deal_info = $_POST["special_deal_info"];
      $additional_info = $_POST["additional_info"];
 
      
$sql = "UPDATE inventory SET title='$title',stockword='$desc',prod_code='$prod_code',brand='$brand',colour='$colour',guarentee='$guarentee',material='$material',prod_type='$prod_type',style='$style',main='$main',second='$sub',price='$price',qty='$qty',special='$special',special_deal_info='$special_deal_info',additional_info='$additional_info' WHERE id=$id)";
      
      echo "Thank you! Information updated.";
      print $sql;
   }
}
?>
 
 
 
 
anyone got any ideas?


cheers
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: edit problems

Post by Burrito »

you need to call mysql_query() to execute your query.

you should also make sure to sanitize that data before you update it.
User avatar
monsta-toast
Forum Newbie
Posts: 16
Joined: Sat Jan 17, 2009 3:21 pm
Location: NEWCASTLE UPON TYNE

Re: edit problems

Post by monsta-toast »

hi could you please explain? as i am going insane trying to fix this!
Wilio
Forum Newbie
Posts: 4
Joined: Sun Jan 18, 2009 10:12 am

Re: edit problems

Post by Wilio »

As the poster above said you need to use mysql_query() to perform your task.
And as for inserting data in your table, it's quite easy.

$query = INSERT INTO *table_name*(column_name1, column_name2) VALUES ('$var1', '$var2', etc...);

To run a query:

mysql_query($query);
User avatar
monsta-toast
Forum Newbie
Posts: 16
Joined: Sat Jan 17, 2009 3:21 pm
Location: NEWCASTLE UPON TYNE

Re: edit problems

Post by monsta-toast »

hi agian would it look like this:

Code: Select all

$sql = "UPDATE inventory SET title='$title',stockword='$desc',prod_code='$prod_code',brand='$brand',colour='$colour',guarantee='$guarentee',material='$material',prod_type='$prod_type',style='$style',cat='$main',subcat='$sub',price='$price',qty='$qty',special='$special',special_deal_info='$special_deal_info',additional_info='$additional_info' WHERE id=$id)";
 
 
$query = "INSERT INTO inventory (title, stockword, prod_code, brand, colour, guarantee, material, prod_type, style, cat, subcat, price, qty, special, special_deal_info, additional_info) VALUES ('$title','$desc','$prod_code','$brand','$colour','$guarantee','$material','$prod_type','$style','$main','$sub','$price','$qty','$special','$special_deal_info','$additional_info')";
 
 
    if (!mysql_query($query))
    {
  die('Error: ' . mysql_error());
  } 
 
 
or am i still a mile away
User avatar
monsta-toast
Forum Newbie
Posts: 16
Joined: Sat Jan 17, 2009 3:21 pm
Location: NEWCASTLE UPON TYNE

Re: edit problems

Post by monsta-toast »

fixed it :D

Code: Select all

 
      
$query = "UPDATE inventory SET title='$title',stockword='$desc',prod_code='$prod_code',brand='$brand',colour='$colour',guarantee='$guarentee',material='$material',prod_type='$prod_type',style='$style',cat='$main',subcat='$sub',price='$price',qty='$qty',special='$special',special_deal_info='$special_deal_info',additional_info='$additional_info' WHERE id=$id";
 
 
    if (!mysql_query($query))
    {
  die('Error: ' . mysql_error());
  } 
 
 
a big THANKS
Post Reply