2013-03-29 217 views
-1

我想将下面的mysql更改为mysqli(准备好的语句),但我陷入了困境。准备语句

问题:Warning: mysqli_stmt ::bind_result(): Number of bind variables doesn't match number of fields in prepared statement在发生此错误$ get_empty_field = $ OK-> bind_result($用户名,$姓名,$姓氏);线。我不与转换做的,我只是做了我所能做离开了休息,因为它是。(我不是混合mysql和mysqli的)

MySQL查询

$check_added_files = mysql_query("select * from `vpb_uploads` where `username` = '".mysql_real_escape_string($username)."' and `firstname` = '' and `image_one` != '' and `image_two` != '' and `image_three` != '' and `image_four` != '' and `image_five` != ''"); 
        if(mysql_num_rows($check_added_files) == 1) 
        { 
        echo 'up_to_five_already'; 
        } 
        else 
        { 
        if (move_uploaded_file($_FILES['file_to_upload']['tmp_name'], $final_uploads_location)) 
        { 
        $check_empty_field = mysql_query("select * from `vpb_uploads` where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'"); 
        if(mysql_num_rows($check_empty_field) < 1) 
        { 
        mysql_query("insert into `vpb_uploads` values('', '".mysql_real_escape_string($username)."', '', '', '".mysql_real_escape_string($random_name_generated)."', '', '', '', '', '".mysql_real_escape_string(date("d-m-Y"))."')"); 

        $identity = "image_one"; 
        } 
        else 
        { 
        $get_empty_field = mysql_fetch_array($check_empty_field); 
        $image_one = strip_tags($get_empty_field["image_one"]); 
        $image_two = strip_tags($get_empty_field["image_two"]); 
        $image_three = strip_tags($get_empty_field["image_three"]); 
        $image_four = strip_tags($get_empty_field["image_four"]); 
        $image_five = strip_tags($get_empty_field["image_five"]); 
        global $identity; 

        if(empty($image_one)) 
        { 
         mysql_query("update `vpb_uploads` set `image_one` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'"); 

         $identity = "image_one"; 

        } 
        elseif(empty($image_two)) 
        { 
         mysql_query("update `vpb_uploads` set `image_two` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'"); 

         $identity = "image_two"; 
        } 
        elseif(empty($image_three)) 
        { 
         mysql_query("update `vpb_uploads` set `image_three` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'"); 

         $identity = "image_three"; 
        } 
        elseif(empty($image_four)) 
        { 
         mysql_query("update `vpb_uploads` set `image_four` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'"); 

         $identity = "image_four"; 
        } 
        elseif(empty($image_five)) 
        { 
         mysql_query("update `vpb_uploads` set `image_five` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'"); 

         $identity = "image_five"; 
        } 

我有什么到目前为止:

$mysqli = new mysqli("localhost", "root", "", "newlogin"); 
       if ($mysqli->connect_errno) { 
       echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; 
       } 


       $stmt = $mysqli->prepare("select * from `vpb_uploads` where `username` = ? and `firstname` = '' and `image_one` != '' and `image_two` != '' and `image_three` != '' and `image_four` != '' and `image_five` != ''"); 
       $stmt->bind_param('s', $username); 
       $stmt->execute(); 
       $stmt->store_result(); 

       if ($stmt->num_rows == 1) { 

       echo 'up_to_five_already'; 
       } 


       else 
       { 
       if (move_uploaded_file($_FILES['file_to_upload']['tmp_name'], $final_uploads_location)) 
       { 
       $firstname = "''"; 
       $lastname = "''"; 
       $stmt = $mysqli->prepare("select * from `vpb_uploads` where `username` = ? and `firstname` = ? and `lastname` = ?"); 
       $stmt->bind_param('sss', $username, $firstname, $lastname); 
       $stmt->execute(); 
       $stmt->store_result(); 
       if ($stmt->num_rows < 1) 

       { 
       $date = 'date("d-m-Y")'; 
       $image_2 = "''"; 
       $image_3 = "''"; 
       $image_4 = "''"; 
       $image_5 = "''"; 
       $stmt = $mysqli->prepare("insert into `vpb_uploads` (`username`, `firstname`, `lastname`, `image_one`, `image_two`, `image_three`, `image_four`, `image_five`, `date`) values(?,?,?,?,?,?,?,?,?)"); 
       $stmt->bind_param('sssssssss', $username, $firstname, $lastname, $random_name_generated, $image_2, $image_3, $image_4, $image_5, $date); 
       $stmt->execute(); 


       $identity = "image_one"; 
       } 
       else 
       { 

       $get_empty_field = $stmt->bind_result($username, $firstname, $lastname); 
       $image_one = strip_tags($get_empty_field["image_one"]); 
       $image_two = strip_tags($get_empty_field["image_two"]); 
       $image_three = strip_tags($get_empty_field["image_three"]); 
       $image_four = strip_tags($get_empty_field["image_four"]); 
       $image_five = strip_tags($get_empty_field["image_five"]); 
       global $identity; 

       if(empty($image_one)) 
        { 
       $stmt = $mysqli->prepare("update `vpb_uploads` set `image_one` = ? where `username` = ? and `firstname` = ? and `lastname` = ? "); 
       $stmt->bind_param('ssss', $random_name_generated, $username, $firstname, $lastname);  
       $stmt->execute(); 
       $stmt->close(); 

       $identity = "image_one"; 

       } 
       elseif(empty($image_two)) 
       { 
       mysql_query("update `vpb_uploads` set `image_two` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'"); 

       $identity = "image_two"; 
       } 
       elseif(empty($image_three)) 
       { 
       mysql_query("update `vpb_uploads` set `image_three` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'"); 

       $identity = "image_three"; 
       } 
       elseif(empty($image_four)) 
       { 
       mysql_query("update `vpb_uploads` set `image_four` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'"); 

       $identity = "image_four"; 
       } 
       elseif(empty($image_five)) 
       { 
       mysql_query("update `vpb_uploads` set `image_five` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'"); 

       $identity = "image_five"; 
       } 
+0

你能告诉我们错误发生的确切位置吗? – Shoe

+0

我还没有完成...我已经说过,在这个问题.. – magnsta

+0

'$ ok'从哪里来? – Shoe

回答

0

你需要改变你的SELECT声明:

$stmt = $mysqli->prepare("select `username`,`firstname`,`lastname` from `vpb_uploads` where `username` = ? and `firstname` = ? and `lastname` = ?"); 

而且你将需要修复的变量名:

$get_empty_field = $stmt->bind_result($username, $firstname, $lastname); 
+0

感谢,似乎解决了错误。现在不得不处理剩下的事情。谢谢! – magnsta

1

As I told you before,你必须改变以PDO,不mysqli的。 当然,只是复制代码不会有好处。您至少需要先连接到PDO。有一个链接到我提供的PDO标签维基,有完整的连接代码进行复制。另外,除了复制代码之外,您还需要付出一些努力才能使其工作 - 至少通过调整数据库设置。

一般来说,您在这里得到的代码并不打算开箱即用。他们会给你一个想法,让你学习。这是非常重要的事情 - 你需要从答案中学习,以便能够自己做下一个类似的工作。如果您只是复制并粘贴代码,那么您会要求Stack Overflow重写所有的应用程序。请从答案中学习,并尝试运用你得到的知识。

+0

如果我知道如何去做,我会将其更改为PDO。我做了谷歌搜索,我有点困惑。如果我再次陷入困境,我可能会进一步尝试PDO。不管怎么说,还是要谢谢你! – magnsta

+0

在未来几年这将是一场艰苦的战斗。 –

+0

@joeframbach没有什么可争取的。只需点击链接并比较我提供的几行代码,以及OP为之努力奋斗的几个代码屏幕。 –