bind_param()中变量的问题

时间:2012-10-20 17:26:40

标签: php mysql mysqli

我一直有error_reporting并且我多次查看它,所以我想我可能会错误地调用值或者没有声明正确的内容。任何人都可以跳进去告诉我最新的代码,以及为什么还有错误“调用非对象上的成员函数bind_param()”,因为我很难过:

$db=new mySQLi($host,$dbhuser,$dbhpwd,$dbh); 
$insert_stmt = $db->stmt_init();    
$insert_stmt = $db->prepare("INSERT INTO food (id,timestamp,userid,tagline,group,location,event_date,event_time,image,type) VALUES(NULL,NOW(),?,?,?,?,?,?,?,?)");
        $insert_stmt->bind_param("issssssi",$_SESSION['id'],$tagline,$organizer,$location,$event_date,$event_time,$image_name,$food_id);
        $insert_stmt->execute();

userid是一个整数,我从存储的cookie中提取。

$ tagline,$ organizer,$ location,是最多140个字符的文本。

$ event_date = $ event_date = substr(date('Y'),0,2)。$ year。“ - ”。$ month。“ - ”。$ day; $ event_time = date(“H:i:s”,strtotime($ hour。“:”。$ minute。$ period)); - 所有这些变量都是用户输入两位数字作为文本

$ image_name是文本,文件路径

和type是1-29之间的整数。

以下是一个示例结果?数据库的值:38,测试,测试,测试,2013-2-20,14:15:00,Halloween_Beer_2.jpg,7

这是一个名为food的表格在mysql中设置的屏幕截图: enter image description here

1 个答案:

答案 0 :(得分:-2)

$insert_stmt = $db->prepare("INSERT INTO food (id,timestamp,userid,tagline,group,location,event_date,event_time,image,type) VALUES(NULL,NOW(),?,?,?,?,?,?,?,?)");
        $insert_stmt->bind_param("issssssi",$_SESSION['id'],$tagline,$organizer,$location,$event_date,$event_time,$image_name,$food_id);
        $insert_stmt->execute();

你做错了。BindParam只接受一个值

$query->bindParam("value",":value",PDO::PARAM_INT);// type

你可以做这样的事情

   $insert_stmt = $db->prepare("INSERT INTO food (id,timestamp,userid,tagline,group,location,event_date,event_time,image,type) VALUES(NULL,NOW(),?,?,?,?,?,?,?,?)");

$insert_stmt->execute(array("issssssi",$_SESSION['id'],$tagline,$organizer,$location,$event_date,$event_time,$image_name,$food_id))

或者你可以看到这个        PDO tutorial