-1

This is my deleteriddle.php but it doesn't work. Please help me

<?php

require("dbcon.php");

$ridid = isset ($_GET['riddleid'])?$_GET['riddleid']:"";
$query = sprintf("DELETE FROM riddle WHERE riddleid= '$ridid'");
$result = mysql_query($query);

if (!$result) {
    die('Invalid query: ' . mysql_error());
}
else {
    echo '<script type="text/javascript">
    alert("Riddle successfully delete");
    window.location.href = "viewriddle.php";
    </script>';
}

mysql_close($dbcon);

?>
blex
  • 24,941
  • 5
  • 39
  • 72
fam29
  • 1
  • 1
  • What error do you get? – Tomasz W. Oct 08 '17 at 13:27
  • `mysql` brrrrrrrrr.... please use something better like `mysqli` or [`PDO`](https://stackoverflow.com/questions/28096054/how-to-replace-mysql-functions-with-pdo/28096059#28096059). – Mouser Oct 08 '17 at 13:28
  • there is no error. It always say that the riddle successfully deleted but when it goes back to view.php the record is still there – fam29 Oct 08 '17 at 13:29
  • Is this posts related to this? https://stackoverflow.com/questions/46626634/how-to-delete-the-currently-displayed-record-using-php – CodingInTheUK Oct 08 '17 at 13:29
  • I'm sorry Im kinda new to programming so I don't really know what is mysqli or PDO. please bear with me – fam29 Oct 08 '17 at 13:30
  • yes it is. I'm also the one who posted it – fam29 Oct 08 '17 at 13:30
  • Please look that up. Those newer libraries are safer when it comes to SQL-injection. – Mouser Oct 08 '17 at 13:31
  • I would like to look into it but I don't have much time. This website I'm making is due tomorrow. please help me – fam29 Oct 08 '17 at 13:33
  • but in the query DELETE FROM, whenever I specify the id like.. delete from riddle where riddleid = 1. It has deleted the record with the ID 1. but when I use the '$ridid' it doesn't work – fam29 Oct 08 '17 at 13:43

1 Answers1

0

Try this :

$query = sprintf("DELETE FROM riddle WHERE riddleid= %u", $ridid);
Sagar Jajoriya
  • 2,377
  • 1
  • 9
  • 17
  • 1
    Try to print the `$query` just before the `mysql_query`. Check its right or not ?? – Sagar Jajoriya Oct 08 '17 at 13:39
  • still nothing happens. but in the query DELETE FROM, whenever I specify the id like.. delete from riddle where riddleid = 1. It has deleted the record with the ID 1. but when I use the '$ridid' it doesn't work – fam29 Oct 08 '17 at 13:43
  • Remove the single quotes around the `$ridid` in your query and try – Sagar Jajoriya Oct 08 '17 at 13:44
  • when I removed the single quote..Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 – fam29 Oct 08 '17 at 13:46