douxihui8270 2018-04-09 02: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-09 02: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.

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

报告相同问题?

悬赏问题

  • ¥15 pip install后修改模块路径,import失败,需要在哪里修改环境变量?
  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题