Jump to content

Trouble deleting a row from a sql table


justin2021

Recommended Posts

I'm trying to write a PHP program that a user will use to register to my site. I have a temporary table temp for which the user's information will be stored until they verify their account using an email that is sent to them. Once they verify their account through email, the information stored in the temporary table will be moved to a table called 'users'. I then want to delete the temporary data that they had stored. Unfortunately I'm having trouble doing this. This snippet of code isn't exactly the entire code, but just a test I've been using since I realized that my real code wasn't working as far as deletion goes. Here is my test code.

 

<?php
include('connect.php');

$sql2="SELECT username FROM temp WHERE username = 'justin'";
$result2=mysql_query($sql2);
if($result2)
echo ("Record found"."<BR>");
else
echo ("Record not found");
$sql3="DELETE FROM temp WHERE username='justin'";
$result3=mysql_query($sql3);
if($result3)
echo ("SUCESSFUL DELETION");
else
echo ("failed");
?>

 

In this code, I check the temp table to see if 'justin' can be found. This has consistently returned "Record found". On the other hand, my deletion command has not worked yet and will constantly fail. connect.php is correct as far as I know and has worked flawlessly for the other parts of this small project. 

 

Is something wrong here? What could be the problem?

Link to comment
Share on other sites

So tell us what does happen, if the record isn't being deleted.

 

When I run this code, I get a message saying "Record found, failed". I check the database using PHPmyAdmin and the record remains in the temp table. Here is the actual program that I am wanting to run. It stores the data perfectly fine into the users table, but as I stated, the temp data remains.

 


<?php
include("connect.php");

//set variable to get passkey from URL
$passkey=$_GET['passkey'];

$sql1="SELECT * FROM temp WHERE code='$passkey'";
$result1=mysql_query($sql1);

//if sucessfully queried
if($result1)
{
echo ("sucessfully queried");
//how many have passkey
$count=mysql_num_rows($result1);
//if passkey is in database, retrieve data
if($count == 1)
{
	$rows = mysql_fetch_array($result1);
	$namex=$rows['username'];
	$emailx=$rows['email'];
		$passwordx=$rows['password'];
		echo ("<BR>"."sucessfully counted");

	//takeout spaces
	$name=str_replace(' ','',$namex);
	$email=str_replace(' ','',$emailx);
	$password=str_replace(' ','',$passwordx);

	//insert into users table from tmp
	$sql2="INSERT INTO users (username,email,password) VALUES ('$name','$email','$password')";
	$result2=mysql_query($sql2);
	if($result2)
	{
		echo ("<BR>"."SUCESSFUL INSERTION");
		//header("Location:sucessful_registration.html");
		$sql3="DELETE FROM temp WHERE code='$passkey'";
		$result3=mysql_query($sql3);
	}
}
}
else
{
echo ("ERROR: Incorrect confirmation code");
}
?>	

 

 

Link to comment
Share on other sites

Instead of just "failed", echo the query string and error message while developing. Also, there's no reason to use parentheses with echo . . .

 

else
   echo "<br>Query $sql3 failed<br>With error: " . mysql_error() . '<br>';

 

Great. That helped out a lot. Turns out that the user with access to the database didn't have DELETE enabled in its privileges.

Link to comment
Share on other sites

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.