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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。