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

时间:2010-01-20 04:50:33

标签: php jquery

我有以下代码,并且SELECT正常工作以提取表单数据,但是,UPDATE语句不起作用。任何关于可以纠正什么以使其发挥作用的建议将不胜感激。

<?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>

2 个答案:

答案 0 :(得分:0)

尝试设置error_reporting(E_ALL)并检查firebug中的错误......

更新引用了一个未定义的变量:

$education_info['name']}'

当时没有设置,你可能想要使用$ _POST ['name']或者其他东西

(您可能希望在查询中绑定参数,因为它也更容易)

答案 1 :(得分:0)

您忘记发布RID = where 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>