如何通过模式编辑在MySQL数据 [英] How to edit data in mysql through a modal

查看:209
本文介绍了如何通过模式编辑在MySQL数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直有这个code错误。我是新来引导,用模态的东西。我曾经逃跑子窗体为我所有的'花哨'的形式,但现在我正在朝模态的举动。这是我的$ C $下抓住情态动词:

 <按钮类=BTN BTN-主数据切换=莫代尔数据目标=#newmemberModal>添加新的成员 - 其中,/按钮>

< BR />
< BR />

< PHP
    $结果= mysql_query(SELECT * FROM`players` ORDER BY`players`.`user` ASC)
        或死亡(mysql_error());

    回声<类股利='表响应'><表类='表'>中;
    回声< TR><第i个用户名和LT; /第i个百分位>排名< /第i个百分位>位置< /第i个百分位>原则声明< /第i个百分位>标签< /第i ;百分位> AIT< /第i个百分位>服务条纹< /第i个百分位>注< /第i个百分位>< /第i个百分位>< /第i个百分位&GT ;&所述; /第i;&所述; / TR>中;
    而($行= mysql_fetch_array($结果)){

        回声< TR>中;
        回声'< TD> 。 $行[用户。 '< / TD>';
        回声'< TD> 。 $行['级别'。 '< / TD>';
        回声'< TD> 。 $行[位置。 '< / TD>';
        回声'< TD> 。 $行['日期'。 '< / TD>';
        回声'< TD> 。 $行[标签。 '< / TD>';
        回声'< TD> 。 $行[美国在台协会。 '< / TD>';
        回声'< TD> 。 $行[SS。 '< / TD>';
        回声'< TD> 。 $行['笔记']。 '< / TD>';
        回声'< TD><按钮类=BTN BTN默认数据切换=莫代尔数据目标=#editModal>编辑< /按钮>< / TD>';
        回声'< TD>< A HREF =?类/表格/ delete.php的id ='$行['身份证'。'>删除< / A>< / TD>';
        回声'< TD>< A HREF =JavaScript的:无效(0); NAME =我的窗口名称称号=我在这里的称号的onClick =的window.open(dd.php?ID ='。$行['身份证'。',Ratting,宽度= 350,高度= 550,0,状态= 0);> DD< / A>< BR />< A HREF =JavaScript的:无效(0); NAME =我的窗口名称称号=我在这里的称号的onClick =的window.open(hd.php?ID ='。$行['身份证'。',Ratting,宽度= 350,高度= 550,0,状态= 0);>高清&所述; / a取代;&所述; / TD>';
        回声< / TR>中;
    }
    回声< /表>< / DIV>中;

?>
 

下面是我的edit.php的:

 < PHP include_once(类/ check.class.php'); ?>
    < PHP
     功能renderForm($ ID,$用户,$军衔,$位置,$日期,$标记,$ AIT,$ SS,$票据,$错误)
     {
     ?>

      < PHP
     如果($错误!='')
     {
     回声'< D​​IV的风格=填充:4PX;边框:1px的固体红色​​;颜色:红色;>'$错误< / DIV>';。
     }
     ?>
    ?&所述; PHP的若(protectThis(1,2)):>
     <中心><形式的行动=的方法=邮报>
     <输入类型=隐藏名称=ID值=< PHP的echo $ ID;?>/>
     < D​​IV>
     <强>用户名:​​< / STRONG> < BR /><输入类=TB5类型=文本名称=用户的价值=<?PHP的回声$用户;>中/>< BR />< BR />
     <强>等级:*当前为< / STRONG> <?PHP的echo $排名; ?> < BR />
     <选择名称=等级>
     <期权价值=招<如果($排名=='招')回声选择的PHP; ?>>待定排名< /选项>
     <期权价值=官<如果PHP($排名=='官')回声选择; ?>> PVT< /选项>
     <期权价值=领头羊<回声选择的PHP如果($排名=='领导者'); ?>> PV2< /选项>
    < /选择>< BR />< BR />

     <强>位置:其中; / STRONG> < BR /><输入类=TB5类型=文本名称=位置值=< PHP的echo $位置;>中/>< BR />< BR />
     <强>日期:*< / STRONG> < BR /><输入类=TB5类型=文本名称=日期值=< PHP的echo $日期;?>中/>< BR />< BR />
     <强>标签:*< / STRONG> < BR /><输入类=TB5类型=文本名称=标签值=< PHP的echo $标记;>中/>< BR />< BR />
     <强> AIT的:其中; / STRONG> < BR /><输入类=TB5类型=文本名称=AIT值=< PHP的echo $ AIT;>中/>< BR />< BR />
     <强>服务条纹:LT; / STRONG>< BR /> <输入类=TB5类型=文本名称=SS值=< PHP的echo $ SS;>中/>< BR />< BR />
     <强>注:其中; / STRONG>< BR /> <输入类=TB5类型=文本名称=音符值=< PHP的echo $笔记;>中/>< BR />< BR />
     <输入类=BTN式类型=提交名称=提交值=提交>< /中心>

     < / DIV>
     < /形式GT;
    ?&所述; PHP的elseif的(protectThis(6)):>?
      <中心><形式的行动=的方法=邮报>
     <输入类型=隐藏名称=ID值=< PHP的echo $ ID;?>/>
     < D​​IV>
     <强>用户名:​​< / STRONG> < BR /><输入类=TB5TYPE =测试NAME =用户的价值=<?PHP的回声$用户;>中禁用/>< BR />< BR />
     <强>等级:*当前为< / STRONG> <?PHP的echo $排名; ?> < BR />
     <选择名称=等级>
     <期权价值=招<如果($排名=='招')回声选择的PHP; ?>> PVT< /选项>
     <期权价值=官<如果PHP($排名=='官')回声选择; ?>> PV2< /选项>

    < /选择>< BR />< BR />

     <强>位置:其中; / STRONG> < BR /><输入类=TB5类型=隐藏名称=位置值=< PHP的echo $位置;>中/>< BR />< BR />
     <强>日期:*< / STRONG> < BR /><输入类=TB5类型=文本名称=日期值=< PHP的echo $日期;?>中/>< BR />< BR />
     <强>标签:*< / STRONG> < BR /><输入类=TB5类型=文本名称=标签值=< PHP的echo $标记;>中/>< BR />< BR />
     <强> AIT的:其中; / STRONG> < BR /><输入类=TB5类型=文本名称=AIT值=< PHP的echo $ AIT;>中/>< BR />< BR />
     <强>服务条纹:LT; / STRONG>< BR /> <输入类=TB5类型=隐藏名称=SS值=< PHP的echo $ SS;>中/>< BR />< BR />
     <强>注:其中; / STRONG>< BR /> <输入类=TB5类型=隐藏名称=音符值=< PHP的echo $笔记;>中/>< BR />< BR />
     <输入类=BTN式类型=提交名称=提交值=提交>< /中心>

     < / DIV>
     < /形式GT;
        < PHP ENDIF; ?>
    < PHP
     }
     包括(连接-db.php中');

     如果(使用isset($ _ POST [提交]))
     {
     如果(is_numeric($ _ POST ['身份证']))
     {
     的$ id = $ _ POST ['身份证'];
     $ USER = mysql_real_escape_string(用htmlspecialchars($ _ POST ['用户']));
     $排名= mysql_real_escape_string(用htmlspecialchars($ _ POST ['排名']));
     $位置= mysql_real_escape_string(用htmlspecialchars($ _ POST ['位置']));
     $日期= mysql_real_escape_string(用htmlspecialchars($ _ POST ['日']));
     $标记= mysql_real_escape_string(用htmlspecialchars($ _ POST ['标签']));
     $ AIT = mysql_real_escape_string(用htmlspecialchars($ _ POST ['AIT']));
     $ SS = mysql_real_escape_string(用htmlspecialchars($ _ POST ['SS']));
     $票据= mysql_real_escape_string(用htmlspecialchars($ _ POST ['笔记']));

     如果($用户==''|| $排名==''|| $日期==''|| $标签=='')
     {
     $错误='错误:请填写所有必填字段!;

     @renderForm($ ID,$用户,$军衔,$位置,$日期,$标记,$ AIT,$ SS,$票据,$错误);
     }
     其他
     {

     请求mysql_query(更新播放器设置用户='$用户',等级='$排名,位置=$位置,日期='$日期,标签='$标签',AIT ='$ AIT',SS = $ SS,指出='$笔记'WHERE ID ='的$ id')
     或死亡(mysql_error());


    标题(位置:home.php);
     }
     }
     其他
     {

     回声错误!;
     }
     }
     其他

 {


 如果(使用isset($ _ GET ['身份证'])及和放大器; is_numeric($ _ GET ['身份证'])及和放大器; $ _GET ['身份证'] 0)
 {
 的$ id = $ _GET ['身份证'];
 $结果= mysql_query(SELECT * FROM球员WHERE ID = $ ID)
 或死亡(mysql_error());
 $行= mysql_fetch_array($结果);

 如果($行)
 {
 的$ id = $行['身份证'];
 $ USER = $行['用户'];
 $排名= $行['排名'];
 $位置= $行['位置'];
 $日期= $行['日期'];
 $标签= $行['标签'];
 $ AIT = $行[美国在台协会];
 $ SS = $行['SS'];
 $票据= $行['笔记'];

 @renderForm($ ID,$用户,$军衔,$位置,$日期,$标记,$ AIT,$ SS,$笔记,'');
 }
 其他
 {
 回声没有结果!;
 }
 }
 其他
 {
 回声错误!;
 }
 }
?>
 

现在的问题是我一直让我的错误!在模态对话框消息,而不是别的。我想要做的目标是有它从储存在我的数据库中的ID拉起数据。我认为这个问题还真是,它不是抓住ID当我点击用户编辑。坦率地说,我不知道我怎么会能够通过一个模式来编辑ID。帮助将非常AP preciated。谢谢

奖励:我无法打开两个情态动词在同一页上。如果我有两个在同一页上没有人出现:

 <! - 情态动词 - >

< D​​IV CLASS =模式变脸ID =newmemberModal的tabindex = -  1角色=对话框中的咏叹调 -  labelledby =myModalLabelARIA隐藏=真>
    < D​​IV CLASS =模式 - 对话>
        < D​​IV CLASS =模式的内容>
            < D​​IV CLASS =模式的首部>
                <按钮式=按钮级=关闭数据解雇=莫代尔ARIA隐藏=真>&安培;倍;< /按钮>
                < H4类=模式的标题>添加新成员< / H4>
            < / DIV>

            < D​​IV CLASS =模态体>
            <?PHP的包括:(类/表格/ new.php); ?>

            < / DIV>
        < / DIV>
    < / DIV>
< / DIV>

< D​​IV CLASS =模式变脸ID =editModal的tabindex = -  1角色=对话框中的咏叹调 -  labelledby =myModalLabelARIA隐藏=真>
    < D​​IV CLASS =模式 - 对话>
        < D​​IV CLASS =模式的内容>
            < D​​IV CLASS =模式的首部>
                <按钮式=按钮级=关闭数据解雇=莫代尔ARIA隐藏=真>&安培;倍;< /按钮>
                < H4类=模式的标题>编辑< / H4>
            < / DIV>

            < D​​IV CLASS =模态体>
            <?PHP的包括:(类/表格/ edit.php的); ?>

            < / DIV>
        < / DIV>
    < / DIV>
< / DIV>
 

解决方案

您编辑按钮不会将ID传递给 edit.php的这就是为什么模态没有被激活。你需要打开它,而不是同时加载之前被填充编辑模式的内容。使用包括将加载编辑页面没有通过一个ID给它。

重写你的编辑按钮包含ID

 <按钮类=BTN BTN-主数据切换=模式
数据目标=#editModal数据formid =< PHP的echo $ ID;>中>
    编辑<?PHP的echo $ ID; ?>
< /按钮>
 

JS加载编辑形式,而打开模态

  // jQuery函数加载数据
$(#editModal)。在('show.bs.modal',函数(事件){
    VAR按钮= $(event.relatedTarget);
    检索数据 -  *属性//引导方式
    //数据formid在这种情况下
    变种的id = button.data('formid');
    $获得(yourSiteUrl +'/班/表格/ edit.php的?ID ='+ ID,
    功能(数据){
        $(#editModal .modal式标题)的HTML(数据)。
        //调整模态所加载的形式的大小。
        $(#editModal)模式(handleUpdate)。
    });
});
 

检查本教程学习引导模态

PS:我知道OP可能已经通过,现在解决了这个问题,或者将已经采取了不同的方法,我已经发布了答案,所以,这可能是有用的人。

I've been having errors with this code. I'm new to bootstrap and using modals for things. I used to run off child forms for all my 'fancy' forms but now I'm doing a move towards modals. This is my code for grabbing the modals:

<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>";

?>

Here's my 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!';
 }
 }
?>

The problem is I keep getting my 'Error!' message in the modal box and not anything else. The goal I'm trying to do is have it pull up data from an ID that's stored in my database. I think the issue really is that it's not grabbing the ID when I click edit on the user. Quite frankly, I don't know how I would be able to edit an ID through a modal. Help would be much appreciated. Thanks

Bonus: I can't open two modals on the same page. If i have two on the same page none of them show up:

<!--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">&times;</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">&times;</button>
                <h4 class="modal-title">Edit</h4>
            </div>

            <div class="modal-body">
            <?php include("classes/forms/edit.php"); ?>

            </div>
        </div>
    </div>
</div>

解决方案

Your edit button doesn't pass the ID to edit.php which is the reason why the modal is not activated. You need is populating the content of the edit modal before opening it and not while loading it. Using include will load the edit page without passing an ID to it.

Rewrite your edit button to contain ID

<button class="btn btn-primary" data-toggle="modal" 
data-target="#editModal" data-formid="<?php echo $id; ?>">
    Edit <?php echo $id; ?>
</button>

JS for loading edit form while opening the modal

//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: I know OP might've solved this issue by now or would've taken a different approach, I have posted the answer so that it might be useful to others.

这篇关于如何通过模式编辑在MySQL数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆