简体   繁体   中英

What is wrong in these mysqli prepared statements?

I'm trying to make a registration script using PHP with Mysql database. The insertion cannot be done. If I register with an email-id which is already in the database, it is working fine. But, the script fails to insert new entries. It is returning 'bool(false)'. I've tried the to do the same using PDO. The insertion can't be done. So, I tried mysqli prepared statements instead and even this yields the same result. Here is the code.

<?php

$dbh = new mysqli('localhost', 'user', 'pass', 'db');

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

        $ip = $_SERVER['REMOTE_ADDR'];
        $name = $_POST['$name'];
        $mail = $_POST['mail'];
        $passw = $_POST['passw'];
        $codeone = $_POST['codeone'];
        $descs = $_POST['desc'];
        $newstrings = 'specialstring';
        $encrypted_pass = crypt( $passw );

        $stmt = $dbh->prepare("SELECT mail FROM userrecs WHERE mail=?");
        $stmt->bind_param('s',$mail);


        if($stmt->execute())
        {
        $stmt->store_result();
        $rows = $stmt->num_rows;
        if($rows == 1)
            {
                    session_start();
                    $_SESSION['notification_one'] = 'bla';
                    header('location:/someplace');  
                 }

              else {
                    $statement = $db->prepare("INSERT INTO userrecs (ip,name,mail,pass,codeone_one,desc_one,spcstrings) VALUES (?,?,?,?,?,?,?)");
              $statement->bind_param('ssssiss',$ip,$name,$mail,$encrypted_pass,$codeone,$descs,$newstrings);

try {
    if($statement->execute())
    {
            session_start();
            $_SESSION['noti_two'] = 'bla';
            header('location:/someplace');  
               }
               else
               {
               var_dump($statement->execute());
              $statement->errorInfo();
               }
    }
    catch(PDOException $pe) {
        echo "S";
    echo('Connection error, because: ' .$pe->getMessage());
    }       
              }
        }
    }
    else{
        header('location:/someplace');
    }
?>

EDIT:

This is the PDO-only code. I was mixing PDO and mysqli in the previous code.

<?php


$dsn = 'mysql:dbname=dbname;host=localhost';
$user = 'user';
$password = 'pass';
    $dbh = new PDO($dsn, $user, $password);

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

        $ip = $_SERVER['REMOTE_ADDR'];
        $name = $_POST['$name'];
        $mail = $_POST['mail'];
        $pass = $_POST['passw'];
        $codeone = $_POST['codeone'];
        $descs = $_POST['desc'];
        $newstrings = 'specialstring';
        $encrypted_pass = crypt( $passw );


        $sql = "SELECT mail FROM userrecs WHERE mail=:mail";
        $statement = $dbh->prepare($sql);
        $statement->bindValue(':mail',$mail,PDO::PARAM_STR);

        if($statement->execute())
            {
              if($statement->rowCount() == 1)
                 {
                        session_start();
                    $_SESSION['noti_one'] = 'bla';
                    header('location:/someplace');  
                 }

              else {
                $sql2 = "INSERT INTO userrecs (ip,name,mail,pass,codeone_one,desc_one,spcstrings) VALUES (:ip,:name,:mail,:encrypted_pass,:codeone,:descs,:newstrings)";

                $stmt = $dbh->prepare($sql2);
                $stmt->bindParam(':ip',$ip,PDO::PARAM_STR);
                $stmt->bindParam(':name',$name,PDO::PARAM_STR);
$stmt->bindValue(':mail',$mail,PDO::PARAM_STR);
$stmt->bindParam(':encrypted_pass',$encrypted_pass,PDO::PARAM_STR);
$stmt->bindParam(':codeone',$codeone,PDO::PARAM_STR);
$stmt->bindParam(':descs',$descs,PDO::PARAM_STR);
$stmt->bindParam(':newstrings',$temstr,PDO::PARAM_STR);

try {
    if($stmt->execute())
    {

            session_start();
            $_SESSION['noti_two'] = 'bla';
            header('location:/someplace');  
               }
               else
               {
               var_dump($stmt->execute());
              $stmt->errorInfo();
               }
    }
    catch(PDOException $pe) {
        echo "S";
    echo('Connection error, because: ' .$pe->getMessage());
    }
              }

        }
    }
    else{
        header('location:/someplace');
    }
?>

Please ignore variable or table names . I edited some of the names here.

I believe you have an error in your logic. Try this code and see what you get ...

<?php

$dbh = new mysqli('localhost', 'user', 'pass', 'db');

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

    $ip = $_SERVER['REMOTE_ADDR'];
    $name = $_POST['$name'];
    $mail = $_POST['mail'];
    $passw = $_POST['passw'];
    $codeone = $_POST['codeone'];
    $descs = $_POST['desc'];
    $newstrings = 'specialstring';
    $encrypted_pass = crypt($passw);

    $stmt = $dbh->prepare("SELECT mail FROM userrecs WHERE mail=?");
    $stmt->bind_param('s', $mail);
    $test = $stmt->execute();   
    if($test) {
        $stmt->store_result();
        $rows = $stmt->num_rows;
        if($rows == 1) {
            session_start();
            $_SESSION['notification_one'] = 'bla';
            header('location:/someplace');
        } else {
            $statement = $db->prepare("INSERT INTO userrecs (ip,name,mail,pass,codeone_one,desc_one,spcstrings) VALUES (?,?,?,?,?,?,?)");
            $statement->bind_param('ssssiss', $ip, $name, $mail, $encrypted_pass, $codeone, $descs, $newstrings);

            try {
                if($statement->execute()) {
                    session_start();
                    $_SESSION['noti_two'] = 'bla';
                    header('location:/someplace');
                } else {
                    var_dump($statement->execute());
                    $statement->errorInfo();
                }
            } catch (PDOException $pe) {
                echo "S";
                echo('Connection error, because: ' . $pe->getMessage());
            }
        }
    }else{
        echo "test is not ok";
        var_dump($test);
    }
} else {
    header('location:/someplace');
}

You are mixing PDO and mysqli driver in the same script, this is not possible.

Please use either one but not both.

PDO is the prefferred extension.


EDIT:

In your query:

INSERT INTO userrecs (ip,name,mail,pass,codeone_one,desc_one,spcstrings) VALUES (...)

NAME is a mysql reserved keyword , you escape it by using backticks:

INSERT INTO userrecs (ip,`name`,mail,pass,codeone_one,desc_one,spcstrings) VALUES (...)

EDIT:

Change

var_dump($statement->execute());
$statement->errorInfo();

to

var_dump($statement->errorInfo());

EDIT:

$dsn = 'mysql:dbname=dbname;host=localhost';
$user = 'user';
$password = 'pass';
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (isset($_POST['regsubmit'])) {
    try {
        $sql = "SELECT mail FROM userrecs WHERE mail=:mail";
        $stmt = $dbh->prepare($sql);
        $stmt->bindValue(':mail', $_POST['mail'], PDO::PARAM_STR);
        if ($stmt->execute() && $stmt->rowCount() == 1) {
            session_start();
            $_SESSION['noti_one'] = 'bla';
            header('location:/someplace');
        } else {
            $sql = "INSERT INTO userrecs (ip,name,mail,pass,codeone_one,desc_one,spcstrings) VALUES (:ip,:name,:mail,:encrypted_pass,:codeone,:descs,:newstrings)";
            $stmt = $dbh->prepare($sql);
            $stmt->bindValue(':ip', $_SERVER['REMOTE_ADDR'], PDO::PARAM_STR);
            $stmt->bindValue(':name', $_POST['$name'], PDO::PARAM_STR);
            $stmt->bindValue(':mail', $_POST['mail'], PDO::PARAM_STR);
            $stmt->bindValue(':encrypted_pass', crypt($_POST['passw']), PDO::PARAM_STR);
            $stmt->bindValue(':codeone', $_POST['codeone'], PDO::PARAM_STR);
            $stmt->bindValue(':descs', $_POST['desc'], PDO::PARAM_STR);
            $stmt->bindValue(':newstrings', 'specialstring', PDO::PARAM_STR);

            if ($stmt->execute()) {
                session_start();
                $_SESSION['noti_two'] = 'bla';
                header('location:/someplace');
            } else {
                var_dump($stmt->errorInfo());
            }

        }
    } catch (PDOException $pe) {
        echo "S";
        echo('Connection error, because: ' . $pe->getMessage());
    }
} else {
    header('location:/someplace');
}

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