【发布时间】:2016-08-17 03:23:41
【问题描述】:
有人可以帮忙吗?所以我正在使用 wamp 服务器进行 php 练习。当我只有 3 列时,我的表单只接受记录,但是当我添加更多列时,记录不会被添加。
代码如下
html表单
<body>
<form action = "Form.php" method = "post">
<label> Title: </label> <input type = "text" name = "title"/>
<label> First Name: </label> <input type = "text" name = "fname"/>
<label> Last Name: </label> <input type = "text" name = "lname"/>
<label> EmailAddress: </label> <input type = "text" name = "address"/>
<label> Hobby: </label> <input type = "text" name = "hobby"/>
<label> Sex: </label> <input type = "text" name = "gender"/>
<label> UserName: </label> <input type = "text" name = "uname"/>
<input type = "submit" name = "submitbtn" value = "Submit"/>
</form>
</body>
php页面
<?php
if (isset($_POST['submitbtn'])){
$title = $_POST['title'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$address = $_POST['address'];
$hobby = $_POST['hobby'];
$sex = $_POST['gender'];
$uname = $_POST['uname'];
$connect_db = mysqli_connect("localhost","root","","Humans");
if(!$connect_db){
echo "Connection failed";
} else
echo "Connection successful";
if(!mysqli_select_db($connect_db, 'Humans')){
echo "Database not selected";
} else
echo "Database selected";
$sql = "INSERT INTO People(Title, FirstName, LastName, EmailAddress, Hobby, Sex, UserName)VALUES('$title','$fname','$lname','$address','$hobby','$sex','$uname')";
if(mysqli_query($connect_db, $sql)){
echo "Record was added";
}else
echo "Record was not added";
}
?>
输出:
Connection successful
Database selected
Record was not added
【问题讨论】:
-
phpMyAdmin不是数据库它是一个用 PHP 编写的工具,可让您维护 MYSQL 数据库 -
打印查询并在 PhpMyadmin 中运行以查看错误,例如 echo $sql;die;复制并在 myadmin 中运行此查询。
-
简单的代码缩进让你的代码更容易阅读,更重要的是更容易调试
-
echo "没有添加记录-" . mysqli_error($conn);
-
无密码的 root、sql 注入问题、错误的代码缩进等等......在构建数据库之前,您可能需要花更多时间了解 PHP 和 MySQL 的工作原理
标签: php phpmyadmin wampserver