【问题标题】:I cannot insert records into Database using HTML Form and PHP我无法使用 HTML 表单和 PHP 将记录插入数据库
【发布时间】:2014-09-26 21:45:14
【问题描述】:

当我运行这些脚本时,我的 MyPHP 数据库中没有显示任何记录。

我正在跑步: 阿帕奇 2.4.7 MYSQL 5.6.15 PHP 5.5.8

首先是 HTML 代码...

<html>

<center>
  <font face="Helvetica">

<u><b>Matthew Gieger's Guestbook</b></u>

<form action="link.php" method="post"/>
<p>Name: </p>
<input type="text" name="Name" required/>
<p>Email: </p>
<input type="email" name="Email" required />
<p>Message: </p>
<p><textarea rows="4" cols="50" name="Message"> </textarea></p>
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</form>
</center>
</html>

还有 PHP 脚本。这就是我认为问题所在......

 <?php

$username='root';
$password='';
$database='guestbook';

$name= $_POST['Name'];
$email= $_POST['Email'];
$message= $_POST['Message'];

new mysqli('localhost',$username,$password,$database) or die("could not connect to localhost");

echo"connected";

mysqli:"insert into contacts (Name,Email,Message,Timestamp) values ($name,$email,$message,date())";

?>

运行代码时没有错误。我只是得到预期 “连接”

【问题讨论】:

  • 尝试通过if条件调试。

标签: php html mysql apache easyphp


【解决方案1】:

您没有在任何地方定义mysqli 对象。而且你没有在任何地方使用mysqli_query()

Here are the docs to mysqli.

试试这样的:

$mysqli = new mysqli('localhost', $username, $password, $database);

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$mysqli->query("INSERT INTO `contacts`(`Name`, `Email`, `Message`, `Timestamp`) VALUES ('". $name ."', '". $email ."', '". $message ."', '". $timestamp ."')");

【讨论】:

  • 做到了!太感谢了!抱歉,我不能投票……我没有足够的声誉。
  • @MattG 也看看 Schlaus 的回答,以防止出现安全问题(sql 注入)。
  • 我在向数据库添加第二条记录时仍然遇到问题。似乎它不允许超过 1 条记录。有什么想法吗?
  • @MattG 好吧,例如,您设置了id 吗?可能是您一直尝试使用id = 1 插入一条记录,但由于该记录现在已经存在,您需要将下一条记录设为id 2。您可以使用MySQL 自动为您执行此操作字段上的AI - Auto Increment 选项。如果没有任何错误或更多细节,我真的无法做出任何其他猜测;)
  • 哦,好吧,大概就是这样。几天前设置字段时,我找不到自动递增的选项。我会看一下文档,我相信这会解决它!再次感谢
【解决方案2】:

您没有正确实例化 mysqli 类。您需要将对象实例保存到变量中:

$mysqli = new mysqli('localhost',$username,$password,$database);

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

您的代码中还有一个严重的安全问题,因为您没有转义输入变量。最好的解决方案是查看 PDO:http://php.net/manual/en/pdo.construct.php 和参数绑定。

如果你真的想用 mysqli 做到这一点,你应该首先在你的所有输入变量上使用mysqli_real_escape_string

$name = mysqli_real_escape_string($mysqli, $_POST['Name']);
$email = mysqli_real_escape_string($mysqli, $_POST['Email']);
$message = mysqli_real_escape_string($mysqli, $_POST['Message']);

然后正确运行您的查询:

$mysqli->query("insert into contacts (Name,Email,Message,Timestamp) values ('$name','$email','$message',".date().")";

如果您不逃避用户输入,那么使用简单的SQL injection 破解您的数据库将非常容易。

【讨论】:

  • +1 注入漏洞,我自己忘了提。
  • 感谢您的提醒!这只是一个课堂项目,但这绝对是个好消息!
【解决方案3】:

很可能是格式问题,试试这样:

<?php 
$username='root';
$password='';
$database='guestbook';

$name= $_POST['Name'];
$email= $_POST['Email'];
$message= $_POST['Message'];
$db = new mysqli('localhost',$username,$password,$database) or die("could not connect to localhost");

if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$currentDate=date();
$sql = "insert into contacts (Name,Email,Message,Timestamp) values ($name,$email,$message,$currentDate)";

if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}

?>

【讨论】:

  • 它返回了一些错误,但你是对的,我需要 mysqli。我想我需要刷牙了!
猜你喜欢
  • 1970-01-01
  • 2017-08-25
  • 1970-01-01
  • 2015-02-27
  • 1970-01-01
  • 2023-02-06
  • 1970-01-01
  • 1970-01-01
  • 2017-09-22
相关资源
最近更新 更多