douxihui8270 2018-04-08 18:36
浏览 77
已采纳

Php / MySQL:无法仅插入一行

I am trying to make a page that allows the user to enter the steps he will take to do a particular task. However, whenever I submit a step, multiple similar records get stored inside mysql database. So if i type "Do this", in my mysql database, two records will be stored with same data Here is my code...

<?php

$conn = new mysqli("localhost", "root","","KFC");
if(isset($_POST['Submit_btn']))
{
  session_start();
    $var_value = $_SESSION['R_ID'];

    $Name= mysqli_real_escape_string($conn, $_POST['Step']);

    $sql= "INSERT INTO `step`(`Step`, `R_ID`) VALUES ('$Name','$var_value')";

    mysqli_query($conn,$sql);
    if (!mysqli_query($conn,$sql))
  {
  echo("Error description: " . mysqli_error($conn));
  }
}
mysqli_close($conn);
?>
<html>
<body>
<div id="bodyContent">
<h1> Submit Steps </h1>
</div>
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('garden');
$result = mysql_query("SELECT * FROM step WHERE R_ID='$var_value'");
$rows = mysql_num_rows($result);
echo "So far you have submitted " . $rows . " steps for your recipe.";
?>
<form method="post">
<table>
<tr>
<td>STEP: </td>
<td>
<textarea type="text" name="Step" class="textInput"></textarea>
</td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" name="Submit_btn" value="submit">
</td>
<td>
<input type="submit" name="Continue_btn" value="Continue">
</td>
</tr>

</table>
</form>
</body>
</html>

展开全部

  • 写回答

1条回答 默认 最新

  • dongpu3898 2018-04-08 18:45
    关注

    You are running the execution query twice .

    mysqli_query($conn,$sql);
    

    This instruction execute the insert query that you have. You are executing it once, and then you are executing again to validate the boolean return value of the function. You need to use only the validation row.

    This is what you have:

      mysqli_query($conn,$sql);
      if (!mysqli_query($conn,$sql))
      {
        echo("Error description: " . mysqli_error($conn));
      }
    

    You only need:

      if (!mysqli_query($conn,$sql))
      {
        echo("Error description: " . mysqli_error($conn));
      }
    

    The validation row will execute the function and return the boolean value you need.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部