簡體   English   中英

在jQuery對話框中使用AJAX更新MySql

[英]Updating MySql using AJAX in jQuery Dialog Box

我有以下代碼,並且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>

嘗試設置error_reporting(E_ALL)並檢查螢火蟲中的錯誤...

更新引用了一個未定義的變量:

$education_info['name']}'

當時尚未設置,您可能想使用$ _POST ['name']或其他名稱

(您可能還希望將參數綁定到查詢中,因為這樣更容易)

您忘記發布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