Php 将表单中的数据存储到数据库中

Php 将表单中的数据存储到数据库中,php,mysql,sql,database,forms,Php,Mysql,Sql,Database,Forms,当我运行它时,它不会显示任何错误,但它不会在表客户机上的bookstore_hw3数据库中存储任何内容 表clients上还有一列名为client\u id,是自动递增的。这就是问题所在吗 是否有人可以查看此代码并提出问题所在 这是HTML表单: <form action="clientData.php" method="post"> First Name: <input type='text' id='client_fname' /><br /> Last

当我运行它时,它不会显示任何错误,但它不会在表客户机上的
bookstore_hw3
数据库中存储任何内容

表clients上还有一列名为
client\u 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>

名字:
姓氏:
城市: 请选择 普里什廷 米特罗维 佩杰 加科夫 乌罗舍瓦茨 普里兹伦
性别: 请选择 F M
用户名(3-10个字符):
密码(3-10个字符):
这是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.";
?>
问题在于:

$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
(true)或
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])的格式?[某物]:''
这是一个三元运算符,等同于
[检查某物]?[如果为true,则执行此操作]:[如果为false,则执行此操作]
。它基本上是一个简单的
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);

您使用的html属性错误,应使用name属性。例如:

<select name='client_city'>
是错误的,因为如果参数是否为isset,则函数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')";

第一个问题是HTML代码。PHP通过属性
name
而不是
id
识别HTML表单中的数据,因此您应该通过以下方式修改HTML标记:

<input type='text' id='client_fname' name='client_fname' />
为此:

$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。在我的代码中,语句
是set(foo)?foo:bar
检查变量是否存在,如果存在,则返回其内容;如果不存在,则返回
null

第三个问题是您没有在数据库上执行SQL查询。因此,还要添加以下内容:

mysql_query($sql);

此外,您的PHP代码容易受到SQL注入的攻击,应该加以修复(您可以在本文中了解更多信息:)

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
    }
对其他字段使用相同的方法。 希望能有帮助。
Simone

如果您在$sql中声明insert语句,但从未运行过查询,您希望发生什么?无论如何,请注意,您很容易受到sql注入的影响,请改用预处理语句。谢谢你们告诉我错误并向我解释。谢谢你们告诉我错误并向我解释。谢谢你们告诉我错误并向我解释。@user3355976不客气!如果你觉得我的答案有帮助,请投票支持并接受它作为公认的解决方案。我投票支持你的答案,但最后一个答案回答了所有问题,因此我接受这一个作为解决方案。@user3355976 Fair。但我不认为我的答案被投票通过,而是说它需要15%的声誉
$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;
mysql_query($sql);
    $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
    }