php - HTML 表单未插入 MySQL 数据库

标签 php mysql

仍在一个小网站上工作,我在使用 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();
}

?>

感觉我错过了什么,当我提交上一页的表单时,它会运行这个,你可以看到它是否成功然后返回到上一页,它确实返回了但仍然没有出现在数据库中

谢谢

最佳答案

编辑:(答案如下)

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

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)

你似乎还有一个额外的<form>标签;删除它,并删除 die(mysqli_error());; 末尾多余的分号不需要。

另一件事,删除 echo "success";在你的 header("Location:client.php"); 之上,您在 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']);

关于php - HTML 表单未插入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23472318/

相关文章:

php - Zend框架/表格: custom elements not shown on error

php - 通过单击链接或从复选框下拉列表中选择 "select-all",JQuery/Javascript 全选

mysql - 我可以将 .mysql 文件更改为 .sql 还是比这更复杂?

php - mysql如何获取表的外键

php - CakePHP 3 保存 HasOne 关联

php - 在为移动网站提供之前更改图像宽度

php - wordpress 中的背景音乐,带有 ON 和 OFF 手册

php - UPDATE 函数,用户 ID 未知

c# - 来自 MYSQL Membership Provider 的自定义成员资格提供程序

php - 如何在 codeigniter 中使用开始时间和结束时间检查行程开始日期和结束日期