如何通过模态编辑mysql中的数据
Posted
技术标签:
【中文标题】如何通过模态编辑mysql中的数据【英文标题】:How to edit data in mysql through a modal 【发布时间】:2015-02-18 00:35:55 【问题描述】:我一直在使用此代码时遇到错误。我是引导程序和使用模态的新手。我曾经为我所有的“花式”表单运行子表单,但现在我正在向模态移动。这是我抓取模态的代码:
<button class="btn btn-primary" data-toggle="modal" data-target="#newmemberModal">Add new Member</button>
<br />
<br />
<?php
$result = mysql_query("SELECT * FROM `players` ORDER BY `players`.`user` ASC")
or die(mysql_error());
echo " <div class='table-responsive'><table class='table'>";
echo "<tr><th>Username</th> <th>Rank</th> <th>Position</th> <th>DoP</th> <th>Tag</th> <th>AiT</th> <th>Service Stripes</th> <th>Notes</th> <th></th> <th></th><th></th></tr>";
while($row = mysql_fetch_array( $result ))
echo "<tr>";
echo '<td>' . $row['user'] . '</td>';
echo '<td>' . $row['rank'] . '</td>';
echo '<td>' . $row['position'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['tag'] . '</td>';
echo '<td>' . $row['ait'] . '</td>';
echo '<td>' . $row['ss'] . '</td>';
echo '<td>' . $row['notes'] . '</td>';
echo '<td><button class="btn btn-default" data-toggle="modal" data-target="#editModal">Edit</button></td>';
echo '<td><a href="classes/forms/delete.php?id=' . $row['id'] . '">Delete</a></td>';
echo '<td><a href="javascript:void(0);" NAME="My Window Name" title=" My title here " onClick=window.open("dd.php?id=' . $row['id'] . '","Ratting","width=350,height=550,0,status=0,");>DD</a><BR /><a href="javascript:void(0);" NAME="My Window Name" title=" My title here " onClick=window.open("hd.php?id=' . $row['id'] . '","Ratting","width=350,height=550,0,status=0,");>HD</a></td>';
echo "</tr>";
echo "</table> </div>";
?>
这是我的edit.php:
<?php include_once('classes/check.class.php'); ?>
<?php
function renderForm($id, $user, $rank, $position, $date, $tag, $ait, $ss, $notes, $error)
?>
<?php
if ($error != '')
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
?>
<?php if( protectThis("1, 2") ) : ?>
<center><form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<strong>Username: *</strong> <br/><input class="tb5" type="text" name="user" value="<?php echo $user; ?>" /><br/><br/>
<strong>Rank: * Current is </strong> <?php echo $rank; ?> <br/>
<select name="rank">
<option value="recruit" <?php if ($rank == 'recruit') echo 'selected'; ?>>Pending Rank</option>
<option value="officer" <?php if ($rank == 'officer') echo 'selected'; ?>>PVT</option>
<option value="leader" <?php if ($rank == 'leader') echo 'selected'; ?>>PV2</option>
</select><br/><br/>
<strong>Position: </strong> <br/><input class="tb5" type="text" name="position" value="<?php echo $position; ?>" /><br/><br/>
<strong>Date: *</strong> <br/><input class="tb5" type="text" name="date" value="<?php echo $date; ?>" /><br/><br/>
<strong>Tag: *</strong> <br/><input class="tb5" type="text" name="tag" value="<?php echo $tag; ?>" /><br/><br/>
<strong>AiT's: </strong> <br/><input class="tb5" type="text" name="ait" value="<?php echo $ait; ?>" /><br/><br/>
<strong>Service Stripes: </strong><br/> <input class="tb5" type="text" name="ss" value="<?php echo $ss; ?>" /><br/><br/>
<strong>Notes: </strong><br/> <input class="tb5" type="text" name="notes" value="<?php echo $notes; ?>" /><br/><br/>
<input class="btn-style" type="submit" name="submit" value="Submit"></center>
</div>
</form>
<?php elseif( protectThis(6) ) : ?>
<center><form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<strong>Username: *</strong> <br/><input class="tb5" type="test" name="user" value="<?php echo $user; ?>" disabled /><br/><br/>
<strong>Rank: * Current is </strong> <?php echo $rank; ?> <br/>
<select name="rank">
<option value="recruit" <?php if ($rank == 'recruit') echo 'selected'; ?>>PVT</option>
<option value="officer" <?php if ($rank == 'officer') echo 'selected'; ?>>PV2</option>
</select><br/><br/>
<strong>Position: </strong> <br/><input class="tb5" type="hidden" name="position" value="<?php echo $position; ?>" /><br/><br/>
<strong>Date: *</strong> <br/><input class="tb5" type="text" name="date" value="<?php echo $date; ?>" /><br/><br/>
<strong>Tag: *</strong> <br/><input class="tb5" type="text" name="tag" value="<?php echo $tag; ?>" /><br/><br/>
<strong>AiT's: </strong> <br/><input class="tb5" type="text" name="ait" value="<?php echo $ait; ?>" /><br/><br/>
<strong>Service Stripes: </strong><br/> <input class="tb5" type="hidden" name="ss" value="<?php echo $ss; ?>" /><br/><br/>
<strong>Notes: </strong><br/> <input class="tb5" type="hidden" name="notes" value="<?php echo $notes; ?>" /><br/><br/>
<input class="btn-style" type="submit" name="submit" value="Submit"></center>
</div>
</form>
<?php endif; ?>
<?php
include('connect-db.php');
if (isset($_POST['submit']))
if (is_numeric($_POST['id']))
$id = $_POST['id'];
$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 = 'ERROR: Please fill in all required fields!';
@renderForm($id, $user, $rank, $position, $date, $tag, $ait, $ss, $notes, $error);
else
mysql_query("UPDATE players SET user='$user', rank='$rank', position='$position', date='$date', tag='$tag', ait='$ait', ss='$ss', notes='$notes' WHERE id='$id'")
or die(mysql_error());
header("Location: home.php");
else
echo 'Error!';
else
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM players WHERE id=$id")
or die(mysql_error());
$row = mysql_fetch_array($result);
if($row)
$id = $row['id'];
$user = $row['user'];
$rank = $row['rank'];
$position = $row['position'];
$date = $row['date'];
$tag = $row['tag'];
$ait = $row['ait'];
$ss = $row['ss'];
$notes = $row['notes'];
@renderForm($id, $user, $rank, $position, $date, $tag, $ait, $ss, $notes, '');
else
echo "No results!";
else
echo 'Error!';
?>
问题是我不断收到“错误!”模态框中的消息,而不是其他任何内容。我试图做的目标是让它从存储在我的数据库中的 ID 中提取数据。我认为问题实际上是当我单击用户的编辑时它没有获取 ID。坦率地说,我不知道如何通过模式编辑 ID。帮助将不胜感激。谢谢
奖励:我无法在同一页面上打开两个模式。如果我在同一页面上有两个,它们都不会出现:
<!--Modals-->
<div class="modal fade" id="newmemberModal" 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">Add New Member</h4>
</div>
<div class="modal-body">
<?php include("classes/forms/new.php"); ?>
</div>
</div>
</div>
</div>
<div class="modal fade" id="editModal" 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">Edit</h4>
</div>
<div class="modal-body">
<?php include("classes/forms/edit.php"); ?>
</div>
</div>
</div>
</div>
【问题讨论】:
表单有动作post
但 php 正在寻找 GET
@charlietfl 那是因为他们也在通过表单进行编辑。至少我是这样做到的。它适用于普通网页,但不适用于模态。
modal 也不例外,它只是将内容放在其他内容之上...最终表单仍然是表单
@charlietfl 那么你认为我会做什么?我尝试将 POST 更改为 GET 但仍然是相同的错误。如果你能给我一个答案,那就太好了
这段代码很难阅读,我的 50 øre 是你有一个逻辑缺陷。添加一些缩进,请更改为 switch(大小写),制作一些功能,go oop 或至少添加缩进和 cmets。如果您想严格使用 if/else if/else 进行编程,请添加关闭 cmets 以便阅读:-)
【参考方案1】:
试试这个.. //编辑链接或按钮。
<a href="#" data-toggle="modal" data-target="#myModal_23" data-formid="<?php echo $id; ?>" class="button yellow" >
<i class="ace-icon fa fa-pencil bigger-130"></i>
</a>
//ajax_edit.php
//modal window
<div class="modal fade " id="myModal_23" role="dialog">
$id = $_REQUEST['id'];
//echo $id;
//code
</div>
//脚本
<script>
$("#myModal_23").on('show.bs.modal', function(event)
var button = $(event.relatedTarget);
var id = button.data('formid');
//alert(id);
$.get('ajax_edit.php?id='+id,
function(data)
$("#myModal_23").html(data);
);
);
</script>
【讨论】:
【参考方案2】:您的编辑按钮没有将 ID 传递给edit.php
,这就是模式未激活的原因。您需要在打开它之前填充编辑模式的内容,而不是在加载它时。使用 include
将加载编辑页面而不向其传递 ID。
重写您的编辑按钮以包含 ID
<button class="btn btn-primary" data-toggle="modal"
data-target="#editModal" data-formid="<?php echo $id; ?>">
Edit <?php echo $id; ?>
</button>
打开模态时加载编辑表单的JS
//jQuery function for loading data
$("#editModal").on('show.bs.modal', function(event)
var button = $(event.relatedTarget);
//bootstrap way of retrieving data-* attributes
//data-formid in this case
var id = button.data('formid');
$.get(yourSiteUrl+'/classes/forms/edit.php?id='+id,
function(data)
$("#editModal .modal-title").html(data);
//Resize the modal to the size of the loaded form.
$("#editModal").modal("handleUpdate");
);
);
Check this tutorial to learn about bootstrap modals
P.S:我知道 OP 现在可能已经解决了这个问题,或者会采取不同的方法,我已经发布了答案,以便它可能对其他人有用。
【讨论】:
以上是关于如何通过模态编辑mysql中的数据的主要内容,如果未能解决你的问题,请参考以下文章
如何编辑html页面中的段落并将其存储在mysql数据库中?
如何通过对vue js中的每个搜索结果使用模态在谷歌地图上显示json数据获得的纬度和经度?