简体   繁体   中英

PHP / MySQL: How to return info if record already exists (without update)

I am using the below to insert a new row into a MySQL db. The query for this is stored in a PHP file ( ajax.php ) and the input values are passed to this via an Ajax call in jQuery.

Everything works as intended but I would like to return a message to the user if an email is already in the db. I know how to update a db row using ON DUPLICATE KEY but can someone tell me how I can just check for this and echo something if it exists there already (ie without updating it) ?
(email is a single primary key so I only need to check for this column.)

My PHP:

$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
if($conn->connect_error){
    die("Connection failed: " . $conn->connect_error);
}
$email = $_POST["email"];
$dob = $_POST["dob"]; 
$sql = "INSERT INTO Users (email, dob) VALUES ('" . $email . "', '" . $dob . "')"; 
if ($conn->query($sql)){ 
    echo 'DB Update successful'; 
}else{ 
    echo 'DB Update failed'; 
} 
$conn->close();

You just need a simple SELECT call before inserting.

$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
if($conn->connect_error){
    die("Connection failed: " . $conn->connect_error);
}
$email = $_POST["email"];
$dob = $_POST["dob"];
$sql = "SELECT email FROM Users WHERE email = " .$email;
$query = $conn->query($sql);
if (mysqli_num_rows($query) > 0){
    echo "There exists an user with this email";
}
else {
    $sql = "INSERT INTO Users (email, dob) VALUES ('" . $email . "', '" . $dob . "')";
    if ($conn->query($sql)) {
        echo 'DB Update successful';
    }
    else {
        echo 'DB Update failed';
    };
}

$conn->close();

The simple way is just to count the occurances of entries in the table with the same email as you are trying to store. Counting rather than SELECTing means it is quicker and only a count is returned not a complete row that you have to then look at or count the rows in the result.

$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
if($conn->connect_error){
    die("Connection failed: " . $conn->connect_error);
}

$stmt = $Conn->prepare("Select Count(email) as cnt FROM Users WHERE email = ?");
$stmt->bind_param('s', $_POST["email"]);
$stmt->execute();
$stmt->bind_result($cnt)
$stmt->close();

if ( $Cnt > 0 ) {
    // its already there so do whatever you want in this case
} else {

    $sql = "INSERT INTO Users (email, dob) VALUES (?,?)"; 
    $stmt = $Conn->prepare($sql);
    $stmt->bind_param('ss', $_POST["email"],$_POST["dob"]);
    if ($stmt->execute(){ 
        echo 'DB Update successful'; 
    }else{ 
        echo 'DB Update failed'; 
    } 
}
$conn->close();

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