What am I missing?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
gathem
Forum Newbie
Posts: 4
Joined: Fri Sep 16, 2005 3:57 pm

What am I missing?

Post 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);
}
}
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

I guess you shouldn't quote numbers because that does not follow SQL standards.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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';
Post Reply