Edit A Database With PHP???
Hey I Created The Code Bellow, but I Can't Figure Out Why It Doesn't Actually Update The Database... Everything Appears Correct and The Page Loads Error Free... I'm Hoping Some One Can Help...
<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","bluestar_admin","admin" );
//select which database you want to edit
mysql_select_db("bluestar_members");
//If cmd has not been initialized
if(!isset($cmd))
{
//display all the news
$result = mysql_query("select * from products order by id");
//run the while loop that grabs all the news scripts
while($r=mysql_fetch_array($result))
{
//grab the title and the ID of the news
$name=$r["name"];//take out the title
$barcode=$r["barcode"];//take out the id
$price=$r["price"];//take out the id
$id=$r["id"];//take out the id
//make the title a link
echo "<a href='edit.php?cmd=edit&id=$id'>$title - Edit</a>";
echo "<br>";
}
}
?>
<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
if (!isset($_POST["submit"]))
{
$id = $_GET["id"];
$name = $_GET["name"];
$barcode = $_GET["barcode"];
$price = $_GET["price"];
$sql = "SELECT * FROM products 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"] ?>">
<input type=hidden name="name" value="<?php echo $myrow["name"] ?>">
<input type=hidden name="barcode" value="<?php echo $myrow["barcode"] ?>"> <input type=hidden name="price" value="<?php echo $myrow["price"] ?>">
Name Of Product:<INPUT TYPE="TEXT" NAME="name" VALUE="<?php echo $myrow["name"] ?>" SIZE=30><br>
Barcode:<INPUT TYPE="TEXT" NAME="barcode" VALUE="<? echo $myrow["barcode"] ?>" SIZE=30><br>
Price:<INPUT TYPE="TEXT" NAME="price" VALUE="<?php echo $myrow["price"] ?>" SIZE=30><br>
<input type="hidden" name="cmd" value="edit">
<input type="submit" name="submit" value="submit">
</form>
<? } ?>
<?
if ($_POST["$submit"])
{
$name = $_POST["name"];
$barcode = $_POST["barcode"];
$price = $_POST["price"];
$sql = "UPDATE products SET name='$name',barcode='$barcode',price='$price' WHERE id=$id";
$result = mysql_query($sql);
echo "Thank You! Information Updated!";
}
}
?>
|