2013-04-08 174 views
0

我收到以下错误,当我尝试更新使用mysqli准备语句的查询。错误消息为何出现意外?任何帮助。准备语句UPDATE

Parse error: syntax error, unexpected '$prob' (T_VARIABLE) 

下面是该查询

$mysqli = new mysqli("localhost", "root", "", "newlogin"); 
     if ($mysqli->connect_errno) { 
     echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; 
     } 
     $check = $mysqli->prepare("select * from `vpb_uploads` where `username` = ? and `firstname` = '' and `image_one` != ''"); 
     $check->bind_param('s', $username); 
     $check->execute(); 
     $check->store_result(); 

     if($check->num_rows < 1) 
     { 
      echo '<div class="vpb_error_info" align="left">Sorry, It seems you have not added any file yet.<br>Please click on the Add Files button to first of all add at least one file before submitting your details. Thanks...</div>'; 
     } 
     else 
     { 
     $firstname = trim(strip_tags($_POST["firstname"])); 
     $lastname = trim(strip_tags($_POST["lastname"])); 

     if($prob = $mysqli->prepare("update `vpb_uploads` set `firstname` = ?, `lastname` = ? where `username` = ? and `firstname` = '' and `image_one` != ''") 
     $prob->bind_param('sss', $firstname, $lastname, $username); 
     $prob->execute(); 
     ) 

     { 
       echo '<font style="font-size:0px;">success</font>'; 
       echo '<div class="vpb_error_info" align="left">Congrats <b>'.$firstname.' '.$lastname.'</b>, your details have been submitted successfully. Thanks...</div>'; 
     } 

回答

0

试试这个代码:

$mysqli = new mysqli("localhost", "root", "", "newlogin"); 
if ($mysqli->connect_errno) { 
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; 
} 
$check = $mysqli->prepare("select * from `vpb_uploads` where `username` = ? and `firstname` = '' and `image_one` != ''"); 
$check->bind_param('s', $username); 
$check->execute(); 
$check->store_result(); 

if($check->num_rows < 1){ 
    echo '<div class="vpb_error_info" align="left">Sorry, It seems you have not added any file yet.<br>Please click on the Add Files button to first of all add at least one file before submitting your details. Thanks...</div>'; 
} else { 
    $firstname = trim(strip_tags($_POST["firstname"])); 
    $lastname = trim(strip_tags($_POST["lastname"])); 

    $prob = $mysqli->prepare("update `vpb_uploads` set `firstname` = ?, `lastname` = ? where `username` = ? and `firstname` = '' and `image_one` != ''") ; 
    if ($prob){ 
     $prob->bind_param('sss', $firstname, $lastname, $username); 
     $success = $prob->execute(); 
     if ($success){ 
      echo '<font style="font-size:0px;">success</font>'; 
      echo '<div class="vpb_error_info" align="left">Congrats <b>'.$firstname.' '.$lastname.'</b>, your details have been submitted successfully. Thanks...</div>'; 
     } 
    } 
} 
+0

我改变它到你写的东西,但我得到解析错误:语法错误,意想不到的'{'这是回声上方 – magnsta 2013-04-08 22:34:39

+0

@magnsta我编辑我的答案,让我知道它是否适合你。 – vikingmaster 2013-04-08 22:39:35

+0

不,我仍然得到相同的错误意外{ – magnsta 2013-04-08 22:47:02

0

乍一看,它看起来像你的错误是第19行:

if($prob = $mysqli->prepare(" 
    UPDATE `vpb_uploads` 
    SET `firstname` = ?, 
     `lastname` = ? 
    WHERE `username` = ? 
    AND `firstname` = '' 
    AND `image_one` != '' 
") 

    $prob->bind_param('sss', $firstname, $lastname, $username); 
    $prob->execute(); 

) 

你有ifbind_paramexecute功能声明本身。它需要:

if($prob = $mysqli->prepare(" 
    UPDATE `vpb_uploads` 
    SET `firstname` = ?, 
     `lastname` = ? 
    WHERE `username` = ? 
    AND `firstname` = '' 
    AND `image_one` != ''; 
") { 
    $prob->bind_param('sss', $firstname, $lastname, $username); 
    $prob->execute(); 
} 

如果你感到困惑与您的支架,您的最终代码应该是这样的:

$mysqli = new mysqli("localhost", "root", "", "newlogin"); 
if ($mysqli->connect_errno) { 
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; 
} 
$check = $mysqli->prepare("select * from `vpb_uploads` where `username` = ? and `firstname` = '' and `image_one` != '';"); 
$check->bind_param('s', $username); 
$check->execute(); 
$check->store_result(); 

if($check->num_rows < 1) { 
    echo '<div class="vpb_error_info" align="left">Sorry, It seems you have not added any file yet.<br>Please click on the Add Files button to first of all add at least one file before submitting your details. Thanks...</div>'; 
} else { 
    $firstname = trim(strip_tags($_POST["firstname"])); 
    $lastname = trim(strip_tags($_POST["lastname"])); 
} 

if($prob = $mysqli->prepare("update `vpb_uploads` set `firstname` = ?, `lastname` = ? where `username` = ? and `firstname` = '' and `image_one` != '';")) { 
    $prob->bind_param('sss', $firstname, $lastname, $username); 
    $prob->execute(); 
    echo '<font style="font-size:0px;">success</font>'; 
    echo '<div class="vpb_error_info" align="left">Congrats <b>'.$firstname.' '.$lastname.'</b>, your details have been submitted successfully. Thanks...</div>'; 
} 
+0

我得到解析错误:语法错误,意外'{' – magnsta 2013-04-08 22:35:56

+0

看起来好像你还没有关闭'} else {'在第15行上开始的另一个大括号。我已经将它包含在我的答案中。 – 2013-04-08 22:37:03