我一直试图弄清楚如何在点击提交后将模式中的数据插入到数据库中。我现在的问题是它只是不断刷新页面,但没有添加任何内容,请帮助。
这是我的模态电话:
<button class="btn btn-default" data-toggle="modal" data-target="#loginModal">Login</button>
<div class="modal fade" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Login</h4>
</div>
<div class="modal-body">
<?php include("new.php");?>
</div>
</div>
</div>
</div>
这是我的new.php:
<?php
function renderForm($user, $rank, $position, $error)
{
?>
<?php
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<div class="form-group">
<label for="username">Username</label>
<input id="username" class="form-control" type="text" name="user" placeholder="Username" value="<?php echo $user; ?>" />
</div>
<div class="form-group">
<label for="rank">Rank</label>
<select class="form-control" name="rank">
<option value="recruit">recruit</option>
<option value="officer">officer</option>
<option value="leader">leader</option>
</select>
</div>
<div class="form-group">
<label for="position">Position</label>
<input id="position" class="form-control" type="text" name="position" placeholder="Leader" value="<?php echo $position; ?>" />
</div>
<div class="form-group">
<label for="Date">Date</label>
<input id="Date" class="form-control" type="text" name="date" placeholder="<?php echo date('d M y'); ?>" value="<?php echo $date; ?>" />
</div>
<div class="form-group">
<label for="Tag">Tag</label>
<input id="Tag" class="form-control" type="text" name="tag" value="<?php echo $tag; ?>" />
</div>
<div class="form-group">
<label for="AiT">AiT</label>
<input id="AiT" class="form-control" type="text" name="ait" value="<?php echo $ait; ?>" />
</div>
<div class="form-group">
<label for="ServiceStripes">Service Stripes</label>
<input id="ServiceStripes" class="form-control" type="text" name="ss" value="<?php echo $ss; ?>" />
</div>
<div class="form-group">
<label for="Notes">Notes</label>
<input id="Notes" class="form-control" type="text" name="notes" placeholder="Notes" value="<?php echo $notes; ?>" />
</div>
<button type="submit" class="btn btn-default" value="Submit">Submit</button>
</form>
<?php
}
include('classes/connect-db.php');
if (isset($_POST['submit']))
{
$user = mysql_real_escape_string(htmlspecialchars($_POST['user']));
$rank = mysql_real_escape_string(htmlspecialchars($_POST['rank']));
$position = mysql_real_escape_string(htmlspecialchars($_POST['position']));
$date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
$tag = mysql_real_escape_string(htmlspecialchars($_POST['tag']));
$ait = mysql_real_escape_string(htmlspecialchars($_POST['ait']));
$ss = mysql_real_escape_string(htmlspecialchars($_POST['ss']));
$notes = mysql_real_escape_string(htmlspecialchars($_POST['notes']));
if ($user == '' || $rank == '' || $date == '' || $tag == '')
{
$error = '<center>ERROR: Please fill in all required fields!</center>';
@renderForm($user, $rank, $position, $error);
}
else
{
mysql_query("INSERT players SET user='$user', rank='$rank', position='$position', date='$date', tag='$tag', ait='$ait', ss='$ss', notes='$notes'")
or die(mysql_error());
}
}
else
{
@renderForm('','','');
}?>
这就像这个项目的骨干。我已经做了一段时间了,我试图从儿童窗户转移到模态,但我无法弄清楚如何去做。
答案 0 :(得分:4)
它不起作用的原因是因为你的条件声明:
if (isset($_POST['submit'])){...}
您的代码中没有名为“submit”的元素,我怀疑它应该与您的提交按钮一起使用:
<button type="submit" class="btn btn-default" value="Submit">Submit</button>
将其修改为:
<button type="submit" name="submit" class="btn btn-default" value="Submit">Submit</button>
报告错误报告时,会发出“未定义索引”通知。
将error reporting添加到文件的顶部,这有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:错误报告应仅在暂存时完成,而不是生产。
脚注:
考虑转向更安全的MySQL API:
使用mysqli
with prepared statements或PDO with prepared statements,它们更安全。