Click to See Complete Forum and Search --> : Need help with update script


Alan P
07-31-2007, 09:58 AM
This update script will not update. I've gone over it a hundred times and can't understand why. Can anyone see what's wrong?

Thanks.


<table width="95%" border="0" cellspacing="0" cellpadding="3">

<?php

include("includes/dbconnect.php");

if(!isset($cmd))
{

$result = mysql_query("select * from $i order by item_title");

while($r = mysql_fetch_array($result))
{

$item_title = stripslashes($r['item_title']);
$id = $r['id'];

echo "
<tr>
<td width='5%'><font face=arial size=3>$id</font></td>
<td width='45%'><font face=arial size=3>$item_title</td>
<td width='50%'>
[<a href='edit_item.php?cmd=edit&id=$id'> Edit </a>] &nbsp;&nbsp;&nbsp;&nbsp;
[<a href='edit_item.php?cmd=delete&id=$id'> Delete </a>]</td>
</tr>
<tr><td colspan='3' style='border-top: 1px solid #666'>&nbsp;</td></tr>
";

}
}
?>

</table>

<?php

if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
if (!isset($_POST["submit"]))
{
$id = $_GET["id"];
$sql = "SELECT * FROM $i WHERE id=$id";

$result = mysql_query($sql);
$row = mysql_fetch_array($result);

$item_title = stripslashes($row['item_title']);
$item_desc = nl2br(stripslashes($row['item_desc']));
$item_desc = str_replace("<br />","",$item_desc);

$item_price = stripslashes($row['item_price']);
$in_stock = stripslashes($row['in_stock']);
$quantity = stripslashes($row['quantity']);
$payment = nl2br(stripslashes($row['payment']));
$payment = str_replace("<br />","",$payment);

$cat_id = stripslashes($row['cat_id']);

echo "

<center>
<table width='95%' cellspacing=0 cellpadding=3 border=0>
<tr>
<td>
<form action='edit_item.php' method='post'>
<input type=hidden name='id' value='$id'>

<strong>Item Title:</strong><br>
<input type='text' size='55' name='item_title' value='$item_title'><br><br>

<strong>Item Description:</strong><br>
<textarea name='item_desc' rows='5' cols='55' wrap='virtual'>$item_desc</textarea><br><br>

<strong>Item Price:</strong><br>
<input type='text' name='item_price' size='25' value='$item_price'>

<br><br>

<strong>Item in Stock -</strong> $in_stock<br>
Keep current answer or change:
";

if ($in_stock == 'No')
{
echo "
<input type='radio' name='in_stock' value='No' checked> Keep Current - No
<input type='radio' name='in_stock' value='Yes'> Yes
";
}
elseif ($in_stock == 'Yes')
{
echo "
<input type='radio' name='in_stock' value='Yes' checked> Keep Current - Yes
<input type='radio' name='in_stock' value='No'> No
";
}

echo "

<br><br>

<strong>Quantity:</strong><br>
<input type='text' name='quantity' size='3' value='$quantity'>

<br><br>

<strong>Payment method:</strong><br>
<textarea rows='10' name='payment' cols='75' wrap='virtual'>$payment</textarea>

<br><br>
";


$query = "SELECT * FROM $c";
$result = mysql_query($query) or die('Error, query failed');

echo "
<strong>Item Category: -</strong> ";

$query = "SELECT * FROM $c WHERE cat_id = '$cat_id'";
$result = mysql_query($query) or die('Error, query failed');
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$cat_name = stripslashes($row['cat_name']);
$cat_id = stripslashes($row['cat_id']);
echo "
$cat_name

<br>

(<span style='font-size:80%; color:red'>Keep current item category or select another one below.</span>)
<br>
<select name='cat_id' size='4'>
<option value='$cat_id' selected='selected'>Keep Current item Category</option>
";
}

$query = "SELECT * FROM $c ORDER BY cat_name";
$result = mysql_query($query) or die('Error, query failed');
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$cat_id = stripslashes($row['cat_id']);
$cat_name = stripslashes($row['cat_name']);

echo "
<option value='$cat_id'>$cat_name</option>
";
}
echo "
</select>

<br><br>

<input type='hidden' name='cmd' value='edit'>
<input type='submit' name='submit' value='Submit'>
</form>
</td>
</tr>
</table>
</center>
";
}

?>


<?php

if ($_POST["$submit"])
{

$item_title = mysql_real_escape_string($_POST['item_title']);
$item_desc = mysql_real_escape_string($_POST['item_desc']);
$item_price = mysql_real_escape_string($_POST['item_price']);
$in_stock = mysql_real_escape_string($_POST['in_stock']);
$quantity = mysql_real_escape_string($_POST['quantity']);
$payment = mysql_real_escape_string($_POST['payment']);
$cat_id = mysql_real_escape_string($_POST['cat_id']);

$sql = "UPDATE $i SET cat_id='$cat_id', item_title='$item_title', item_desc='$item_desc', item_price='$item_price', in_stock='$in_stock', quantity='$quantity', payment='$payment' WHERE id=$id";

$result = mysql_query($sql);
echo "<br><br><p align='center'><span style='color:red'>Item updated.</span><br><br><a href='edit_item.php'>Edit Another Itemd</a></p>";

}
}
?>

Beedge
07-31-2007, 10:56 AM
$sql = "UPDATE $i SET cat_id='$cat_id', item_title='$item_title', item_desc='$item_desc', item_price='$item_price', in_stock='$in_stock', quantity='$quantity', payment='$payment' WHERE id=$id";


Im not sure but I think that the var $id has not been declared.

can you please put echo $sql; after this line to make sure all vars are correct?

Alan P
07-31-2007, 01:36 PM
echo $sql;
No, nothing.


It's a mystery to me. I have dozens of update scripts similar to this one that work fine. I have one other update script that has the radio buttons and drop-down box like the ones you see in this script and it works fine. So it has to be something small that I've over-looked, I just can't see it.

When you run it you get nothing but the page title.

Detect
07-31-2007, 02:46 PM
shouldn't...

if ($_POST["$submit"])

be...

if ($_POST["submit"])

:)

Alan P
07-31-2007, 04:55 PM
Good Eye Detect! It's working!

Thanks all!