PHP:将表单中的值插入MySQL
我从终端在PHP:将表单中的值插入MySQL,php,html,mysql,Php,Html,Mysql,我从终端在mysql中创建了一个users表,并试图创建一个简单的任务:从表单中插入值。这是我的dbConfig文件 <?php $mysqli = new mysqli("localhost", "root", "pass", "testDB"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exi
mysql
中创建了一个users
表,并试图创建一个简单的任务:从表单中插入值。这是我的dbConfig文件
<?php
$mysqli = new mysqli("localhost", "root", "pass", "testDB");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
?>
点击我的保存按钮后,什么也没发生,数据库仍然是空的。我尝试了
echo'ing
插入INSERT
查询,它按照预期从表单中获取所有值。在我尝试从终端检查这是否有效后,我登录到我的sql
尝试从users表返回所有数据,我得到一个空集。以下代码只是声明了一个包含MySQL查询的字符串变量:
$sql = "INSERT INTO users (username, password, email)
VALUES ('".$_POST["username"]."','".$_POST["password"]."','".$_POST["email"]."')";
它不执行查询。为了做到这一点,你需要使用一些函数,但让我先解释一些其他的东西
决不信任用户输入:决不应将用户输入(例如来自$\u GET
或$\u POST
的表单输入)直接添加到查询中。有人可以以这种方式小心地操作输入,从而对您的数据库造成巨大损害。这就是所谓的SQL注入。你可以阅读更多关于它的内容
要保护脚本免受此类攻击,您必须使用预先准备好的语句。更多关于准备好的声明
将准备好的语句包含到代码中,如下所示:
$sql = "INSERT INTO users (username, password, email)
VALUES (?,?,?)";
请注意?
是如何用作值的占位符的。接下来,您应该使用mysqli\u prepare
准备语句:
$stmt = $mysqli->prepare($sql);
然后开始将输入变量绑定到准备好的语句:
$stmt->bind_param("sss", $_POST['username'], $_POST['email'], $_POST['password']);
最后执行准备好的语句。(这是实际插入的位置)
注意虽然不是问题的一部分,但我强烈建议您不要以明文形式存储密码。相反,您应该使用
密码\u散列
来存储密码的散列您的代码中有两个问题
<?php
$conn=mysqli_connect("localhost","root","password","testDB");
if(!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
?>
include('dbConfig.php');
<!Doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="$1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="style.css">
<title>test</title>
</head>
<body>
<?php
if(isset($_POST['save']))
{
$sql = "INSERT INTO users (username, password, email)
VALUES ('".$_POST["username"]."','".$_POST["password"]."','".$_POST["email"]."')";
$result = mysqli_query($conn,$sql);
}
?>
<form action="index.php" method="post">
<label id="first"> First name:</label><br/>
<input type="text" name="username"><br/>
<label id="first">Password</label><br/>
<input type="password" name="password"><br/>
<label id="first">Email</label><br/>
<input type="text" name="email"><br/>
<button type="submit" name="save">save</button>
</form>
</body>
</html>
index.php
<?php
$conn=mysqli_connect("localhost","root","password","testDB");
if(!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
?>
include('dbConfig.php');
<!Doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="$1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="style.css">
<title>test</title>
</head>
<body>
<?php
if(isset($_POST['save']))
{
$sql = "INSERT INTO users (username, password, email)
VALUES ('".$_POST["username"]."','".$_POST["password"]."','".$_POST["email"]."')";
$result = mysqli_query($conn,$sql);
}
?>
<form action="index.php" method="post">
<label id="first"> First name:</label><br/>
<input type="text" name="username"><br/>
<label id="first">Password</label><br/>
<input type="password" name="password"><br/>
<label id="first">Email</label><br/>
<input type="text" name="email"><br/>
<button type="submit" name="save">save</button>
</form>
</body>
</html>
include('dbConfig.php');
测试
使用函数mysqli_query执行SQL查询您的代码只是echo
ing一个SQL查询…您需要mysqli_查询($mysqli,$SQL)代码>查询。您当前只是echo
ing查询,它基本上只是将其输出到屏幕上;谢谢你这么详细的回答。我将对此进行辩护,因为这是我对PHP的第一次尝试,我只关注功能,但我将在未来尝试改进。@不现实如果您刚刚开始,我强烈建议您研究PDO。它比mysqli更容易使用,尤其是在预处理语句中。我尝试了这个方法,但遇到一个错误,抱怨mysqli_prepare需要2个参数。@dimluca参数绑定中的“sss”是什么?@A.Volg's代表字符串。如果其中一个参数是一个数字,我们将使用“d”表示十进制