数据不会从HTML表单传递到数据库

时间:2018-02-01 09:57:29

标签: php html mysql forms

我是PHP的新手,我不确定我做错了什么。我可以打开html表单并提供数据,但是当我点击提交时,它显示我的PHP代码和数据库(显然)没有更新。我已经尝试通过phpMyAdmin手动向表中插入值,它可以工作。我看过网上但我的语法看起来很好(对我来说)。我做错了吗?如果错误不太明显是否有一种有效的调试方法(即看错误)?

我有这个HTML代码

<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Hotel Start Page</title>
</head>

<body>
<form action="customerCreate.php" method="post">
<fieldset><legend>Enter your information in the form below:</legend>
<header align="center">   <b>Customer Sign Up</b></header>
<p><b>Surname:            </b><input type="text" name="surname" size="30" maxlength="40"/></p>
<p><b>Name:               </b><input type="text" name="name" size="30" maxlength="40"/></p>
<p><b>E-mail:             </b><input type="text" name="email" size="30" maxlength="40"/></p>
<p><b>Telephone:          </b><input type="text" name="tel" size="30" maxlength="10"/></p>
<p><b>Password:           </b><input type="password" name="passwd" size="30" maxlength="10"/></p> 
</fieldset>
<div align="center"><input type="submit" name="submit" value="Create Account"/></div>
</form>

</body>

这个PHP代码

<?php

 $host = 'localhost';
 $username = 'root';
 $password = '';
 $db = 'my_hotel';

 $conn = new mysqli($host,$username,$password,$db);

if ($conn->connect_error) {
  die("Connection Error: " .$conn->connect_error);
}

$sname = $_POST['surname'];
$name = $_POST['name'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$passwd = $_POST['passwd'];

$sql = "INSERT INTO Customer (sname, name, email, tel, passwd) VALUES 
(?,?,?,?,?)";

$stmt = $conn->prepare($sql);
$stmt->bind_param("sssss",$sname,$name,$email,$tel,$passwd);
$stmt->execute();

$cid = "SELECT cid FROM Customers WHERE sname='$sname' AND passwd='$passwd' ";
$result = $conn->query($cid);
$row = $result->fetch_assoc();

echo "Customer Added.<br>";
echo "Your Customer ID is ".$row['cid'];

$stmt->close();
$conn->close();

?>

此表位于phpMyAdmin中名为My_hotel的数据库中

CREATE TABLE Customer (
   cid INT AUTO_INCREMENT,
   sname VARCHAR(15),
   name VARCHAR(15),
   email VARCHAR(15),
   tel VARCHAR(15),
   passwd VARCHAR(15),
   PRIMARY KEY (cid)
);

1 个答案:

答案 0 :(得分:1)

如果您获得了php代码,那么您的应用程序必须不在您的Web服务器中,因此浏览器会下载源文件。您应该将文件复制/同步到(可能)/var/www/html

请注意:http://thisinterestsme.com/php-displayed-in-browser/