使用预准备语句和PDO更新两个表

时间:2015-04-03 15:11:37

标签: javascript php jquery mysql pdo

我尝试更新两个表中的数据,并根据下面的代码,只有第一个表格' student_information'正在更新 遗漏了'training_information'表未更新。这两个表由IndexNo链接。我需要有关如何使用我的样本预处理语句一次更新两个表的代码的指导。 请关注我尝试使用给定的URL获取id值。这有助于我在一次操作中查看和更新​​有关IndexNo的一个不同列的数据。

 URL: http://localhost/sample/updateStudentInfo.php?id=14

updateStudentInfo.php代码如下

            <?php
                // Connect to MySQL database
                    function connect(){
                        try{
                            $dbConn = new PDO('mysql:host=localhost; dbname=hpcz', 'root', 'root');
                            $dbConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                            return $dbConn;
                        }
                        catch(PDOException $e){
                            echo $e->getMessage();
                        }
                    }
                $dbh = connect();
                //gets the value of the id from the URL     
                $id = htmlspecialchars($_GET["id"]); 

                $sql = "SELECT 
            student_information.IndexNo,
            student_information.SurName,
            student_information.FirstName,
            student_information.MiddleName,
            grade_obtained.Grade,
            secondary_school.SchoolName

                            FROM student_information

                            LEFT JOIN training_information
                            ON student_information.IndexNo = training_information.IndexNo


                            WHERE student_information.IndexNo='$id'";
                            $result = $dbh->prepare($sql);
                            $result->execute();
                            while ($row = $result->fetch(PDO::FETCH_ASSOC))
                    {

                            //Initializing the id from URL with the IndexNo from the database
                                $id=$row["IndexNo"];

                ?>

            <div><form method="post" >
                <table  width="500" border="1" >
                  <tr>
                    <td colspan="4">Update Student Information</td>
                 </tr>
                 <tr>
                   <td colspan="4"><div align="center"><strong>Applicant Information</strong></div></td>
                 </tr>
                  <tr>
                    <td><div align="right">Sur Name:</div></td>
                    <td><input type="text" name="surname" id="surname" value="<?php echo $row['SurName']; ?>"/></td>
                    <td><div align="right">First Name:</div></td>
                    <td><input type="text"  text-input" name="fname" id="fname" value="<?php echo $row['FirstName']; ?>"/></td>
                  </tr>
                    <tr>
                    <td><div align="right">Middle Name:</div></td>
                    <td><input type="text" name="mname" id="mname" value="<?php echo $row['MiddleName']; ?>"/></td>
                    <td><div align="right">NRC/Passport No:</div></td>
                    <td><input type="text"  name="nrc" id="nrc" value="<?php echo $row['NRCPassportNo']; ?>"/></td>
                  </tr>
                 <tr>
                   <td colspan="4"><div align="center"><strong>Training Information</strong></div></td>
                 </tr> 
                 <tr>
                    <td><div align="right">Secondary School Attended:</div></td>
                    <td><select name="secondarysch" id="secondarysch" >
                    <option selected="selected"><?php echo $row['SchoolName']; ?></option>
                        </select></td>
                     <td><div align="right">Grade Obtained:</div></td>
                    <td><select name="grade" id="grade" >
                    <option selected="selected"><?php echo $row['Grade']; ?></option>
                        </select></td>
                  </tr>

                    <input type="hidden" name="id" id="id" value="<?php echo $row['IndexNo']; ?>"/>
                <tr>
                   <td colspan="4"><div align="center"><button id="update">Update</div></td>
                  </tr>
                  </table>
            <?php }?>       
            </form>

JavaScript代码:

            $(document).ready(function(){
                $("#update").click(function(){
                    /*Applicant Information*/
                    var vid = $("#id").val();
                    var vsurname = $("#surname").val();
                    var vmname = $("#mname").val();
                    var vfname = $("#fname").val();
                    /*Training Information*/
                    var vsecondarysch = $("#secondarysch").val();
                    var vgrade = $("#grade").val();
                    /*Jquery post method */

                    $.post("http://localhost/sample/sendUpdateStudentInfo.php", 
                        {
                            id : vid,
                            surname : vsurname,
                            mname : vmname,
                            fname : vfname,
                            secondarysch :vsecondarysch,
                            grade : vgrade
                        },
                        /*Handles response from server*/
                        function(response){
                            alert(response);
                        });
                    alert("You are here");
                  });
               });

sendUpdateStudentInfo.php代码:

       <?php
        $method = $_SERVER['REQUEST_METHOD'];


        function connect(){
            try{
                $dbConn = new PDO('mysql:host=localhost; dbname=hpcz', 'root', 'root');
                $dbConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                return $dbConn;
            }
            catch(PDOException $e){
                echo $e->getMessage();
            }
        }

        /*Checks if method is HTTP POST*/
        if(strtolower($method) == 'post'){
            /*Applicant Information*/
            $id = addslashes($_POST['id']);
            $surname = addslashes($_POST['surname']);
            $mname = addslashes($_POST['mname']);
            $fname = addslashes($_POST['fname']);

            /*Training Information*/
            $secondarysch = addslashes($_POST['secondarysch']);
            $grade = addslashes($_POST['grade']);

            try {

                $dbHandler = connect();
                $dbHandler->beginTransaction();

                $setInfo = "UPDATE student_information 
                                        SET
                                   SurName = :vSurname,
                                   MiddleName = :vMName,
                                   FirstName = :vFName
                                        WHERE 
                                   IndexNo = '$id'";

                $stmt = $dbHandler->prepare($setInfo);

                $stmt->bindParam(':vSurname', $surname);
                $stmt->bindParam(':vMName', $mname);
                $stmt->bindParam(':vFName', $fname);
                $stmt->execute();
                $stmt->closeCursor();

                $setTrainingInfo = "UPDATE  training_information 
                                                SET 
                                            SecondarySchoolID = :Secondarysch,
                                            GradeObtainedID = :Grade
                                                WHERE 
                                            IndexNo = '$id'";

                $stmt = $dbHandler->prepare($setTrainingInfo);

                $stmt->bindParam(':Secondarysch', $secondarysch);
                $stmt->bindParam(':Grade', $grade);
                $stmt->execute();
                $stmt->closeCursor();

                $dbHandler->commit();

                echo "The Operation was Successful!!!!!!";

            } catch (PDOException $e) {
                $dbHandler->rollback();
                die($e->getMessage());
            }

        }else{
            echo "Oops! Make sure Method is POST";
        }
    ?>

0 个答案:

没有答案