doumeng06063991 2014-03-28 16:57
浏览 21
已采纳

PHP / MySQLi在非对象上执行()

This question applies specifically to issues in the Openwall tutorial on PHP password hashing. Please read the edit below.

This question has been asked hundreds of times, but none of the answers found my issue.

$app->post("/movies", function() use ($app, $log){
   $db = new mysqli(db_address,db_user,db_pass,db_database,db_port);
   if ($db->connect_errno) {
     $log->emergency("EM[MYSQL] Can't connect to MySQL: " . $db->error);
   }
   ($stmt = $db->prepare("INSERT INTO movies (MovieName, MovieCover, MovieDescription, MovieDirector, MovieDate) VALUES (':title', ':cover', ':description', ':director', ':release_date')") || $log->error('ER[MYSQL] Could not prepare statement: ' . $db->error));
   echo $db->error;
   var_dump($stmt);

   $title = grabPostVar('title');
   $cover = grabPostVar('cover');
   $desc = grabPostVar('desc');
   $director = grabPostVar('director'); 
   $release_date = grabPostVar('release_date');

   if(!($stmt->execute(array(':title' => $title, ':cover' => $cover, ':description' => $desc, ':director' => $director, ':release_date' => $release_date)))) 
   {
     $log->emergency('EM[MYSQL] Misc issue: ' .  $db->errno);
   } else{
     $log->info('I[MOVIE] Movie "' . $title . '" created, by user (u)$current_user(/u).');
   }

   $stmt->close();
   $db->close();
}

On the front of this, it looks exactly the same as the other questions, but var_dump($stmt) retuns true, and according to the documentation

mysqli_prepare() returns a statement object or FALSE if an error occurred.

I used the following lines to check for this:

echo $db->error;
var_dump($stmt);

No error is printed, and var_dump yields bool(true). Any ideas? A statement very like this was working 100% very recently, but with no MySQL error return I'm completely stumped.

Edit:

Yes it was all to do with that stupid OR comparison, thank you for your answers.

if(!($stmt = $db->prepare('INSERT INTO movies (MovieName, MovieCover, MovieDescription, MovieDirector) values (?, ?, ?, ?)'))){
        $log->error('ER[MYSQL] Could not prepare statment: ' . $db->error);
    }

On a sidenote, I suggest that anyone following the Openwall tutorial on password hashing should change to if statements as well.

  • 写回答

1条回答 默认 最新

  • dongpengqin3898 2014-03-28 17:03
    关注

    You have an issue with your parenthesis, which is causing '$stmt' to be evaluated to the result of the logical or represented by || in the line:

    ($stmt = $db->prepare("INSERT INTO movies (MovieName, MovieCover, MovieDescription, MovieDirector, MovieDate) VALUES (':title', ':cover', ':description', ':director', ':release_date')") || $log->error('ER[MYSQL] Could not prepare statement: ' . $db->error));
    

    Changing it to:

    ($stmt = $db->prepare("INSERT INTO movies (MovieName, MovieCover, MovieDescription, MovieDirector, MovieDate) VALUES (':title', ':cover', ':description', ':director', ':release_date')")) || $log->error('ER[MYSQL] Could not prepare statement: ' . $db->error);
    

    Will fix it, though you might consider making your code cleaner.

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

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算