我无法弄清楚我在这里做错了什么。我一直在看这个问题的不同视频,并且一直在寻找答案。
当我点击网页上的提交按钮时,字段会被清除,并且不会发生任何其他情况。可能有一个简单的错误,但我无法找出我做错了什么
<a1>
<h1>Registrer ny bruker</h1>
<hr>
<form method="post" action="" >
<input type="text" name="surName" placeholder="Etternavn" required>
<br><br>
<input type="text" name="givenName" placeholder="Fornavn" required>
<br><br>
<select name="gender">
<option value="male">male</option>
<option value="female">female</option>
</select>
<br><br>
<input type="number" name="age" placeholder="Alder" required>
<br><br>
<input type="text" name="mail" placeholder="e-post" required>
<br><br>
<input type="text" name="userName" placeholder="Brukernavn" required>
<br><br>
<input type="text" name="password" placeholder="Passord" required>
<br><br>
<input type="submit" name="submitted" value="Registrer">
</form>
</a1>
<?php
if(isset($_POST['submitted'])){
$mysqli = new mysqli('my database username and password');
$email = $mysqli->real_escape_string($_POST['mail']);
$surName = $mysqli->real_escape_string($_POST['surName']);
$givenName = $mysqli->real_escape_string($_POST['givenName']);
$username = $mysqli->real_escape_string($_POST['userName']);
$password = md5($_POST['password']);
$gender = $mysqli->real_escape_string($_POST['gender']);
$age = $mysqli->real_escape_string($_POST['age']);
$sql = "INSERT INTO blog_user (first_name, last_name, age, gender, user_password, user_name, mail)
VALUES ('$givenName','$surName','$age','$gender','$password','$username','$email')
";
$insert = $mysqli->query($sql);
if ($insert) {
echo "Succes!";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
$mysqli->close();
}
?>
编辑:当我第一次添加这个时,我做了一个小错字,所以第一个答案并没有解决我的问题。我没有给他们一个投票,我很抱歉我打错了。
答案 0 :(得分:-1)
<?php
if(isset($_POST['submitted'])){
$mysqli = new mysqli('my database username and password');
$email = $mysqli->real_escape_string($_POST['mail']);
$surName = $mysqli->real_escape_string($_POST['surName']);
$givenName = $mysqli->real_escape_string($_POST['givenName']);
$username = $mysqli->real_escape_string($_POST['userName']);
$password = md5($_POST['password']);
$gender = $mysqli->real_escape_string($_POST['gender']);
$age = $mysqli->real_escape_string($_POST['age']);
$sql = "INSERT INTO blog_user (first_name, last_name, age, gender, user_password, user_name, mail)
VALUES ('$givenName','$surName','$age','$gender','$password','$username','$email')
";
$insert = $mysqli->query($sql);
if ($insert) {
echo "Succes!";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
$mysqli->close();
}
?>
你错过了if语句。
答案 1 :(得分:-1)
您的代码是&#34;清除字段&#34;因为您的表单始终首先显示默认值。 输入字段没有给出默认值,因此&#34;清除&#34;每次执行你的php文件。
<form method="post" action="" >
<input type="text" name="surName" placeholder="Etternavn" required>
<br><br>
<input type="text" name="givenName" placeholder="Fornavn" required>
<br><br>
<select name="gender">
<option value="male">male</option>
<option value="female">female</option>
</select>
<br><br>
<input type="number" name="age" placeholder="Alder" required>
<br><br>
<input type="text" name="mail" placeholder="e-post" required>
<br><br>
<input type="text" name="userName" placeholder="Brukernavn" required>
<br><br>
<input type="text" name="password" placeholder="Passord" required>
<br><br>
<input type="submit" name="submitted" value="Registrer">
</form>
出于这个原因,您应该在显示表单之前使用表单请求部分
if(isset($_POST['submitted'])){
$mysqli = new mysqli('my database username and password');
$email = $mysqli->real_escape_string($_POST['mail']);
$surName = $mysqli->real_escape_string($_POST['surName']);
$givenName = $mysqli->real_escape_string($_POST['givenName']);
$username = $mysqli->real_escape_string($_POST['userName']);
$password = md5($_POST['password']);
$gender = $mysqli->real_escape_string($_POST['gender']);
$age = $mysqli->real_escape_string($_POST['age']);
$sql = "INSERT INTO blog_user (first_name, last_name, age, gender, user_password, user_name, mail)
VALUES ('$givenName','$surName','$age','$gender','$password','$username','$email')
";
$insert = $mysqli->query($sql);
if ($insert) {
echo "Succes!";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
$mysqli->close();
}
并显示类似这样的默认值...
<form method="post" action="" >
<input type="text" name="surName" placeholder="Etternavn"
required value="<?php echo $surName;?>">
<br><br>
<input type="text" name="givenName" placeholder="Fornavn"
required value="<?php echo $givenName;?>">
<强>等... 强>
注意数据库查询应该使用预准备语句来避免sql 注射。
答案 2 :(得分:-4)
试试这个......
<?php
if(isset($_POST['submitted'])){