Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prepared Statements Checking if row exists

Tags:

php

mysqli

I am new to Prepared Statements in Php and and wondering how you would best approach checking if a row already exists as I seem to be getting confused at this stage:

<?php 
include '../config.php'; 

$stmt = $conn->prepare("INSERT INTO users (email, password) VALUES (?, ?)");
$stmt->bind_param("ss", $email, $password);

if(mysqli_num_rows($stmt) > 0) {
    $email = $_POST['email'];
    $password = $_POST['password'];
    $stmt->execute();
    header('Location: ../login.php');   
} else {
    echo 'user already exists';
}
$stmt->close();
$conn->close();
?>

The above returns the else statement, if I switch them around it will insert again making use of the else statement and inserting the record but still not checking.

** UPDATE **

Here is my updated code for you to see after assistance below..

<?php 
include '../config.php'; 

ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

$email = $_POST['email'];
$password = $_POST['password'];

$stmt_check = $conn->prepare("SELECT * FROM users WHERE email =?");
$stmt_check->bind_param("s", $email);
$stmt_check->execute();
if($stmt_check->num_rows > 0) {
    echo 'user already exists';
} else {
    $stmt = $conn->prepare("INSERT INTO users (email, password) VALUES (?, ?)");
    $stmt->bind_param("ss", $email, $password);
    $stmt->execute();
    // header('Location: ../login.php');
}    
$stmt->close();
$conn->close();
?>
like image 888
danjbh Avatar asked Jan 03 '17 09:01

danjbh


2 Answers

mysqli_num_rows applicable to SELECT statement.

$stmt_check = $conn->prepare("SELECT * FROM users WHERE email =? AND password =?");
$stmt_check->bind_param("ss", $email, $password);
$stmt_check->execute();
if(mysqli_num_rows($stmt_check) > 0)

Updated Code

<?php 

include '../config.php'; 

$email = $_POST['email'];
$password = $_POST['password'];

$stmt_check = $conn->prepare("SELECT * FROM users WHERE email =? AND password =?");
$stmt_check->bind_param("ss", $email, $password);
$stmt_check->execute();
if($stmt_check->num_rows > 0){
  echo 'user already exists';
} else {
  $stmt = $conn->prepare("INSERT INTO users (email, password) VALUES (?, ?)");
  $stmt->bind_param("ss", $email, $password);
  $stmt->execute();
  header('Location: ../login.php');
}

$stmt->close();
$conn->close();
?>

Quick Link

  • mysqli_num_rows
  • mysql_num_rows

Which States,

This command is only valid for statements like SELECT or SHOW that return an actual result set. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows().

Edit 1

Change

if(mysqli_num_rows($stmt_check) > 0){

To

if($stmt_check->num_rows > 0){

See Example2 of PHP mysqli_num_rows() Function

like image 80
Nana Partykar Avatar answered Sep 29 '22 14:09

Nana Partykar


This is my updated code, please try

  <?php 

include '../config.php'; 

$email = $_POST['email'];
$password = $_POST['password'];

$stmt_check = $conn->prepare("SELECT * FROM users WHERE email =? AND password =?");
$stmt_check->bind_param("ss", $email, $password);
$stmt_check->execute();
$stmt_check->store_result();
$numberofrows = $stmt_check->num_rows;

if(($numberofrows) > 0)
  echo 'user already exists';
} else {
  $stmt = $conn->prepare("INSERT INTO users (email, password) VALUES (?, ?)");
  $stmt->bind_param("ss", $email, $password);
  $stmt->execute();
  header('Location: ../login.php');
}

$stmt->close();
$conn->close();
?>
like image 33
Dipali Sakle Systematix Avatar answered Sep 29 '22 14:09

Dipali Sakle Systematix