Klinetel
Klinetel

Reputation: 302

PHP Simple Insert Issue

I cannot figure out what the issue is with my PHP but the INSERT function seems to fail at all times. I have the correct table rows listed but there could just be a simple syntax issue. If that code is needed, I can supply it also. Here's my PHP and database structure:

enter image description here

  <?php  
 $con = mysql_connect('***','***','***');  
    if (!$con)  
{  
    echo "Failed to make connection.";  
    exit;  
}  
$db = mysql_select_db('***');  
if (!$db)  
{  
    echo "Failed to select db.";  
    exit;  
}  
$username   = $_POST['username'];  
$password   = $_POST['password'];  
$name      = $_POST['name'];  
$email      = $_POST['email'];  
$sql        = "SELECT username,email FROM Users WHERE username = '" . $username . "' OR email = '" . $email . "'";  
$query      = mysql_query($sql);  
if (mysql_num_rows($query) > 0)  
{  
    echo "That username or email already exists";  
}  
else  
{  
    $insert = "INSERT INTO Users(username,password,name,email) VALUES ('" . $username . "','" . $password . "','" . $name . "','" . $email . "')";  
    $query  = mysql_query($insert);  
    if ($query)  
    {  
        echo "Thanks for registering. You may now login.";  
    }  
    else  
    {  
        echo "Insert failed";  
    }  
}  
?>  

Upvotes: 0

Views: 119

Answers (3)

cssyphus
cssyphus

Reputation: 40038

This is untested but it may be as simple as this:

$insert = "INSERT INTO Users(username,password,name,email) VALUES ('$username','$password','$name','$email')";

Upvotes: 1

IchBin
IchBin

Reputation: 83

Throw the error if the query fails so you can see what's happening. Take this out before you go live though.

$query = mysql_query($insert);
if (!$query) {
    die('Invalid query: ' . mysql_error());
} else {
    echo "Thanks for registering. You may now login.";
}

Also a couple of suggestions for your login form. Escape the stuff your inputting into your database using http://www.php.net/manual/en/function.mysql-real-escape-string.php. I'd also recommend finding a good regex to validate the email before you save it. And even hashing the password at a minimum.

Upvotes: 1

Vilsol
Vilsol

Reputation: 722

Might be that you dont have a space in between Users and "(". Also, is your table with a capital letter. Maybe the table has some prefixes?

Upvotes: 1

Related Questions