2012-09-28 39 views
2

我一直在将我的网站从不受保护的mysql查询改写为mysqli预准备语句,并且一切进行得很顺利,直到得到此结果为准备语句中的参数没有提供数据。没有提供准备好的语句中的参数的数据MySQLi PHP

我已经研究过它无济于事,我正在请求这个社区的盲目迷人来帮助我。

if(empty($err)) { 
    $pSETQuery = NULL; 
    if(!empty($_POST['password'])) { 
     $pSETQuery .= ", password = ?"; 
    } 
    if($session->isSuperuser()) { 
     $pSETQuery .= ", usertype = ?"; 
    } 
    if(!($stmt = $database->prepare("UPDATE user SET username = ?, email = ? $pSETQuery WHERE UserId = ?"))) { 
     $err[] = "PREPARE FAILED."; 
    } 
    $stmt->bind_param("s", $_POST['username']); 
    $stmt->bind_param("s", $_POST['email']); 
    if(!empty($_POST['password'])) { 
     $stmt->bind_param("s", $_POST['password']); 
    } 
    if($session->isSuperuser()) { 
     $stmt->bind_param("s", $_POST['usertype']); 
    } 
    $stmt->bind_param("i", $_POST['userid']); 
    if(!$stmt->execute()){ 
     $err[] = "Execute failed. ERROR: " . $stmt->error; 
    } 

} 

问候, 哈利

+0

任何帮助,将不胜感激,需要一个快速的答案,将接受它,如果它是值得的。 – PwnageAtPwn

回答

0

好像您可能想要在继续​​前更新其配置文件之前验证所有这些字段。

if(empty($err)) { 

    //Check for UserId, otherwise you can't update a profile 
    if (empty($_POST['userid'])) { 
     //Handle error here... 
     exit; 
    } 

    $userid = $_POST['userid']; 

    //Filter out all NULL values and replace with empty strings (safety first!) 
    $username = empty($_POST['username']) ? '' : $_POST['username']; 
    $password = empty($_POST['password']) ? '' : $_POST['password']; 
    $email = empty($_POST['email']) ? '' : $_POST['email']; 
    $usertype = empty($_POST['usertype']) ? '' : $_POST['usertype']; 

    $pSETQuery = ''; 
    $pSETQuery .= !empty($username) ? ", username = ?" : ""; 
    $pSETQuery .= !empty($password) ? ", password = ?" : ""; 
    $pSETQuery .= (!empty($usertype) && $session->isSuperuser()) ? ", usertype = ?" : ""; 
    //This line stops someone from being able to enter a blank username 

    //Prepare statement 
    if(!($stmt = $database->prepare("UPDATE user SET email = ? $pSETQuery WHERE UserId = ?"))) { 
     $err[] = "PREPARE FAILED."; 
    } 

    //Bind parameters where appropriate 
    $stmt->bind_param("s", $email); 
    if(!empty($username)) $stmt->bind_param("s", $username); 
    if(!empty($password)) $stmt->bind_param("s", $password); 
    if($session->isSuperuser() && !empty($usertype)) $stmt->bind_param("s", $usertype); 
    $stmt->bind_param("i", $userid); 

    //Execute statement 
    if(!$stmt->execute()){ 
     $err[] = "Execute failed. ERROR: " . $stmt->error; 
    } 
} 
0

“没有在事先准备好的声明参数提供的数据”意味着语句是好的,但你要提供的bind_param瓦尔的至少一个是预期不存在!我会打印出$ _POST,看看发生了什么,最终设置$ pSETQuery ='';而不是空!

$_POST['username'] 
$_POST['email'] 
$_POST['password'] 
$_POST['usertype'] 
$_POST['userid'] // this one is the one i would really watch after, how do you tell the userid if the user is not logged (i assume that from email, passwrod and might be wrong) 

欢呼

2

你使用Zend框架? 这可能是Php和Zend之间的版本问题。 我得到了PHP 5.3 +的问题,他们在插入Zend Framework 1.8.3时出现同样的错误。

如果您在这种情况下,其中一种解决方案是将连接器更改为数据库。试试这个,它为我工作:

$db = new Zend_Db_Adapter_Pdo_Mysql(array(
    'host'  => '127.0.0.1', 
    'username' => 'webuser', 
    'password' => 'xxxxxxxx', 
    'dbname' => 'test' 
)); 
0

我找到解决同样的问题的方式。

这是一个值过去的MySQL,它是NULL。鉴于此列不能在表格定义中为NULL ...

相关问题