使用PHP在MySQL中存储简单数据

时间:2013-01-18 21:25:20

标签: php mysql

我已经设置了这个简单的表单,允许您输入个人详细信息并将其存储在数据库中,但它不起作用。

表单HTML:

<html>
<body>
<form name="test" action="store.php" method="post">
Voornaam: <input type="text" name="first_name"> <br>
Achternaam: <input type="text" name="last_name"> <br>
E-Mail: <input type="text" name="e-mail"> <br>
Telefoon: <input type="text" name="phone"> <br>
Adres: <input type="text" name="address"> <br>
Postcode: <input type="text" name="postal_code"> <br>
Stad: <input type="text" name="city"> <br>
Provincie: <input type="text" name="province"> <br>
Land: <input type="text" name="country"> <br>
<input type="submit" value="Verzenden">
</form>
<body>
</html>

PHP:

<?php
/*Database test.
© 2013 Sydcul. All rights reserved.*/

//Please change the following settings:
/*Your database host*/ $host = "localhost";
/*Your database user*/ $user = "root";
/*Your database password*/ $password = "";

//Please don't change the following:
$connection = mysql_connect($host, $user, $password);

mysql_select_db("contact", $connection);

$debug = "INSERT INTO data (first_name, last_name, e-mail, phone, address, postal_code, city, province, country)
VALUES ('" . $_POST["first_name"] . "', '" . $_POST["last_name"] . "', '" . $_POST["e-mail"] . "', '" . $_POST["phone"] . "', '" . $_POST["address"] . "', '" . $_POST["postal_code"] . "', '" . $_POST["city"] . "', '" . $_POST["province"] . "', '" . $_POST["country"] . "')";

mysql_query("INSERT INTO data (first_name, last_name, e-mail, phone, address, postal_code, city, province, country)
VALUES ('" . $_POST["first_name"] . "', '" . $_POST["last_name"] . "', '" . $_POST["e-mail"] . "', '" . $_POST["phone"] . "', '" . $_POST["address"] . "', '" . $_POST["postal_code"] . "', '" . $_POST["city"] . "', '" . $_POST["province"] . "', '" . $_POST["country"] . "')");

echo $debug;

mysql_close($connection);
?>

我相信我有一次工作,但那只是名字。 我认为这是我看不到的非常明显的事情,所以请花点时间看一下并帮助我。

我把调试内容放进去调试,它告诉我这个:

INSERT INTO data (first_name, last_name, e-mail, phone, address, postal_code, city, province, country) VALUES ('John', 'Johnson', 'john@johnson.com', '1234567890', 'Street 1', '1234 AB', 'City', 'Province', 'Country')

1 个答案:

答案 0 :(得分:3)

您的电子邮件字段e-mail必须被反引号字符(`)包围,以便您的最终查询

INSERT INTO data (first_name, last_name, `e-mail`, phone, address, postal_code, city, province, country) VALUES ('John', 'Johnson', 'john@johnson.com', '1234567890', 'Street 1', '1234 AB', 'City', 'Province', 'Country')