我目前遇到问题,将表单字段推送到数据库中。这是我的HTML代码:
<form action="" method="post">
<p>Gender:</p><input type="radio" value="Male" name="gender">Male</input>
<input type="radio" value="Female" name="gender">Female</input><br>
<p>Birthday :</p>
<input type="text" placeholder="Help VITians to wish you."></input><br>
<p>Relationship :</p>
<select name="relation">
<option value="I don't want to say">I dont want to say</option>
<option value="Single">Single</option>
<option value="In a Relationship">In a Relationship</option>
</select><br><br>
<input type="submit" name="basic" value="Submit"></input>
</form>
这是我的PHP代码:
<?php
$senddata = @$_POST['basic'];
//Password variables
$gender = strip_tags(@$_POST['gender']);
$birthday = strip_tags(@$_POST['birthday']);
$relation = strip_tags(@$_POST['relation']);
if ($senddata) {
//If the form has been submitted ..
//Check whether old password equals $db_password
if (strlen($birthday) <= 4) {
echo "Sorry! Birthday is not right! Try entering it in DD/MM/YYYY format.";
}
else if(empty($birthday)){
echo "Please enter your Birthday";
}
else
{
$update_query = mysqli_query($conn,"UPDATE users SET gender='$gender', birthday='$birthday',relation='$relation' WHERE username='$user'");
echo "Success! Your basic information has been updated!";
}
}
else
{
}
?>
,但我无法进入数据库,我的PHP代码也没有在同一页面上显示错误消息。有什么错误吗?
答案 0 :(得分:0)
首先在生日输入中给出名称设置数据库连接(如果没有设置),我在代码中的任何地方都看不到$user
。请先检查$user
是否有任何值或不是这样的:
$update = mysqli_query($conn,"UPDATE `users` SET `gender`='".$gender."', `birthday`='".$birthday."',`relation`='".$relation."' WHERE `username`='".$user."'");
答案 1 :(得分:0)
你的where条件和mysql连接也有问题。
请声明$ user和$ conn。
$user = "usernameindb";
//DATA BASE CONNECTION
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = mysqli_connect($servername, $username, $password, $dbname);
$update_query = mysqli_query($conn,"UPDATE `users` SET `gender`='".$gender."', `birthday`='".$birthday."',`relation`='".$relation."' WHERE `username`='".$user."'");