Jump to content

[SOLVED] Strange MySQL/PHP Script error


neex1233

Recommended Posts

<?php $allow = array ('2');include ("/home/Username/public_html/folder/protect.php"); ?>

<title>Delete User</title>

<?php

$con = mysql_connect("localhost","My_Username","Password");

mysql_real_escape_string(mysql_select_db("My_DB", $con));

$username = $_POST['username'];

if (isset($_POST['delete'])) {

$sql = mysql_real_escape_string(mysql_query("DELETE FROM users WHERE username = '$username'"));

mysql_query($sql)

or die (mysql_error());

}

?>

 

<strong>User Deleted</strong>

<meta http-equiv="REFRESH" content="2;url=myurl">

 

That's my whole page. As you can see, there is no '1' on the page. Here is my error:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1

 

What is wrong with this script? It really does delete the user, but I always get this error and it doesn't redirect the user to my specified page. Thank you.

Link to comment
Share on other sites

Upon closer review, you are escaping the whole result of one query and putting that into another one -

 

$sql = mysql_real_escape_string(mysql_query("DELETE FROM users WHERE username = '$username'"));

 

You are supposed to only escape string data that goes into the query and $sql should be just the query string because you are then putting that into another mysql_query() statement.

Link to comment
Share on other sites

I did that, but I'm still getting the same error.

 

I might not have did it right, here's my new code:

 

<?php $allow = array ('5');include ("/home/username/public_html/folder/protect.php"); ?>
<title>Delete User</title>
<?php
$con = mysql_connect("localhost","Username","Password");
mysql_select_db("DB_Name", $con);
$username = $_POST['username'];
if (isset($_POST['delete'])) {
$sql = mysql_query("DELETE FROM users WHERE username = '$username'");
mysql_query(mysql_real_escape_string($sql))
or die (mysql_error());
}
?>

<strong>User Deleted</strong>
<meta http-equiv="REFRESH" content="1;url=myurl">

 

Oh yes, and the script still works, it's just the error that shows up and none of the things I put in the body.

Link to comment
Share on other sites

Actually, I fixed it. Well, not really, I just removed the 'mysql_error()' line.

 

If you want to properly fix it, implement my suggestion and post the current code.

 

(You can mark the top "Not Solved" again, by clicking the same button as marking it solved.)

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.