当我运行它时,它不会显示任何错误,但是它不会在表客户端的bookstore_hw3数据库上存储任何内容。
表客户端上还有一个名为client_id的列,它是自动递增的。这就是问题所在吗?
有人能回顾一下这段代码并提出问题所在吗?
这是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='';
?>发布于 2014-05-11 17:01:43
问题在于:
$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']);isset所做的就是返回一个1 (真)或0 (false),所以您要将数据值设置为1或0。所以应该是这样的:
$fname = isset($_POST['client_fname']) ? $_POST['client_fname'] '';
$lname = isset($_POST['client_lname']) ? $_POST['client_lname'] '';
$city = isset ($_POST['client_city']) ? $_POST['client_city'] '';
$sex = isset($_POST['client_sex']) ? $_POST['client_sex'] '';
$username = isset($_POST['client_username']) ? $_POST['client_username'] '';
$pass = isset($_POST['client_pass']) ? $_POST['client_pass'] '';注意isset([something]) ? [something] : '';的格式,它是一个三元操作符,等同于[check something] ? [if true do this] : [if false do this]。它基本上是一个简单的if/else逻辑位,但在一条紧凑的线上。
此外,您还设置了查询,但从不运行它:
$sql = "INSERT INTO clients (client_fname, client_lname, client_city, client_sex, client_username, client_pass) VALUES ('$fname','$lname','$city','$sex','$username','$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_query($sql, $connection);发布于 2014-05-11 17:02:53
您使用的html属性是错误的,应该使用name属性。例如:
<select name='client_city'>和密码
$fname = isset($_POST['client_fname']);是错误的,因为如果参数是或不是isset,则函数将返回true或false。
所以插入的查询是
"INSERT INTO clients (client_fname, client_lname, client_city, client_sex, client_username, client_pass) VALUES ('false','false','false','false','false','false')";发布于 2014-05-11 17:12:29
使用isset函数是不正确的。
您可以使用如下所示:
$fname = ""; // default for var for insert statement
if (isset($_POST['client_fname'])) // check if the field if not emprty
{
$fname = $_POST['client_fname'] // set the var for insert statement
}与其他字段使用相同的方法。
https://stackoverflow.com/questions/23595390
复制相似问题