Forum Moderators: coopster

Message Too Old, No Replies

Syntax Explanation

Can't delete from DB

         

calmseas

12:06 am on Aug 22, 2010 (gmt 0)

10+ Year Member



The following snippet produces a value of '1' for $result, so the row in TABLE course_signup is not deleted. There is no mysql_error() produced. The values exist and are correct in the DB, so I don't understand why the row is not being deleted.
Also, if $lastName ECHOs the correct string value, why can't I use that variable in the query string, rather than .......' " .$lastName. " '..........
Is there a way I can get a more descriptive error as to why the DELETE failed?

echo $courseTitle ;
echo $lastName ;
echo $firstName."<br />"
$query = "DELETE FROM course_signup WHERE course_title = ' ".$courseTitle." '
AND last = ' ".$lastName." '
AND first = ' ".$firstName." ' " ;

$result = mysqli_query($db , $query ) or die( mysql_error() ) ;

echo "result is: " ;
echo $result ;

mack

12:08 am on Aug 22, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



When you echo the values at the start are they being extracted from the DB.

It might be an idea to use a while loop. and loop through the records then delete records that match?

Mack.

rocknbil

2:07 am on Aug 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is there a way I can get a more descriptive error as to why the DELETE failed?


You have spaces. '$courseTitle." '
which is 'programming 101 ', not 'programming 101'

You don't need the concatenation with scalar variables, it's making it harder. But to answer the question,

echo "<p>$courseTitle $lastName $firstName </p>";

$query = "select id from course_signup where course_title='$courseTitle' and last='$lastName' and first = '$firstName'";

$result = mysqli_query($db,$query) or die("Can't get id in delete: " . mysql_error());
if ($row = mysqli_fetch_array($result)) {
$id = $row[0];
// Yes you can recycle "$query"
$query = "delete from course_signup where id=$id";
mysqli_query($db,$query) or die("Can't do the delete: " . mysql_error());
}
else { echo "<p>No record was found to delete.</p>"; }

calmseas

4:24 pm on Aug 22, 2010 (gmt 0)

10+ Year Member



EXCELLENT..........thank you.