Jump to content

Archived

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

tet3828

Help editing mySQL data

Recommended Posts

The script below gives the user a visual of items in the mySQL database I've setup.
I added an edit button to the script (commented below)
what script do I need to add in order to modify the entities of  an item's row in the DB when the edit button is pressed? :-\

<?php

if(!isset($_POST['submit']))
{
die("<html><body><form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">
<select name=\"cat\">
<option value=\"Holiday\">Holiday</option>
<option value=\"Animals\">Animals</option>
</select>
<input type=\"submit\" name=\"submit\" value=\"Search!\">
</form>
</body>
</html>");
}


$qry = "SELECT itemName,itemPrice,itemSmall FROM `products` WHERE itemCat='".$_POST['cat']."'";
$result = mysql_query($qry) or die(mysql_error());


while($row = mysql_fetch_array($result))

{
echo "<table border=\"1\"><tr><td>".$row['itemName']."</td></tr>";
echo "<tr><td><img src=\"".$row['itemSmall']."\" /></td>";
echo "</tr><tr><td>Price: $".$row['itemPrice']."</td>";
echo "</tr>";
echo "<tr><td> <input type=\"submit\" name=\"submit\" value=\"Edit This Item\">    \\\EDIT button
  </form>";
echo "</td></tr>";
echo "<br>";
echo "<br>";

 

}

echo "</table>";

?>

Share this post


Link to post
Share on other sites
UPDATE `table_name` WHERE id=$id_variable SET `field_you_are_editing`=$new_data LIMIT 1

assuming you have a form that gets the old data, & posts it as $new_data when it has been edited and submitted.  That's just the SQL.  if you need some help with the php, i'll help you, but as long as you have that SQL, all you should do is really just run the query.

Share this post


Link to post
Share on other sites
yep I deff need help with the php. do I have to put some sort of form action inside the while loop?

Share this post


Link to post
Share on other sites
and if it makes a difference...

the currently stored entities on the mySQL table 'products'

is as follows

itemName
itemDesc
itemID
itemPrice
itemCat
itemSub
itemAval
itemSmall
itemImage

Share this post


Link to post
Share on other sites
oh i'm sorry, i was confused as to what you were trying to do:

this doesn't have to be in a form at all

what you need to do is

still keep the loop that shows the info for each row, that part is fine

make a file called edit.php (you'll know what to do with this in a second)

now instead of a button, i'd use a link that says "edit";  now, inside the while loop where you have the link make it <a href="edit.php?id=$row['itemID']">(with the properly escaped characters of course)

now in your edit.php file, where you have the text form to update the row, use the following query to select the row:
[code]
<?php
$id = $_GET['id'];
//put your connection stuff and all that here, i'll skip to the query
$sql = "SELECT * FROM `products` WHERE `itemID`=\"$id\"";
//then echo the data into your textarea
?>
[/code]

hopefully i've cleared up the method of using the id in a link
as you can see, all you do is call the itemID value into the edit.php?id=itemID part of the link,
and that value is picked up by the $_GET variable in edit.php, and used to select the data from that row.

Share this post


Link to post
Share on other sites
in the commented line below.... why is the whole H REF tag being echoed insted of the actual link between the tags?



echo "<table border=\"1\"><tr><td>".$row['itemName']."</td></tr>";
echo "<tr><td><img src=\"".$row['itemSmall']."\" /></td>";
echo "</tr><tr><td>Price: $".$row['itemPrice']."</td>";
echo "</tr>";
echo "<tr><td><a href=\"edit.php?id=".$row['itemId']." />Edit This Item</a>";  \\\\this line actually echos the whole a href tag, what am I doing wrong?
echo "</td></tr>";
echo "<br>";
echo "<br>";

Share this post


Link to post
Share on other sites
because you don't use [/url] for one thing, you use [code]</a>[/code]. also, anchor tags aren't self closing, so there shouldn't be a slash at the end of your link tag.

Share this post


Link to post
Share on other sites

×

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.