本文介绍了表单未提交到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,在我在表单上输入信息后,它会使用新表单刷新 new.php 页面,但不提交任何信息.我没有收到任何错误,就像我在页面上单击刷新一样.我觉得我已经做对了一切,但我似乎无法弄清楚为什么这不起作用.我刚刚从 mysql 过渡到 mysqli,所以我对它有点菜鸟.它也不会路由回 home.php.
new.php
<?php include_once('header.php');?><?php include_once('config/db.php');?><div id="newform"><表单角色=表单"><div class="form-group"><label for="user">用户</label><input type="text" class="form-control" id="user" placeholder="用户名">
<div class="form-group"><label for="rank">Rank</label><select class="form-control" name="rank"><option value="1">Noob</option><option value="2">Semi-Noob</option></选择>
<div class="form-group"><label for="date">日期</label><input type="text" class="form-control" id="date" placeholder="<?php echo date('d M y'); ?>">
<div class="form-group"><label for="tag">标签</label><input type="text" class="form-control" id="tag" placeholder="Tag">
<中心><button type="submit" class="btn btn-default">提交</button></中心></表单>
<?phpif(isset($_POST['提交'])){//要插入数据库的值$user = $_POST['user'];$rank = $_POST['rank'];$date = $_POST['date'];$tag = $_POST['tag'];//准备语句$query = "INSERT INTO 玩家(用户、等级、日期、标签)VALUES(?, ?, ?)";$statement = $mysqli->prepare($query);//标记的绑定参数,其中(s = string, i = integer, d = double, b = blob)$statement->bind_param('siss', $user, $rank, $date, $tag);//执行if($statement->execute()){header('位置:home.php');}别的{die('错误 : ('.$mysqli->errno .') '.$mysqli->error);}$statement->close();}?><?php include_once('footer.php');?>
解决方案
在
<button type="submit" class="btn btn-default" name="submit">Submit</button>
So after I enter the information on my form it refreshes the new.php page with a new form but doesn't submit any information. I get no errors its just like I clicked refresh on the page. I feel like i've done everything right but I just can't seem to figure out why this isn't working. I've just transitioned from mysql to mysqli so I'm kinda a noob with it. It's also not routing back to home.php.
new.php
<?php include_once('header.php'); ?>
<?php include_once('config/db.php'); ?>
<div id="newform">
<form role="form">
<div class="form-group">
<label for="user">User</label>
<input type="text" class="form-control" id="user" placeholder="Username">
</div>
<div class="form-group">
<label for="rank">Rank</label>
<select class="form-control" name="rank">
<option value="1">Noob</option>
<option value="2">Semi-Noob</option>
</select>
</div>
<div class="form-group">
<label for="date">Date</label>
<input type="text" class="form-control" id="date" placeholder="<?php echo date('d M y'); ?>">
</div>
<div class="form-group">
<label for="tag">Tag</label>
<input type="text" class="form-control" id="tag" placeholder="Tag">
</div>
<center>
<button type="submit" class="btn btn-default">Submit</button>
</center>
</form>
</div>
<?php
if(isset($_POST['submit'])){
//Values to be inserted into the DB
$user = $_POST['user'];
$rank = $_POST['rank'];
$date = $_POST['date'];
$tag = $_POST['tag'];
//Preparing the statement
$query = "INSERT INTO players (user, rank, date, tag) VALUES(?, ?, ?)";
$statement = $mysqli->prepare($query);
//Binding Parameters for markers, where (s = string, i = integer, d = double, b = blob)
$statement->bind_param('siss', $user, $rank, $date, $tag);
//Execution
if($statement->execute()){
header('Location: home.php');
}else{
die('Error : ('.$mysqli->errno .') '. $mysqli->error);
}
$statement->close();
}
?>
<?php include_once('footer.php'); ?>
解决方案
Check after adding name="submit"
in <button type="submit" class="btn btn-default">Submit</button>
<button type="submit" class="btn btn-default" name="submit">Submit</button>
这篇关于表单未提交到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!