将表单数据插入到具有复合键-PHP的表中

时间:2016-03-05 17:58:07

标签: php jquery

我有一个包含3个下拉列表的表单,这些列表中的选择需要发布到数据库。该表单将更新名为CourseModule的表。 CourseModule有一个复合主键,由courseID和moduleID组成,引用了Course表和Module表。 Course表具有属性courseID(PK)和courseName。 Module表具有moduleID(PK)和moduleName属性。

我写的php脚本给出了一个错误,表示列数不匹配第1行的值计数。表单已附加。我是否需要在PHP脚本中加入表并插入到每个表中?
Form

我的表格代码如下:

    <?php
    // first we get the modules JOINED with the course

        $sql = 'SELECT * FROM `course` c
        JOIN coursemodule cm ON c.courseID=cm.courseID
        JOIN module m ON m.moduleID=cm.moduleID';
        $result = mysql_query($sql);

        $courses_w_modules = [];
        while ($row = mysql_fetch_array($result)) {

        $courses_w_modules[$row['courseName']][] = $row;
        }

    ?>


    <div class="module"> 
    <form method="post" action="courseManagement.php">
                    <table>
                    <center> <span>Course Management<span></center><P><P><P>
                    <tr>
                    <td>View Courses:     </td>
                         <td> 
                         <select id='courseNameSelect' name='courseName'>
                         <?php

                                    foreach($courses_w_modules as $courseName => $modules) {

                                        $json = json_encode($modules);

                                        echo "<option data-modules='$json' value='" . $courseName ."'>" . $courseName ."</option>";
                                    }
                        ?> 
                        </select>
                    </td>

                        </tr>


                        <tr>
                        <td>
                        Modules in the choosen course
                        </td>
                        <td>
                        <ul id="results">
                        </ul>
                        </td>
                    </tr>


                    <tr>
                    <td>Choose module to add to course: </td>
                         <td> <?php
                            $sql = "SELECT moduleName FROM module";
                            $result = mysql_query($sql);
                            echo "<select name='moduleName1'>";
                                while ($row = mysql_fetch_array($result)) {
                                    echo "<option value='" . $row['moduleName'] ."'>" . $row['moduleName'] ."</option>";
                                    }
                                    echo "</select>";
                        ?> 
                    </td>
                </tr>


                <tr>
                    <td>Choose module to delete from the course: </td>
                         <td> <?php
                            $sql = "SELECT moduleName FROM module";
                            $result = mysql_query($sql);
                            echo "<select name='moduleName2'>";
                                while ($row = mysql_fetch_array($result)) {
                                    echo "<option value='" . $row['moduleName'] ."'>" . $row['moduleName'] ."</option>";
                                    }
                                    echo "</select>";
                        ?> 
                    </td>
                </tr>



                </table><p><br>

                <center>    
                <input type="submit" value="Update">
                </center>
                </form> 


     </div>
     <script>
     $('#courseNameSelect').change(function(){
         var modules = $(this).find(':selected').data('modules');
         console.log(modules);
         var content = "";
         $.each(modules, function(key,value){
             content += "<li>"+value.moduleName+"</li>";
         });
         $("#results").html(content);
     });
     </script>

我处理表单的PHP脚本无效,但如下所示:

    session_start();
$con = mysql_connect($host,$database_username,$database_password);
// Make sure we connected succesfully
if(! $con)
    {
die('Connection Failed'.mysql_error());
    }   

mysql_select_db($database,$con);

if(isset($_POST['courseName'], $_POST['moduleName1'], $_POST['moduleName2']))
{
$course=$_POST['courseName'];
$modAdd=$_POST['moduleName1'];
$modDel=$_POST['moduleName2'];
$course=mysql_real_escape_string($course);
$modAdd=mysql_real_escape_string($modAdd);
$modDel=mysql_real_escape_string($modDel);
$addModQuery="INSERT INTO module VALUES('$course','$modAdd','$modDel')";
$modAdded=mysql_query($addModQuery);
if(!$modAdded)
{
    echo 'Could Not Add or delete Module to Course, Try Again Later.<br>';
    echo mysql_error();
}
    else
        echo 'Module Added Successfully.<br>';
        header( "refresh:5;url=assignCourse.php" );
        return;
        mysql_close($con);  
}       

&GT;

0 个答案:

没有答案