简体   繁体   中英

PHP/MySql Error: Field doesn't exist, but it does

I can't figure out why I get this error on my process.php page:

Connected successfully
Could not run query: Table 'members_db.members where 'email' = '123456789'' doesn't exist 
Invalid query: Duplicate entry '123456789' for key 'email'

Doesn't make much sense, as it seems to be contradicting itself in the error message.

Here my process.php:

<?php
session_start();
$con = mysql_connect('localhost', 'root', ''); 
if (!$con) { 
    die('Could not connect: ' . mysql_error()); 
} 
echo 'Connected successfully<br />'; 

// make members the current db
$db_selected = mysql_select_db('members_db', $con);
if (!$db_selected) {
    die ('Can\'t use members database : ' . mysql_error());
}
$hash_password = md5($_POST['password']);

$email = $_POST['email'];

$result = mysql_query("SELECT email,password FROM `members WHERE 'email' = '$email'");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    $query = "INSERT INTO members (email, password)
    VALUES('".$_POST['email']."','".$hash_password."')";

    // Perform Query
    $result2 = mysql_query($query);

    // Check result
    // This shows the actual query sent to MySQL, and the error. Useful for debugging.
    if (!$result2) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        //$message .= 'Whole query: ' . $query;
        die($message);
    }   
$_SESSION['email']=$_POST['email'];
$_SESSION['password']=$hash_password;
$_SESSION['loggedin']="YES";
$url = "Location: /welcome.php";
header($url);
}
$_SESSION['email']=$_POST['email'];
$_SESSION['password']=$hash_password;
$url = "Location: /checklogin.php";
header($url);
?> 

It's probably a stupid error, as I'm learning while working on this project. Thanks for any help!

"SELECT email,password FROM `members WHERE 'email' = '$email'"

"SELECT email,password FROM `members` WHERE 'email' = '$email'"

you missed a backtick => `

Change

 "SELECT email,password FROM `members WHERE 'email' = '$email'"

to

"SELECT email,password FROM `members` WHERE 'email' = '$email'"

For the first error:

Change the first query

$result = mysql_query("SELECT email,password FROM `members WHERE 'email' = '$email'");

to

$result = mysql_query("SELECT email,password FROM `members` WHERE email = '$email'");

Second error:

The 'Duplicate entry' is because you already have an email with the same data and your 'email' column is unique, just delete the members row with email = 123456789.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM