我有一个预先填充了user_ID列表的表[user_id,name]。我想创建一个表单,允许用户针对其user_id注册名称。
以下是基本的HTML表单:
<form action="" id="contact-form" class="form-horizontal" method="post">
<label class="control-label" for="inputName">Enter Name</label>
<input type="text" class="form-control input-lge" name="inputName" id="inputName">
<input type="hidden" class="form-control" id="hidUid" name="hidUid" value="<?php echo $reg_user_id?>">
<input type="submit" id="sub_button" class="btn btn-default btn-lg" value="Save"/>
</form>
注意$ reg_user_id是会话设置的user_id变量。
这是我在PHP的开始:
if(isset($_POST['hidUid']) && isset($_POST['inputName'])){
$inputName=$_POST['inputName'];
$inputUid=$_POST['hidUid'];
if ($stmtint = $mysqli->prepare("SELECT user_email FROM users WHERE user_ID=?")) {
$stmtint->bind_param("s", $inputUid);
$stmtint->execute();
$stmtint->bind_result($user_email);
$stmtint->fetch();
$stmtint->close();
if($user_email != "" && $inputName !=""){
$user_email="";
if ($stmtint = $mysqli->prepare("UPDATE users SET name=? where user_ID=?")) {
$stmtint->bind_param("ss", $inputName,$inputUid);
$stmtint->execute();
$stmtint->close();
echo "User saved successfully";
exit;
}
}
else{
echo "Please enter correct Name and password";
exit;
}
}
}
我的问题是表单提交成功,但数据库没有更新。我在PHP输出或控制台日志中都没有错误。有什么想法吗?
答案 0 :(得分:1)
如果您想查看错误,请尝试此操作。
if(isset($_POST['hidUid']) && isset($_POST['inputName'])){
$inputName = $_POST['inputName'];
$inputUid = $_POST['hidUid'];
if ($stmtint = $mysqli->prepare("SELECT user_email FROM users WHERE user_ID = ?")) {
$stmtint->bind_param("s", $inputUid);
if ($stmtint->execute()) {
$stmtint->bind_result($user_email);
$stmtint->fetch();
$stmtint->close();
}else{
die("Error Message:".$mysqli->error);
}
if ($user_email != "" && $inputName != "") {
$user_email="";
if ($stmtint = $mysqli->prepare("UPDATE users SET name = ? where user_ID = ?")) {
$stmtint->bind_param("ss", $inputName,$inputUid);
if ($stmtint->execute()) {
$stmtint->close();
echo "User saved successfully";
}else{
die("Error Message:".$mysqli->error);
}
exit;
}
}else{
echo "Please enter correct Name and password";
exit;
}
}
}