doukan6564 2015-06-23 13:16
浏览 37
已采纳

准备好的语句没有运行MYSQLI PHP

I have been attempting to switch over to prepared statements, however I cant figure out why my new code no longer functions. I am new to using these and still learning but i understand it is the best practice for security. any help would be appreciated. Thank You.

<?php
$servername = "11.11.11.11";
$username = "root";
$password = "root";
$dbname = "sit";

$conn = new mysqli($servername, $username, $password,$dbname);


if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$result = mysqli_query($conn, "SELECT * FROM `ourstory` ");
$values = mysqli_fetch_array($result);


if(isset($_POST['ourstory_title'])){
$ourstory_title = $_POST['ourstory_title'];
$ourstory_testimonial = $_POST['ourstory_testimonial'];
$ourstory_content = $_POST['ourstory_content'];
$ourstory->execute();

$ourstory = $conn->prepare("UPDATE ourstory SET
    ourstory_title='$ourstory_title' ,
    ourstory_content='$ourstory_content' ,
    ourstory_testimonial='$ourstory_testimonial' 
    WHERE  ourstory_id='1'");
$ourstory->bind_param("sss", $ourstory_title, $ourstory_content, $ourstory_testimonial);   




if (mysqli_query($conn, $ourstory)) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . mysqli_error($conn);
}   
$ourstory->close();
$conn->close();

}

?>
<form id="comment_form" method="post" 
      action="<?php echo $ourstory?>" 
      onsubmit="setTimeout(function () { 
             window.location.reload(); 
      }, 10), location.reload(true);">

<table width="100%" border="0" cellspacing="1" cellpadding="2">


<tr>
<td width="85%">About Us Title</td>
</tr>
<tr>
<td>
   <input class="commentarea" 
          name="ourstory_title" type="text" 
          id="ourstory_title" value="<?php echo $values['ourstory_title']?>">
</td>
</tr>
<tr>
<td width="85%" >Testimonial</td>
</tr>
<tr>
<td>
   <pre>
     <textarea class="commentarea" 
      name="ourstory_testimonial" type="text" 
      id="ourstory_testimonial" rows= "10" ><?php echo $values['ourstory_testimonial']?>
     </textarea>
   </pre>
</td>
</tr>
<tr>
<td width="85%" >About Us Content</td>
</tr>
<tr>
<td>
  <pre>
    <textarea class="commentarea" name="ourstory_content" 
        type="text" id="ourstory_content"  
         rows= "10" ><?php echo $values['ourstory_content']?>
    </textarea>
  </pre>
 </td>
</tr>


<tr>

<td>

<input type="submit" value="Update">
</td>
</tr>
</table>
</form>
  • 写回答

2条回答 默认 最新

  • douchangmian0305 2015-06-23 13:58
    关注

    In conjunction with Mark's answer, am submitting the following as a complimentary answer and using some of my comments left under the OP's question.

    Firstly, <textarea> does not have a type. type="text" remove all of those.

    Then, $ourstory->execute(); is misplaced, it needs to go after $ourstory->bind_param("sss",... once you've used Mark's answer and using placeholders as stated in the answer and from the manual http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php

    You shouldn't have if (mysqli_query($conn, $ourstory)) { what you're looking to use is affected_rows http://php.net/manual/en/mysqli.affected-rows.php in a conditional statement to check if the query was indeed successful.


    From your edit: https://stackoverflow.com/revisions/31003865/4

    printf("Affected rows (UPDATE): %d
    ", $ourstory->affected_rows);
    $ourstory->execute();
    

    this needs to go after executing:

    $ourstory->execute();
    printf("Affected rows (UPDATE): %d
    ", $ourstory->affected_rows);
    

    but I would use a conditional if for that and it should be the connection's variable, i.e. and from the manual:

    int $mysqli->affected_rows;
    

    so do:

    printf("Affected rows (UPDATE): %d
    ", $conn->affected_rows);
    

    Example from the manual:

    <?php
    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
    
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s
    ", mysqli_connect_error());
        exit();
    }
    
    /* Insert rows */
    $mysqli->query("CREATE TABLE Language SELECT * from CountryLanguage");
    printf("Affected rows (INSERT): %d
    ", $mysqli->affected_rows);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化