准备好的声明UPDATE

时间:2013-04-08 22:29:01

标签: php mysqli prepared-statement

当我尝试使用mysqli预编译语句更新查询时,我收到以下错误。错误消息有什么问题,为什么会出现意外?任何帮助。

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>';
        }

2 个答案:

答案 0 :(得分: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();

)

bind_param语句本身包含executeif个函数。它必须是:

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>';
}

答案 1 :(得分: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>';
        }
     }
}