gpt4 book ai didi

php - 如何通过模态向mysql插入数据

转载 作者:太空宇宙 更新时间:2023-11-03 10:48:42 25 4
gpt4 key购买 nike

我一直在尝试弄清楚如何在点击提交后将数据从模式插入到数据库中。我现在遇到的问题是它只是不断刷新页面,但没有添加任何内容,请帮忙。

这是我的模态调用:

  <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">&times;</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('','','');
}?>

这就像这个项目的断骨。已经为此工作了一段时间,我正试图从子窗口移开并进入模态,但我不知道该怎么做。

最佳答案

它不起作用的原因是因为你的条件语句:

if (isset($_POST['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它们更安全

关于php - 如何通过模态向mysql插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27559216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com