2013-08-01 26 views
-1

嘿我只是想插入我的数据库中的一些value's和我用这个代码:bind param =>错误,为什么?

define('SECURE', true); 
include "storescripts/connect_to_mysql.php"; 

if (!$mysqli) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

$stmt = mysqli_prepare($mysqli, "INSERT INTO `trans` VALUES (?, ?, ?)"); 
mysqli_stmt_bind_param($stmt, 'sssd', $txn_id, $payer_email, $mc_gross); 

$txn_id = 123456789; 
$payer_email = '[email protected]'; 
$mc_gross = 100; 

/* execute prepared statement */ 
mysqli_stmt_execute($stmt); 

printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt)); 

/* close statement and connection */ 
mysqli_stmt_close($stmt); 

调用这个剧本我拿到这之后:

Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given 

Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean given 

Warning: mysqli_stmt_affected_rows() expects parameter 1 to be mysqli_stmt, boolean given 

0 Row inserted. 

Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, boolean given 

任何人都可以告诉我为什么准备声明做这个错误?谢谢!问候!

+0

虽然这个**错字**非常简单,[你必须学会​​如何从mysqli得到错误信息](http://stackoverflow.com/a/15447204/285587) –

+1

检查'$ stmt,' sssd',$ txn_id,$ payer_email,$ mc_gross)'参数数量! – 2013-08-01 20:12:54

+1

五个参数,错误在哪里? – user2602535

回答

0

mysql_prepare()行失败并返回false,而不是资源。

trans表中有哪些列?如果你没有三列,那么这可能会导致你的准备错误。你应该总是在INSERT语句中定义的列与值一起:

INSERT INTO `trans` (`txn_id`, `payer_email`, `mc_gross`) VALUES (?, ?, ?) 

只要现有数据库中的任何其他列要么允许NULL或默认值,那么你应该能够插入数据。

编辑:

我不确定的设置才是否可以绑定变量。我建议不要将它们绑定到包含值之后。

+0

这是我表:$的SqlCommand =“CREATE TABLE反式( \t \t \t \t ID INT(11)NOT NULL的auto_increment, \t \t \t \t txn_id VARCHAR(255)NOT NULL, \t \t \t \t PAYER_EMAIL VARCHAR(255 )NOT NULL, \t \t \t \t mc_gross INT(255)NOT NULL, \t \t \t \t PRIMARY KEY(ID), \t \t \t \t UNIQUE KEY(txn_id))“; – user2602535

+0

四列,那么? – user2602535

+0

因为你的原始代码没有指定你试图插入数据的4列中的哪3列,所以mysql不知道把它放在哪里。在我的答案中使用插入语句,它应该绕过该错误。我将编辑以更新正确的列名称。 –

相关问题