简体   繁体   中英

PHP Mysqli no errors, no querys

I'm trying to use mysqli instead of mysql queries, and it's not working. Mysqli:

$mysqli->connect($db1['host'], $db1['user'], $db1['password'], $db1['database']);
if ($mysqli->connect_errno) {
        printf("Connect failed: %s\n", $mysqli->connect_error);
        exit();
    }

no errors. If I try this query:

if(isset($_POST['username']))
{
    $password = $_POST['p']; 

    $random_salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true));

    $password = hash('sha512', $password.$random_salt);

    if ($insert_stmt = $mysqli->prepare("INSERT INTO members (username, email, password, salt) VALUES (?, ?, ?, ?)")) {    
       $insert_stmt->bind_param('ssss', $username, $email, $password, $random_salt); 

       $insert_stmt->execute();
    }
    echo "Success";
}

nothing is inserted, no errors with mysqli error. Table structure is correct, and it says success. I'm new to mysqli, I'm used to mysql. Is there something I've missed with error reporting?

Better try this, its from php manual

<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) 
{
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli- >connect_error;
}

you have to do it like this way

$password = hash('sha512', $password.$random_salt);

$insert_stmt = $mysqli->prepare("INSERT INTO members (username, email, password, salt) VALUES (?, ?, ?, ?)");     
$insert_stmt->bind_param('ssss', $username, $email, $password, $random_salt); 

if($insert_stmt->execute())
{
    echo "Success";
}

Actually you are first checking the query and after that binding the params, because of that it was just displaying Success .

You could do the $stmt->execute(); in an if loop like this:

if ($stmt->execute()){

    $result = $stmt->affected_rows;

    if ($result) { echo "yay" } else { echo "boo"; }
}
else {
    printf("Execute error: %s", $stmt->error);
}

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