Click to See Complete Forum and Search --> : I can't figure out what is wrong.


ScoobyDooobyD00
11-13-2007, 07:58 AM
I am trying to update data in a database but it won't work. I don't get any errors but it doesn't work. I can delete the data and insert it but not delete it. Here is my coding.

<html>
<head>
<title>Edit A Puppy</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<body>
<?php
$conn = mysql_connect(HOST, USER, PASS)
or die
('Error connecting to mysql');
mysql_select_db(brw123_puppies);

if(isset($_GET['id']))
{
$query = "SELECT id, name, price, description ".
"FROM pups ".
"WHERE id = '{$_GET['id']}'";
$result = mysql_query($query) or die('Error : ' . mysql_error());
list($id, $name, $price, $description) = mysql_fetch_array($result,
MYSQL_NUM);

$description = htmlspecialchars($description);
}
else if(isset($_POST['save']))
{

$id = $_POST['id'];
$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];

// update the article in the database
$query = "UPDATE pups ".
"SET name = '$name', price = '$price', description = '$description' ".
"WHERE id = '$id'";
mysql_query($query) or die('Error : ' . mysql_error());

echo "$name has been updated";
}

mysql_close($conn);
?>


<form method="post">
<input type="hidden" name="id" value="<?=$id;?>">
<table border="1">
<tr>
<td width="100">Name:</td>
<td><input name="name" type="text"id="name" value="<?=$name;?>"></td>
</tr>
<tr>
<td width="100">Price:</td>
<td>$<input name="price" type="text" id="price" value="<?=$price;?>"></td>
</tr>
<tr>
<td width="100">Content</td>
<td><textarea name="description" cols="30" rows="6" id="description"><?=$description;?></textarea></td>
</tr>
<tr>
<td width="100">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td colspan="2" align="center"><input name="update" type="submit" id="update" value="Update <?=$name;?>"></td>
</tr>
</table>
<p align="center"><a href="puppyadmin.php">Manage Your Dogs</a></p>
</form>
</body>
</html>


Thanks in advance.

jasonahoule
11-13-2007, 11:41 AM
You don't have a field called "save". It should be "update".

isset($_POST['save']) should be isset($_POST['update'])

Also, if you are using integers as your id's then you should not have the single quotes here - "WHERE id = '$id'"

knowj
11-13-2007, 11:50 AM
You don't have a field called "save". It should be "update".

isset($_POST['save']) should be isset($_POST['update'])

Also, if you are using integers as your id's then you should not have the single quotes here - "WHERE id = '$id'"

It's fine to have single quotes there. but as jason said always check your variables when things don't seem to be working.

My biggest problem use to be having $result and typing $results it would takes hours sometimes for me to find this in long scripts.