问题描述
当我运行它时它不会显示任何错误,但是它不会在表客户端的 bookstore_hw3
数据库中存储任何内容。
It does not show any error when I run it but it does not store anything on the bookstore_hw3
database on the table clients.
表客户端上还有一列称为 client_id
,并且会自动递增。这是问题吗?
There is one more column on the table clients called client_id
and is auto incremented. Is this the problem?
有人可以查看此代码并提出问题所在吗?
Can someone review this code and suggest what the problem is?
这是HTML表单:
<form action="clientData.php" method="post">
First Name: <input type='text' id='client_fname' /><br />
Last Name: <input type='text' id='client_lname' /><br />
City: <select id='client_city'>
<option>Please Choose</option>
<option>Prishtine</option>
<option>Mitrovice</option>
<option>Peje</option>
<option>Gjakove</option>
<option>Ferizaj</option>
<option>Prizren</option>
</select><br />
Gender: <select id='client_sex'>
<option>Please Choose</option>
<option>F</option>
<option>M</option>
</select><br />
Username(3-10 characters): <input type='text' id='client_username' /><br />
Password(3-10 characters): <input type='password' id='client_pass' /><br />
<input type='submit' value='Submit' />
<input type="reset" value="Clear" />
</form>
这是PHP代码:
<?php
include('db_login.php');
// Connect
$connection = mysql_connect($db_host, $db_username, $db_password);
if (!$connection){
die("Could not connect to the database: <br />". mysql_error( ));
}
// Select the database
$db_select = mysql_select_db($db_database);
if (!$db_select){
die ("Could not select the database: <br />". mysql_error( ));
}
$fname = isset($_POST['client_fname']);
$lname = isset($_POST['client_lname']);
$city = isset ($_POST['client_city']);
$sex = isset($_POST['client_sex']);
$username = isset ($_POST['client_username']);
$pass = isset($_POST['client_pass']);
$sql = "INSERT INTO clients (client_fname, client_lname, client_city, client_sex, client_username, client_pass) VALUES ('$fname','$lname','$city','$sex','$username','$pass')";
mysql_close();
echo "Data stored on database.";
?>
这是登录代码:
<?php
$db_host='localhost';
$db_database='bookstore_hw3';
$db_username='root';
$db_password='';
?>
推荐答案
第一个问题是HTML代码。 PHP可以通过属性 name
而不是 id
来识别HTML表单中的数据,因此您应该以这种方式修改HTML标记:
The first problem is in the HTML code. PHP recognizes the data from the HTML form by the attribute name
not id
, so you should modify your HTML tags this way:
<input type='text' id='client_fname' name='client_fname' />
第二个问题是您的PHP代码。
替换此部分:
The second problem is with your PHP code.Replace this part:
$fname = isset($_POST['client_fname']);
$lname = isset($_POST['client_lname']);
$city = isset ($_POST['client_city']);
$sex = isset($_POST['client_sex']);
$username = isset ($_POST['client_username']);
$pass = isset ($_POST['client_pass']);
其中:
$fname = isset($_POST['client_fname']) ? $_POST['client_fname'] : null;
$lname = isset($_POST['client_lname']) ? $_POST['client_lname'] : null;
$city = isset ($_POST['client_city']) ? $_POST['client_city'] : null;
$sex = isset($_POST['client_sex']) ? $_POST['client_sex'] : null;
$username = isset ($_POST['client_username']) ? $_POST['client_username'] : null;
$pass = isset ($_POST['client_pass']) ? $_POST['client_pass'] : null;
isset()
仅检查变量存在并返回true / false。在我的代码中,语句 isset(foo)? foo:bar
检查变量是否存在,如果存在,则返回其内容;如果不存在,则返回 null
。
isset()
only checks if the variable exists and returns true/false. In my code, the statement isset(foo) ? foo : bar
checks if the variable exists and if yes, its content is returned, if no, then null
is returned.
第三个问题是您没有在数据库上执行SQL查询。因此,还要在此添加:
The third problem is that you are not executing your SQL query on the database. So add there also this:
mysql_query($sql);
此外,您的PHP代码容易受到SQL注入的影响,应予以修复(您可以在这篇文章:)
Also your PHP code is vulnerable to SQL injection and should be fixed (you can read more about it in this post: How can I prevent SQL injection in PHP?)
这篇关于将数据从表单存储到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!