dongraa1986 2013-11-26 14:33
浏览 152
已采纳

MySQLi insert into语句插入数据但也显示错误

Going out of my mind on this one. Here's the situation and what I've tried so far:

I've got a simple form that collects an email and a password which are then supposed to be inserted into a database table with three columns. The table is called users and the three columns are called userID, userEmail, userPassword. Here is my starting code:

// hash the password value
$pass = hash_hmac('sha256', $p, 'c#hArl891', true);

// Query the database:
$q = "INSERT INTO users (userEmail, userPassword) VALUES ('".$e."','".$pass."')";       
$r = mysqli_query ($dbc, $q);

if (mysqli_num_rows($r) == 1) { // Successful insertion.

What happened with this code is that the email (variable $e) showed up correctly in the database and the password was a garbled mess (a side issue) but the mysqli_num_rows($r) returned this error:

mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given

I've tried replacing the hashed value with just $p, I've tried replacing both variables with constants, I've tried using back-ticks around the table name and field names, and I've tried a suggestion I found online to use braces:

$q = "INSERT INTO users (`userEmail`,`userPassword`) VALUES ('{$e}','{$pass}')";

Nothing seems to work to get mysqli_num_rows($r) == 1.

According to the returned error message, I'm seeing variable values of:

[e] => webmaster@brukenet.com
[p] => 12345
[pass] => Ç o’,YÊ2ý€|y:fز"?ïÑ`{a]YÑ=Âk¼
[q] => INSERT INTO `users` (`userEmail`, `userPassword`) VALUES ('webmaster@brukenet.com', 'Ç o’,YÊ2ý€|y:fز"?ïÑ`{a]YÑ=Âk¼') 
[r] => 1

and

[dbc] => mysqli Object

These would seem to indicate that $r DOES equal 1 but this is taken directly from the same error message.

I've been up all night and I'm thinking this must be something obvious that I'm just over-looking but I just can't see it.

  • 写回答

1条回答 默认 最新

  • dqzlqfqeh845799833 2013-11-26 14:38
    关注

    Insert queries don't return a result set and you can only use mysqli_num_rows() on a result set.

    Try doing

    if (mysqli_affected_rows($dbc) > 0) {
    

    As for your hashing issue, are you sure you want to store the raw binary output? Change the true to false in your hash_hmac() function or use bin2hex() on your raw binary data to get some readable data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题