sidorak95 Posted May 2, 2011 Share Posted May 2, 2011 Hi, this is the first time I've asked for some help from a forum. I guess this code has sort of got me stumped. So, I have a system where a user can enter a number. The number goes into a mysql database. A random number is also generated and it is added into another column. So, I want users to be able to delete the number using the random number. I've shown it to them, and I need help working my delete script. <?php $con = mysql_connect("localhost","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("a8784hos_adopts", $con); $adopt = $_POST['adoptid']; $uniquecode = $_POST['uniquecode']; $result = mysql_query("SELECT uniquecode, id FROM id WHERE id='$adopt'"); mysql_fetch_array($result); if (($adopt == $row['id']) || ($uniquecode == $row['uniquecode'])){ mysql_query("DELETE FROM id WHERE id='$adopt'"); echo 'Done! <br/><br/><a href="tester.php">Go back....</a>'; } else { echo 'Bad unique code. <br/><br/><a href="tester.php">Go back....</a>'; } ?> So, the random number is uniquecode in the database, and the number is id. However, when I try this code, it always tell me I have a bad unique code. What am I doing wrong? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/235317-selecting-a-row-and-deleting-it/ Share on other sites More sharing options...
spiderwell Posted May 2, 2011 Share Posted May 2, 2011 i dont think you need the ($adopt == $row['id']) for a start as that will always be true anyway. you missed out passing the record set result to $row <?php $con = mysql_connect("localhost","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("a8784hos_adopts", $con); $adopt = $_POST['adoptid']; $uniquecode = $_POST['uniquecode']; $result = mysql_query("SELECT uniquecode, id FROM id WHERE id='$adopt'"); mysql_fetch_array($result); while ($row = mysql_fetch_array($result)){ if ($uniquecode == $row['uniquecode']){ mysql_query("DELETE FROM id WHERE id='$adopt'"); echo 'Done! <br/><br/><a href="tester.php">Go back....</a>'; } else { echo 'Bad unique code. <br/><br/><a href="tester.php">Go back....</a>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235317-selecting-a-row-and-deleting-it/#findComment-1209261 Share on other sites More sharing options...
sidorak95 Posted May 2, 2011 Author Share Posted May 2, 2011 Wow, I can't believe I missed that. I guess I was trying to substitute while with if....... Anyways, now whatever I put in always returns a blank page with nothing in it. I experimented and put an echo in three places: <?php $con = mysql_connect("localhost","a8784hos_sid","bobafett1"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("a8784hos_adopts", $con); $adopt = $_POST['adoptid']; $uniquecode = $_POST['uniquecode']; $result = mysql_query("SELECT uniquecode, id FROM id WHERE id='$adopt'"); echo "Hi."; mysql_fetch_array($result); echo "Hi."; while ($row = mysql_fetch_array($result)){ if ($uniquecode == $row['uniquecode']){ mysql_query("DELETE FROM id WHERE id='$adopt'"); echo 'Done! <br/><br/><a href="tester.php">Go back....</a>'; } else { echo 'Bad unique code. <br/><br/><a href="tester.php">Go back....</a>'; } } echo "Hi."; ?> They all show up, so I guess it's something about the while loop.... Quote Link to comment https://forums.phpfreaks.com/topic/235317-selecting-a-row-and-deleting-it/#findComment-1209265 Share on other sites More sharing options...
spiderwell Posted May 2, 2011 Share Posted May 2, 2011 and I should have looked closer at your code and my answer, remove the extra mysql_fetch_array($result); between the first 2 echo "Hi" statements Quote Link to comment https://forums.phpfreaks.com/topic/235317-selecting-a-row-and-deleting-it/#findComment-1209269 Share on other sites More sharing options...
sidorak95 Posted May 2, 2011 Author Share Posted May 2, 2011 Thanks, that solved it! I should've noticed the extra one, ugh I'm slow today. And of course with every solution comes another problem. The else condition never occurs, a blank page just appears. I'm not sure why it wouldn't, its the simplest of simple codes... Quote Link to comment https://forums.phpfreaks.com/topic/235317-selecting-a-row-and-deleting-it/#findComment-1209272 Share on other sites More sharing options...
spiderwell Posted May 2, 2011 Share Posted May 2, 2011 blank page should only occur if no data has returned from the initial select statement, i suspect you pressed back in your browser and have tried to delete the same record Quote Link to comment https://forums.phpfreaks.com/topic/235317-selecting-a-row-and-deleting-it/#findComment-1209276 Share on other sites More sharing options...
sidorak95 Posted May 2, 2011 Author Share Posted May 2, 2011 It looks like I must have accidentally put in a letter, which causes it to be blank. Now to filter out letters..... Thanks so much for your help! Quote Link to comment https://forums.phpfreaks.com/topic/235317-selecting-a-row-and-deleting-it/#findComment-1209277 Share on other sites More sharing options...
spiderwell Posted May 2, 2011 Share Posted May 2, 2011 error trapping, its the bulk of a programmer's work. its 10% building a script and 90% preventing users from breaking it Quote Link to comment https://forums.phpfreaks.com/topic/235317-selecting-a-row-and-deleting-it/#findComment-1209279 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.