comp_eng_ Posted December 10, 2007 Share Posted December 10, 2007 Hello, I've just started to learn PHP/MySQL from some online tutorials I'm currently working on my first PHP script for deleting a row from my db I'm facing 2 problems with the code: 1- It's not deleting the selected row 2- It's only displaying the rows that have Even indices Hope u can help me this is my code: <?php //connect to mysql$con = mysql_connect("localhost","###","###" , $con ) or die('Unable to connect to DB.<br>'.mysql_connect_error()); // connect or show error message mysql_select_db("sm_FSpeaches") or die('Unable to select DB.'); //select or show error message //If cmd has not been initialized // Specifying that cmd is coming from a GET request helps keeping the script running, // in case the option register_globals was turned off (for security reasons) if(!isset($_GET["cmd"])) { //display all the rows $result = mysql_query("SELECT * FROM Speaches order by No") or die('Query failed.'); //run the while loop that grabs all the rows while($r=mysql_fetch_array($result)) { echo '<a href="delete.php?cmd=delete&No='.$r["No"].'>Delete '.$r["No"].' - '.$r["Title"].'</a><br>'; //make the title a link } } else if($_GET["cmd"]=="delete"){ $num=$_GET["No"]; //MUST BE FILTERED TO AVOID CODE-INJECTION ATTACKS! mysql_query("DELETE FROM Speaches WHERE No=$num"); if (!mysql_query("DELETE FROM Speaches WHERE No=$num")) echo 'Could not delete!'; else echo 'Row delete.'; } else { echo 'Uh oh! Someone made a bobo!<br>Are you playing with the url? DONT!'; } @mysql_free_result($result); //clean & dont show errors if any mysql_close($con); ?> Link to comment https://forums.phpfreaks.com/topic/81081-solved-problem-with-my-first-mysql-delete-script/ Share on other sites More sharing options...
Barand Posted December 11, 2007 Share Posted December 11, 2007 Your first problem is you don't connect to a database. That code is commented out. Link to comment https://forums.phpfreaks.com/topic/81081-solved-problem-with-my-first-mysql-delete-script/#findComment-411565 Share on other sites More sharing options...
comp_eng_ Posted December 11, 2007 Author Share Posted December 11, 2007 Thanks alot I solved my problem by modifing the link row echo '<a href="delete.php?cmd=delete&No='.$r["No"].'">Delete '.$r["No"].' - '.$r["Title"].'</a><br>'; I forgot to add the second " before Delete Link to comment https://forums.phpfreaks.com/topic/81081-solved-problem-with-my-first-mysql-delete-script/#findComment-411742 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.