Jump to content

Selecting a Row and Deleting It


sidorak95

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/235317-selecting-a-row-and-deleting-it/
Share on other sites

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>';
}
}
?>

 

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....

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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.