【问题标题】:HTML form not inserting into MySQL DBHTML 表单未插入 MySQL 数据库
【发布时间】:2014-06-21 17:53:50
【问题描述】:

仍在一个小型网站上工作,并且我在使用 php 插入语句时遇到问题,以前在其他形式中,我成功地将数据插入到我的数据库中,但是对于某些形式,它接受了问题并说查询很好,但是在检查数据库时它的根本没有输入。

HTML 表单

<form>
    <form action="comment.php" method="post">
Your Client Number <input type="text" name="clientNo" /><br>
The Boat's Number <input type="text" name="boatNo" /><br>
The View Date <input type="text" name="viewDate" /><br>
Comments <br><textarea name="comment"></textarea><br>   
<input type="submit" value="submit comments" />
</form>

PHP 代码

<?php
$client = $_POST['clientNo'];
$boat = $_POST['boatNo'];
$date = $_POST['viewDate'];
$comment = $_POST['comment'];

    $con = mysqli_connect("localhost","root","","boats4u")or die(mysqli_error());;
    $res = mysqli_query($con,"INSERT INTO 'boatviewing'(clientNo,boatNo,viewDate,comment)
    VALUES ('$client','$boat','$date','$comment')");


if($res)
{
    echo "success";
    header("Location:client.php");
}
else {
    echo "no".mysqli_error();
}

?>

感觉好像我错过了一些东西,当我从上一页提交表单时,它会运行它,你可以看到它是否成功然后返回到它所做但仍然没有出现在数据库中的上一页

感谢

【问题讨论】:

  • 为什么要打开两个表单标签
    ?

标签: php mysql


【解决方案1】:

编辑:(答案如下)

特别说明:我注意到您没有接受对您的任何问题提供的任何答案,但确实找到了解决方案。

StackOverflow 系统的工作方式是,一旦对问题给出了答案,您就可以勾选白色复选标记直到它变为绿色。

请查看以下有关“接受答案的工作原理”的文章。

StackOverflow 建立在积分/声誉系统之上,旨在鼓励每个人继续取得成功。

阅读“关于”部分:


回答

error reporting 添加到您的文件中会表示错误。

error_reporting(E_ALL);
ini_set('display_errors', 1);

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

如果表名是 MySQL 保留字,则不要在表名周围使用引号或使用反引号;在这种情况下,不只是删除表名周围的引号。

$res = mysqli_query($con,"INSERT INTO boatviewing (clientNo,boatNo,viewDate,comment)

(如果你真的想转义你的表名)

$res = mysqli_query($con,"INSERT INTO `boatviewing` (clientNo,boatNo,viewDate,comment)

你似乎还有一个额外的&lt;form&gt; 标签;删除它,并删除 die(mysqli_error());; 末尾多余的分号。

另外,去掉header("Location:client.php");上面的echo "success";,你在header之前输出,会报错。

改变

$con = mysqli_connect("localhost","root","","boats4u")or die(mysqli_error());;

到:

$con = mysqli_connect("localhost","root","","boats4u") 
or die("Error " . mysqli_error($con));

按照说明书上的方法:

旁注:您当前的代码对SQL injection 开放。使用prepared statements,或PDO


这是一个准备好的语句方法:

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

    $mysqli = new mysqli("localhost","root","","boats4u");

    /* check connection */
    if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
    }
    
    
    /* Set the params */
    
    $client = $_POST['clientNo'];
    $boat = $_POST['boatNo'];
    $date = $_POST['viewDate'];
    $comment = $_POST['comment'];

    /* Create the prepared statement */
    if ($stmt = $mysqli->prepare("INSERT INTO boatviewing (clientNo,boatNo,viewDate,comment) VALUES (?, ?, ?, ?)")) {

    /* Bind the params */

    $stmt->bind_param('ssss', $client, $boat, $date, $comment);

    /* Execute the prepared statement */
    $stmt->execute();

    /* Echo results */
    echo "Inserted {$client}, {$boat}, {$date}, {$comment} into database.\n";

    /* Close the statement */
    $stmt->close();
    }
    else {
    /* Error */
    printf("Prepared Statement Error: %s\n", $mysqli->error);
    }

?>

如果您不想使用准备好的语句(您确实应该这样做)并且处于学习状态,至少使用mysqli_real_escape_string() 进行一些保护

$client = mysqli_real_escape_string($con,$_POST['clientNo']);
$boat = mysqli_real_escape_string($con,$_POST['boatNo']);
$date = mysqli_real_escape_string($con,$_POST['viewDate']);
$comment = mysqli_real_escape_string($con,$_POST['comment']);

【讨论】:

    【解决方案2】:

    您必须检查输入中是否有任何逗号或引号,您必须将其转义,因为查询会返回错误。

    【讨论】:

      【解决方案3】:

      查看tableboatviewing前后插入查询中的单引号,这就是问题所在。

      【讨论】:

      • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方发表评论 - 您可以随时评论自己的帖子,一旦您有足够的reputation,您就可以comment on any post
      • @fluffeh,非常感谢。在发帖时,我没有足够的特权,为什么我在这里回答。谢谢你
      【解决方案4】:

      另外,请确保您的连接正常,阅读http://docs.php.net/manual/pl/mysqli.construct.php

      mysqli_connect("localhost","root","","boats4u")or die(mysqli_error());;
      

      无论如何都会返回一个对象,所以

      or die(mysqli_error());
      

      不会执行,因为 connect 返回并且对象设置为 ->connect_errno

      【讨论】:

        【解决方案5】:
        $res = mysqli_query($con,"INSERT INTO `boatviewing`(`clientNo`,`boatNo`,`viewDate`,`comment`)
            VALUES ($client,$boat,$date,$comment)");
        

        试试这个。

        【讨论】:

          猜你喜欢
          • 2019-01-12
          • 2014-04-04
          • 2017-10-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-04
          • 1970-01-01
          相关资源
          最近更新 更多