Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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