php - 将表单中的数据存储到数据库中

标签 php mysql sql database forms

当我运行它时,它没有显示任何错误,但它没有在 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='';
?>

最佳答案

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

<input type='text' id='client_fname' name='client_fname' />

第二个问题与您的 PHP 代码有关。 替换这部分:

$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

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

mysql_query($sql);

此外,您的 PHP 代码容易受到 SQL 注入(inject)的攻击,应该予以修复(您可以在这篇文章中阅读更多相关信息:How can I prevent SQL injection in PHP?)

关于php - 将表单中的数据存储到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23595390/

相关文章:

php - 您使用什么样的 HTMLPurifier 设置来净化 CSS?

PHP Openssl 解密 AES Mysql 加密

MySQL 筛选结果,其中列 = '$'

java - 这段代码在java中运行后没有显示输出?

java - 第二个结果集给出错误

php - 如何确定 'Codeigniter' 中执行查询的状态?

php - 每个用户临时记录 PHP 数据库的好方法?

mysql - 你如何在 MySQL 中将回车附加到值?

MYSQL - 从两个表的 INNER JOIN 获取数据

python - 将 Pandas 操作转换为 SQL 查询