简体   繁体   English

在jQuery对话框中使用AJAX更新MySql

[英]Updating MySql using AJAX in jQuery Dialog Box

I have the following code and the SELECT is working correctly to pull in the form data, however, the UPDATE statement is not working. 我有以下代码,并且SELECT可以正常工作以提取表单数据,但是UPDATE语句不起作用。 Any suggestions on what could be corrected to make it work would be greatly appreciated. 任何有关可以对其进行纠正以使其起作用的建议,将不胜感激。

<?php
    include 'includes/config.php';
    require_once('includes/auth.php');

    if ( isset( $_GET['aj'] ) && $_GET['aj'] == '1' ) {
        if ( isset( $_GET['ax']) && $_GET['ax'] == 'education_details' ) {
            $sql = 
<<<SQL
                                        SELECT  * 
                                        FROM    education 
                                        WHERE   id = '{$_GET['rid']}'
                                        ORDER BY date_completed DESC
SQL;
            $sql_result = mysql_query($sql) or die("Get education detail error: " . mysql_error());

            while ( $row = mysql_fetch_assoc($sql_result) ) {
                $education_info = array(
                    'member_id' => $row['member_id'],
                    'name' => $row['name'],
                    'degree' => $row['degree'],
                );
            }

            echo json_encode($education_info);
        }
        exit;
    }

    if ( isset($_POST['aj']) && $_POST['aj'] == '1' ) {
        if (isset( $_POST['ax']) && $_POST['ax'] == 'save' ) {
            $sql=
<<<SQL

                    UPDATE  education
                    SET     name =              '{$education_info['name']}',
                            degree =            '{$education_info['degree']}'
                    WHERE   id =                '{$education_info['rid']}'

SQL;
        }

        // echo json_encode($education_info);
        exit;
    }


?>  
<html>
<head>
    <link type="text/css" href="css/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />    
    <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){

            $('#accordion').accordion({ 
                header: "h3", 
                collapsible: true,
                active: false
            });

            $('#dialog').dialog({
                autoOpen: false,
                open: true,
                modal: true,
                width: 525,
                buttons: {
                    "Cancel": function() { 
                        $(this).dialog("close"); 
                    } 
                }
            });


            $('#dialog_link').click(function() {
                $('#dialog').dialog('open');

            });

            education_details.initialize();
        });
        var education_details = {
            url : '<?= $_SERVER['PHP_SELF']; ?>',
            initialize : function() {
                            $('#dialog_edit').dialog({
                                autoOpen: false,
                                open: true,
                                modal: true,
                                width: 525,
                                buttons: {
                                    "Save" : function() {
                                        $.post("educationie.php", {"aj":"1", "ax":"save" , "rid":$(this).attr("id")}, function(data) {
                                            alert(data);
                                        }, "json");
                                    //  $(this).dialog('close');
                                    },
                                    'Cancel': function() { 
                                        $(this).dialog('close'); 
                                    } 
                                }
                            });
                            $('.edit_open').click(education_details.open);

            },
            open :   function() {
                        $('#dialog_edit').dialog('open');
                        $.get(education_details.url, {"aj":"1","ax":"education_details","rid":$(this).attr("id")}, education_details.education_details_success, "json");
                     },
            education_details_success : function(data) {
                                            $("#dialog_edit input[name=name]").val(data.name);
                                            $("#dialog_edit input[name=degree]").val(data.degree);
                                        }


        };
    </script>

</head>
<body>

<?php
    if ( $_POST['ax'] == "new" ) {

        // echo "DID IT GO HERE"; exit;


        $sql=<<<SQL

                INSERT INTO education (member_id, name, degree)
                VALUES      (
                            '{$_SESSION['SESS_MEMBER_ID']}',  
                            '{$_POST['name']}', 
                            '{$_POST['degree']}'
                            )

SQL;


        if(mysql_query( $sql ) or die ( "Insert failed." . mysql_error()) );

        // echo $sql; exit;


        // header( "Location: education.php");
    }


?>

    <button id="dialog_link" class="ui-button ui-state-default ui-corner-all">Add New</button>
    <br>    

    <div id="dialog" title="Add New" style="display:none;">

        <form action="<?= $_SERVER["PHP_SELF"];?>" method="post">
            <input type="hidden" name="ax" value="new">
        Institution<br><input type="text" name="name" /><br>
        Degree(s) Earned<br><input type="text" name="degree" /><br>
        <input type="submit" name="SUBMIT" value="submit" class="ui-button ui-state-default ui-corner-all"/>

    </div>


    <div id='dialog_edit' title='Edit' style="display:none;">
        <form action="<?= $_SERVER["PHP_SELF"];?>" method="post">
        Institution<br><input type="text" name="name" /><br>
        Degree(s) Earned<br><input type="text" name="degree" /><br>
        </form>

    </div>


    <div id="accordion">

    <?

        $sql = mysql_query("    SELECT  * 
                                FROM    education 
                                WHERE   member_id = '" . $_SESSION['SESS_MEMBER_ID'] . "' 
                                ORDER BY date_completed DESC");




        while($row = mysql_fetch_array($sql)){





            echo    "<h3><a href=#>" . $row['name'] ."</a></h3>";
            echo    "<div> 
                            <table>
                            <tbody>
                                <tr>
                                    <td valign='top'>

                                    <a id=\"" . $row['id'] . "\" class=\"edit_open\" href=\"javascript:void(0)\">Edit</a>

                                    <br> Degree(s): ". $row['degree'] . 
                                    "</td>
                                    <td style='padding-left:85px;'>
                                    </td>
                                </tr>
                            </tbody>
                        </table>    
                    </div>";


        }
        mysql_close($con);

        ?>
    </div>

</body>
</html>

try setting error_reporting(E_ALL) and checking the errors in firebug... 尝试设置error_reporting(E_ALL)并检查萤火虫中的错误...

the update references an undefined variable: 更新引用了一个未定义的变量:

$education_info['name']}'

it wasn't set at that point, you probably want to use $_POST['name'] or something instead 当时尚未设置,您可能想使用$ _POST ['name']或其他名称

(you might want to bind parameters in queries as it's easier as well) (您可能还希望将参数绑定到查询中,因为这样更容易)

You forgot to post the RID = where id 您忘记发布RID =其中的id

        <?php
            include 'includes/config.php';
            require_once('includes/auth.php');

            if ( isset( $_GET['aj'] ) && $_GET['aj'] == '1' ) {
                if ( isset( $_GET['ax']) && $_GET['ax'] == 'education_details' ) {
                    $sql = 
        <<<SQL
                                                SELECT  * 
                                                FROM    education 
                                                WHERE   id = '{$_GET['rid']}'
                                                ORDER BY date_completed DESC
        SQL;
                    $sql_result = mysql_query($sql) or die("Get education detail error: " . mysql_error());

                    while ( $row = mysql_fetch_assoc($sql_result) ) {
                        $education_info = array(
                            'id' => $row['id'],
                            'member_id' => $row['member_id'],
                            'name' => $row['name'],
                            'degree' => $row['degree'],
                        );
                    }

                    echo json_encode($education_info);
                }
                exit;
            }

            if ( isset($_POST['aj']) && $_POST['aj'] == '1' ) {
                if (isset( $_POST['ax']) && $_POST['ax'] == 'save' ) {
                    $sql=
        <<<SQL

                            UPDATE  education
                            SET     name =              '{$_POST['name']}',
                                    degree =            '{$_POST['degree']}'
                            WHERE   id =                '{$_POST['rid']}'

        SQL;
                }
                $sql_result = mysql_query($sql) or die("Get education update error: " . mysql_error());

                // echo json_encode($education_info);
                exit;
            }


        ?>  
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
        <head> 
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css" type="text/css" /> 
        <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script> 
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
            <script type="text/javascript">
                $(document).ready(function(){

                    $('#accordion').accordion({ 
                        header: "h3", 
                        collapsible: true,
                        active: false
                    });

                    $('#dialog').dialog({
                        autoOpen: false,
                        open: true,
                        modal: true,
                        width: 525,
                        buttons: {
                            "Cancel": function() { 
                                $(this).dialog("close"); 
                            } 
                        }
                    });


                    $('#dialog_link').click(function() {
                        $('#dialog').dialog('open');

                    });

                    education_details.initialize();
                });
                var education_details = {
                    url : '<?= $_SERVER['PHP_SELF']; ?>',
                    initialize : function() {
                                    $('#dialog_edit').dialog({
                                        autoOpen: false,
                                        open: true,
                                        modal: true,
                                        width: 525,
                                        buttons: {
                                            "Save" : function() {
                                                $.post("<?= $_SERVER['PHP_SELF']; ?>", {"aj":"1", "ax":"save" , "rid":$("#dialog_edit input[name=rid]").val(), "name":$("#dialog_edit input[name=name]").val(), "degree": $("#dialog_edit input[name=degree]").val()}, function(data) {
                                                    alert(data);
                                                }, "json");
                                               $(this).dialog('close');
                                            },
                                            'Cancel': function() { 
                                                $(this).dialog('close'); 
                                            } 
                                        }
                                    });
                                    $('.edit_open').click(education_details.open);

                    },
                    open :   function() {
                                $('#dialog_edit').dialog('open');
                                $.get(education_details.url, {"aj":"1","ax":"education_details","rid":$(this).attr("id")}, education_details.education_details_success, "json");
                             },
                    education_details_success : function(data) {
                                                    $("#dialog_edit input[name=name]").val(data.name);
                                                    $("#dialog_edit input[name=degree]").val(data.degree);
                                                    $("#dialog_edit input[name=rid]").val(data.id);
                                                }


                };
            </script>

        </head>
        <body>

        <?php
            if (!empty($_POST['ax']) && $_POST['ax'] == "new" ) {

                // echo "DID IT GO HERE"; exit;


                $sql=<<<SQL

                        INSERT INTO education (member_id, name, degree)
                        VALUES      (
                                    '{$_SESSION['SESS_MEMBER_ID']}',  
                                    '{$_POST['name']}', 
                                    '{$_POST['degree']}'
                                    )

        SQL;


                if(mysql_query( $sql ) or die ( "Insert failed." . mysql_error()) );

                 //echo $sql; exit;


                // header( "Location: education.php");
            }


        ?>

            <button id="dialog_link" class="ui-button ui-state-default ui-corner-all">Add New</button>
            <br>    

            <div id="dialog" title="Add New" style="display:none;">

                <form action="<?= $_SERVER["PHP_SELF"];?>" method="post">
                    <input type="hidden" name="ax" value="new">
                Institution<br><input type="text" name="name" /><br>
                Degree(s) Earned<br><input type="text" name="degree" /><br>
                <input type="submit" name="SUBMIT" value="submit" class="ui-button ui-state-default ui-corner-all"/>
                </form>

            </div>


            <div id='dialog_edit' title='Edit' style="display:none;">
                <form action="<?= $_SERVER["PHP_SELF"];?>" method="post">
                <input type="hidden" name="rid">
                Institution<br><input type="text" name="name" /><br>
                Degree(s) Earned<br><input type="text" name="degree" /><br>
                </form>

            </div>


            <div id="accordion">

            <?

                $sql = mysql_query("    SELECT  * 
                                        FROM    education 
                                        WHERE   member_id = '{$_SESSION['SESS_MEMBER_ID']}' 
                                        ORDER BY date_completed DESC");




                while($row = mysql_fetch_array($sql)){





                    echo    "<h3><a href=#>" . $row['name'] ."</a></h3>";
                    echo    "<div> 
                                    <table>
                                    <tbody>
                                        <tr>
                                            <td valign='top'>

                                            <a id=\"" . $row['id'] . "\" class=\"edit_open\" href=\"javascript:void(0)\">Edit</a>

                                            <br> Degree(s): ". $row['degree'] . 
                                            "</td>
                                            <td style='padding-left:85px;'>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>    
                            </div>";


                }
                mysql_close($con);

                ?>
            </div>

        </body>
        </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM