Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用准备好的语句和PDO更新两个表_Javascript_Php_Jquery_Mysql_Pdo - Fatal编程技术网

Javascript 使用准备好的语句和PDO更新两个表

Javascript 使用准备好的语句和PDO更新两个表,javascript,php,jquery,mysql,pdo,Javascript,Php,Jquery,Mysql,Pdo,我试图更新两个表中的数据,根据下面的代码,只有第一个表“学生信息”得到更新 漏掉“培训信息”表未更新。这两个表由IndexNo链接。我需要代码的指导,告诉我如何使用示例语句同时更新这两个表。 请注意,我尝试使用给定的URL获取id值。这有助于我在一次操作中查看和更新有关IndexNo的一个不同列的数据 URL: http://localhost/sample/updateStudentInfo.php?id=14 updateStudentInfo.php代码如下 &

我试图更新两个表中的数据,根据下面的代码,只有第一个表“学生信息”得到更新 漏掉“培训信息”表未更新。这两个表由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>
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";
        }
    ?>

请添加此
错误报告(E\u ALL);ini设置(“显示错误”,1)显示是否有任何错误。@Akram Fares没有错误,我正在使用firebug。我认为问题在于“IndexNo='$id'”上的第二个更新查询没有被识别。谢谢
       <?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";
        }
    ?>