Jump to content


Photo

Help editing mySQL data


  • Please log in to reply
7 replies to this topic

#1 tet3828

tet3828
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts
  • Locationfort myers, FL

Posted 26 October 2006 - 10:56 PM

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>";

?>
[ Enter Your Signature Here ]
Always take caution when using other peoples work.

#2 Skatecrazy1

Skatecrazy1
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts
  • LocationSouthern California

Posted 26 October 2006 - 11:21 PM

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.
fuck it or fight it

#3 tet3828

tet3828
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts
  • Locationfort myers, FL

Posted 26 October 2006 - 11:50 PM

yep I deff need help with the php. do I have to put some sort of form action inside the while loop?
[ Enter Your Signature Here ]
Always take caution when using other peoples work.

#4 tet3828

tet3828
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts
  • Locationfort myers, FL

Posted 26 October 2006 - 11:58 PM

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
[ Enter Your Signature Here ]
Always take caution when using other peoples work.

#5 Skatecrazy1

Skatecrazy1
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts
  • LocationSouthern California

Posted 27 October 2006 - 12:49 AM

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:
<?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
?>

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.
fuck it or fight it

#6 tet3828

tet3828
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts
  • Locationfort myers, FL

Posted 27 October 2006 - 08:57 PM

you php ppl rock my frickin socks ThankX

-Trev
[ Enter Your Signature Here ]
Always take caution when using other peoples work.

#7 tet3828

tet3828
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts
  • Locationfort myers, FL

Posted 28 October 2006 - 12:46 AM

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>";
[ Enter Your Signature Here ]
Always take caution when using other peoples work.

#8 Skatecrazy1

Skatecrazy1
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts
  • LocationSouthern California

Posted 28 October 2006 - 04:34 AM

because you don't use [/url] for one thing, you use
</a>
. also, anchor tags aren't self closing, so there shouldn't be a slash at the end of your link tag.
fuck it or fight it




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users