【问题标题】:INSERT INTO statement not workingINSERT INTO 语句不起作用
【发布时间】:2015-12-15 17:36:03
【问题描述】:

我对 PHP/SQL 编码相当陌生,我完全不知道为什么这种编码不起作用。数据库本身是由我的大学制作的,所以我只是想连接它(我不确定我是否能够泄露细节,所以我把连接编码去掉了。我处于基础水平,所以繁重的技术语言会让我头疼,但任何建议都将不胜感激!

我正在尝试将链接到 PHP 文件的表单中的结果插入到数据库表中。我不确定是否需要在 PHP 文件中添加任何内容来说明这一点?但这是我的代码:

<?php
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
// Connect to database
$sql = "SELECT runnerid, position, eventid, date, finishtime, categoryid,  agegrade, pb FROM Results";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
    { 
        echo "<table>";
        echo '<table border=1><tr><th>Runner ID</th><th>Position</th><th>Event ID</th><th>Date</th><th>Finish Time</th><th>Category ID</th><th>Age Grade</th><th>Personal Best</th></tr>';
        echo "<tr><td>";
        echo $row['runnerid'];
        echo "</td><td>";
        echo $row['position'];
        echo "</td><td>";
        echo $row['eventid'];
        echo "</td><td>";
        echo $row['date'];
        echo "</td><td>";
        echo $row['finishtime'];
        echo "</td><td>";
        echo $row['categoryid'];
        echo "</td><td>";
        echo $row['agegrade'];
        echo "</td><td>";
        echo $row['pb'];
        echo "</td>
        </tr>";

    }
    echo "</table>";
}
} else {
 echo "0 results";
}

$sql = "INSERT INTO Results VALUES ('$_POST[runnerid]', '$_POST[position]' '$_POST[eventid]' '$_POST[date]' '$_POST[finishtime]' '$_POST[categoryid]' '$_POST[agegrade]' '$_POST[pb]')";

$result = $conn->query($sql);
if (!$result) {
    die('Could not insert data' . mysql_error()); 
}
$conn->close();
?>

我什至尝试了没有$_POST 编码的代码来添加新数据,但这也不起作用。

【问题讨论】:

  • die() 语句是否有错误?
  • 您在查询中的值之间缺少逗号。 runnderid 后面有一个,但其他人都没有。
  • 请参阅以下链接 php.net/manual/en/mysqli.error.phpphp.net/manual/en/function.error-reporting.php 并将其应用于您的代码。您还将 MySQL 函数与mysql_error() 混合使用,它应该读作mysqli_error($conn)。不发布与此相关的 HTML 表单,让我们都在猜测。 检查错误
  • 值得一提的是,您应该使用 PDO 而不是 mysqli;它更便携,更易于使用。

标签: php mysql database mysqli


【解决方案1】:

还没有人提到的是,鉴于您的代码,我可以通过一个请求轻松清除您的整个数据库。以下是您需要采取的措施来防范SQL injection attacks

$sql = "INSERT INTO Results VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param('iiississ', $_POST["runnerid"], $_POST["position"], $_POST["eventid"], $_POST["date"], $_POST["finishtime"], $_POST["categoryid"], $_POST["agegrade"], $_POST["pb"]);
$result = $stmt->execute();
if (!$result) {
    die('Could not insert data: ' . $conn->error); 
}

Read up on prepared statements:

绑定变量与查询分开发送到服务器,因此不会干扰它。在解析语句模板之后,服务器直接在执行点使用这些值。绑定参数不需要转义,因为它们永远不会直接替换到查询字符串中。必须向服务器提供绑定变量类型的提示,以创建适当的转换。

【讨论】:

  • 好点,毕竟我完全错过了UV+1
【解决方案2】:

如果你很好地缩进你的代码,你会更容易看到错误。

  • 你有不匹配的大括号

  • 您的查询语法错误,即参数之间缺少逗号

  • 如果您在变量名周围使用{},则变量将更好地替换为"" 字符串。

  • 您需要将 &lt;table&gt; 移出 while 循环并删除第二个 &lt;table.... 标记

  • 表格标题在循环之外也会更好

  • 你也在使用mysql_error(),但是你实例化了一个mysqli_对象,它不允许混合扩展。请改用$conn-&gt;error

  • 另见@miken32 回答,建议使用绑定变量。

以下是一些建议的修复方法:-

<?php
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
// Connect to database
$sql = "SELECT runnerid, position, eventid, date, 
               finishtime, categoryid,  agegrade, pb FROM Results";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row

    echo '<table border="1">
         <tr><th>Runner ID</th><th>Position</th><th>Event ID</th>
             <th>Date</th><th>Finish Time</th><th>Category ID</th>
             <th>Age Grade</th><th>Personal Best</th>
         </tr>';

    while($row = $result->fetch_assoc()) {
    { 
        echo "<tr><td>";
        echo $row['runnerid'];
        echo "</td><td>";
        echo $row['position'];
        echo "</td><td>";
        echo $row['eventid'];
        echo "</td><td>";
        echo $row['date'];
        echo "</td><td>";
        echo $row['finishtime'];
        echo "</td><td>";
        echo $row['categoryid'];
        echo "</td><td>";
        echo $row['agegrade'];
        echo "</td><td>";
        echo $row['pb'];
        echo "</td>
        </tr>";

    }
    echo "</table>";

// } remove extra barckey

} else {
 echo "0 results";
}

$sql = "INSERT INTO Results 
          VALUES ('{$_POST['runnerid']}', '{$_POST['position']}', 
                  '{$_POST['eventid']}', '{$_POST['date']}', 
                  '{$_POST['finishtime']}' '{$_POST['categoryid']}',
                  '{$_POST['agegrade']}', '{$_POST['pb']}')";

$result = $conn->query($sql);
if (!$result) {
    die('Could not insert data' . $conn->error); 
}
$conn->close();
?>

【讨论】:

    【解决方案3】:
    $sql = "INSERT INTO Results VALUES ('$_POST[runnerid]', '$_POST[position]', '$_POST[eventid]', '$_POST[date]', '$_POST[finishtime]' ,'$_POST[categoryid]', '$_POST[agegrade]', '$_POST[pb]')";   //missing comma after each value
    

    【讨论】:

    • 如果不执行'{$_POST[runnerid]}',,$_POST 变量将不会被替换到该字符串中
    • @RiggsFolly 双引号字符串可以很好地解析一维数组索引。
    • @miken32 不在我使用过的任何 PHP 上,尤其是在数组索引周围没有单引号的情况下,即 $_POST[runnerid] 应该是 $_POST['runnerid']
    • @RiggsFolly See example 8.
    • @miken32 哦,又是美好的一天,我学到了以前完全错过的东西。谢谢
    【解决方案4】:

    兄弟你忘了,在每个值之后。如果您在 sql 查询中直接从 post 中获取任何值,那么它将介于 '".$_post."' 或 "'.$_post.'" 之间,一个条件将适合您将理解的以下查询。

    一切顺利……

    如果有任何问题,请评论。告诉我我会解决的

    它必须如下所示:

     $sql = "INSERT INTO Results VALUES ('".$_POST[runnerid]."', '".$_POST[position]."', '".$_POST[eventid]."', '".$_POST[date]."', '".$_POST[finishtime]."', '".$_POST[categoryid]."', '".$_POST[agegrade]."', '".$_POST[pb]."')";
    

    【讨论】:

    • 如果您尝试运行该代码,您将收到一打关于未定义常量的通知。
    • @miken32 使用多少常量,它会使页面变慢。以及你想使用的任何功能,你都可以在里面使用。
    猜你喜欢
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多