Jump to content

PHP SQL Data modification


tet3828

Recommended Posts

My script is driving me nuts >:(

The script gets the an item number from a previous page and displays all its stored info as an html table. Great! that part is cool.
Then below the table is some html/php forms that give the use the option to modify the data.

However, instead of modifying the data I am pinpointing; it ereases it from the mySQL database. why is this happening and what can I do to get the result I am expecting?

I am obviously approaching this incorrectly. pls help me get back on track here. thanx again

the script:

<?php



/// Store Passed Data as $id
$id = $_GET['id'];

/// Select data from  mySQL table
$qry = "SELECT itemName,itemCat,itemSub,itemId,itemPrice,itemDesc,itemSmall FROM `products` WHERE `itemId`=\"$id\"";
$result = mysql_query($qry) or die(mysql_error());

///  page header
echo "Listed below is the stored data for the item #$id <br /> Use the fields below the item table to modify the information.";
echo "<br /> ";
echo "<br />";
echo "<br />";

/// Display item data loop
while($row = mysql_fetch_array($result))
{


echo "<table border=\"1\"><tr><td>".$row['itemName']."<td>".$row['itemId']."</td></td></tr>";
echo "<tr><td><img src=\"".$row['itemSmall']."\" /></td><td valign=top width=150>Description:<br />".$row['itemDesc']."</td>";
echo "</tr><tr><td>Price: $".$row['itemPrice']."</td><td>Avilibility: </td></tr>";
echo "<tr><td width=150>Catagory: ".$row['itemCat']."</td><td width=150>Sub-Catagory:".$row['itemSub']."</td></tr>";
}
echo "</table>";
echo "<br /> ";

/// modification form and query


?>
<?php
if (!isset($_POST['submit']))
{
$newId =  $_POST['newId'];
$query = "UPDATE products SET itemName='$newId' WHERE `itemId`=\"$id\"";
$result2 = mysql_query($query);

if ($result2) echo "<p>Update done.</p>";
else echo "<p>yeah that sux, shit didn't work</p>";
}
?>

<form method="post" action="<?php echo $PHP_SELF;?>">
<p>
Item Name:<br />
</p>
<input type="text" name="newName" size="10" maxlength="10" value="" />

<input type="submit" name="submit" value="Modify" />
</form>

</body>

Link to comment
Share on other sites

You need to expicility add any variable you want to access after the form submit to the form itself.  In this case, I think you need:

[code]<form>
...
<input type=hidden name=itemId value="<? echo $itemId ?>">
..
</form>[/code]

Then when that form is submitted, $_POST['itemId'] will be available.

The point here is that variable available the first time your script is run (in response to a GET request), are NOT available the second time, unless you explicitly make them available.  You could also use sessions to store the values you want to keep between script requests.
Link to comment
Share on other sites

I tried to read up on posting and getting info but im still not fully grasping the concept. I added the line to explictlly modify the item... worked great. The script now changes the data in my MySQL database.
However, I don't understand why the submit button is not working?
Also when the page is refreshed after pressing enter for the field. it doesn't change the value in my table until the second time I submit data with the enter key?  ???
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.