用PHP在数据库中插入信息
我是一个初学者,我正在努力学习PHP和SQL。我已经编写了下面的代码,以便可以将数据插入到数据库中,但我不明白它为什么不起作用。它加载了所有内容,但当我点击“注册”按钮时,它什么也不做。有人能帮我弄清楚吗用PHP在数据库中插入信息,php,css,sql,database,html,Php,Css,Sql,Database,Html,我是一个初学者,我正在努力学习PHP和SQL。我已经编写了下面的代码,以便可以将数据插入到数据库中,但我不明白它为什么不起作用。它加载了所有内容,但当我点击“注册”按钮时,它什么也不做。有人能帮我弄清楚吗 <?php include 'config.php'; ?> <?php if(isset($_POST['submit'])) { $username = $_POST['username']; $password = $_
<?php include 'config.php'; ?>
<?php
if(isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$query = "INSERT INTO users1(username,password) VALUES ('$username','$password')";
$result = mysqli_query($con,$query) or die ("problem inserting new product into database");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Sign Up</title>
<style>
button {
text-align:center;
color: gray;
}
</style>
</head>
<body>
<h2>Sign Up</h2>
<h3>Enter your data to register</h3>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">
<label class="user-name">Username</label><input type="text" name="username" placeholder="Enter your username" autofocus required"><br><br>
<label class="pass-word"> Password: </label><input type="password" name="pass" class="info" placeholder="Enter your Password" required ><br><br>
<button class="lbutton" type="submit" value="Submit">Sign Up</button>
<p>Already registered? <a href="login2.php">Login</a></p>
</body>
</html>
尝试关闭窗体
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"
method="POST">
<label class="user-name">Username</label><input type="text" name="username"
placeholder="Enter your username" autofocus required"><br><br>
<label class="pass-word"> Password: </label><input type="password"
name="pass" class="info" placeholder="Enter your Password" required ><br>
<br>
<button class="lbutton" type="submit" value="Submit">Sign Up</button>
<p>Keni nje adrese? <a href="login2.php">Futu</a></p>
</form>
尝试关闭表单
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"
method="POST">
<label class="user-name">Username</label><input type="text" name="username"
placeholder="Enter your username" autofocus required"><br><br>
<label class="pass-word"> Password: </label><input type="password"
name="pass" class="info" placeholder="Enter your Password" required ><br>
<br>
<button class="lbutton" type="submit" value="Submit">Sign Up</button>
<p>Keni nje adrese? <a href="login2.php">Futu</a></p>
</form>
如果这样做,提交按钮应该有一个名称,该名称将包含在post数组中
<button class="lbutton" type="submit" name="submit" value="Submit">Sign Up</button>
注册
在表单提交时,将为表单中具有有效名称属性的所有元素创建post数组
您还需要关闭
如果您这样做,submit按钮应该有一个名称,该名称将包含在post数组中
<button class="lbutton" type="submit" name="submit" value="Submit">Sign Up</button>
注册
在表单提交时,将为表单中具有有效名称属性的所有元素创建post数组
您还需要首先关闭
如果表单将您重定向到同一页面,您可以将操作
参数留空:操作=”
其次
检查变量$\u POST['submit']
是否存在,但表单中的任何元素都没有名称submit
,因此它将永远不存在。Html表单使用字段名作为键将变量发送到数组$\u POST
。如果没有给出名称,则是一个数字
该做什么
将name=“submit”
添加到“提交按钮”
中,这应该会起作用
将密码中的name=“pass”
更改为name=“password”
,或将php中的$\u POST['password']
更改为$\u POST['pass']
同时关闭表单(
)。现代浏览器自动关闭它,但它仍然是不正确的
其他提示
直接访问$\u POST
不是一种好做法。这里有一个更好的解决方案:$username=filter\u input(input\u POST,'username')
而不是$username=$\u POST['username']
在SQL查询中插入变量时使用函数也是一个好主意。
示例:$username=filter\u var(filter\u SANITIZE\u STRING,$username)
首先
如果表单将您重定向到同一页面,您可以将操作
参数留空:操作=”
其次
检查变量$\u POST['submit']
是否存在,但表单中的任何元素都没有名称submit
,因此它将永远不存在。Html表单使用字段名作为键将变量发送到数组$\u POST
。如果没有给出名称,则是一个数字
该做什么
将name=“submit”
添加到“提交按钮”
中,这应该会起作用
将密码中的name=“pass”
更改为name=“password”
,或将php中的$\u POST['password']
更改为$\u POST['pass']
同时关闭表单(
)。现代浏览器自动关闭它,但它仍然是不正确的
其他提示
直接访问$\u POST
不是一种好做法。这里有一个更好的解决方案:$username=filter\u input(input\u POST,'username')
而不是$username=$\u POST['username']
在SQL查询中插入变量时使用函数也是一个好主意。
示例:$username=filter\u var(filter\u SANITIZE\u STRING,$username)
您的按钮需要name=“submit”
如果您刚刚开始,现在正是查找的好时机,在坏习惯出现之前;)我肯定会在旁注中看到,您的标签需要一个属性,您的按钮需要name=“submit”
如果您刚刚开始,现在是查找的好时机,在坏习惯开始之前;)我肯定会看一个旁注,你的标签需要一个属性,但不能。没有包含name='submit'
的字段,它不能。没有包含name='submit'