Page 1 of 1

What am I missing?

Posted: Sun Sep 18, 2005 3:05 pm
by gathem
Ive been messing with this for a while now and as far as i can see everything is correct. The goal is to get the data from another page and then go through the database and delete any row that has the same value that is stored in the field stock. When i run it I dont get any errors but its not deleting it from my database. I verified that it is getting a variable from the other page and that it will compare the variable $stocknum and $stock and should delete the line however it doesnt delete it. Any suggestions or help would be greatly appreciated. Ive searched for a while as to what is preventing it. If anyone sees what im missing or doing incorectly Id thank you for posting.

Code: Select all

<?php
$stocknum =$_POST['stocknum'];

// variable to define connection features
$conn = mysql_connect("localhost", "user", "pass");

// pick database
mysql_select_db("myintern_cartest ", $conn);

// Create sql statement
$sql = "SELECT * FROM cars";

// Execute the sql statement
$result = mysql_query($sql, $conn) or die(mysql_error());

// while function
while($newArray = mysql_fetch_array($result))
{
$stock = $newArray['stock']; 

if ( $stock == $stocknum)
{
$sql = "DELETE FROM cars WHERE stock=$stocknum";
mysql_query($sql);
}
}
?>

Posted: Sun Sep 18, 2005 3:30 pm
by John Cartwright
change your last query to

Code: Select all

mysql_query($sql) or die(mysql_error());
and always quote column values in sql

Code: Select all

$sql = "DELETE FROM cars WHERE stock='$stocknum'";
and see if any errors come up

Posted: Sun Sep 18, 2005 3:54 pm
by Ree
I guess you shouldn't quote numbers because that does not follow SQL standards.

Posted: Sun Sep 18, 2005 4:41 pm
by timvw
MySQL isn't compliant with ANSI SQL anyway.. And i think most sql dbms perform an implicit cast anway..

Anyway, you should test if the posted value is really an integer.. Use a regular expression or is_int or whatever floats your boat..

You don't have to iterate over all the rows, and delete them one by one, the query below will delete all the rows that have a value for the stock attribute that equals $somevalue.

Code: Select all

DELETE FROM table WHERE stock='$somevalue';