数据无法保存到数据库PHP中

时间:2017-03-26 12:25:32

标签: php mysqli

我遇到这个问题,当我按下Add按钮时,它应该将我选择的选项保存到我的数据库表中。

但是当我按下它时,我的数据库表没有收到任何数据。

我的代码有问题吗?我需要找到一种将数据保存到指定表格的正确方法。

非常感谢任何帮助。谢谢

<?php
    include "..\subjects\connect3.php";
    //echo "Connection successs";

    $query = "SELECT * FROM programmes_list";
    $result = mysqli_query($link, $query);
?>

<form name = "form1" action="dropdownindex.php" method="post">
    <table>
      <tr>
        <td>Select Pragramme</td>
        <td>
          <select id="programmedd" onChange="change_programme()">
            <option>select</option>
            <?php while($row=mysqli_fetch_array($result)) { ?>
            <option value="<?php echo $row["ID"]; ?>"><?php echo $row["programme_name"]; ?></option>
            <?php } ?>
          </select>
        </td>
      </tr>

      <tr>
        <td>Select intake</td>
        <td>
          <div id="intake">
            <select>
              <option>Select</option>
            </select>
          </div>
        </td>
      </tr>

      <tr>
        <td>Select Subjects</td>
        <td>
          <div id="subject">
            <select >
              <option>Select</option>
            </select>
          </div>
        </td>
      </tr>

      <input type="submit" value="Add" name="send">

    </table>
</form>


<?php
    if(isset($_POST['Add'])) {

        //print_r($_POST);
        $course1 = implode(',',$_POST['programmedd']);
        $course2 = implode(',',$_POST['intake']);
        $course3 = implode(',',$_POST['subject']);

        $db->query("INSERT INTO programmes(programme_registered, intake_registered, subjects_registered)
                VALUES (' ".$course1." ',' ".$course2." ', ' ".$course3." ' )");

        echo $db->affected_rows;
    }
?>

<script type="text/javascript">
    function change_programme()
    {
        var xmlhttp=new XMLHttpRequest();
        xmlhttp.open("GET","ajax.php?programme="+document.getElementById("programmedd").value,false);
        xmlhttp.send(null);

        document.getElementById("intake").innerHTML=xmlhttp.responseText;

        if(document.getElementById("programmedd").value=="Select"){

          document.getElementById("subject").innerHTML="<select><option>Select</option></select>";    
        }
    }


    function change_intake()
    {
        var xmlhttp=new XMLHttpRequest();
        xmlhttp.open("GET","ajax.php?intake="+document.getElementById("intakedd").value,false);
        xmlhttp.send(null);

        document.getElementById("subject").innerHTML=xmlhttp.responseText;
    }
</script>


//ajax.php
<?php
    $dbhost = 'localhost' ;
    $username = 'root' ;
    $password = '' ;
    $db = 'programmes' ;

    $link = mysqli_connect("$dbhost", "$username", "$password");

    mysqli_select_db($link, $db);

    if (isset($_GET["programme"])) {
      $programme = $_GET["programme"];
    } else {
      $programme = "";
    }

    if (isset($_GET["intake"])) {
      $intake = $_GET["intake"];
    } else {
      $intake = "";
    }

    if ($programme!="") {
      $res=mysqli_query($link, "select * from intakes where intake_no = $programme");
      echo "<select id='intakedd' onChange='change_intake()'>";
      echo "<option>" ; echo "Select" ; echo "</option>";
      while($value = mysqli_fetch_assoc($res)) {

        echo "<option value=".$value['ID'].">";
        echo $value["intake_list"];
        echo "</option>";
      }   
      echo "</select>";
    }

    if ($intake!="") {
      $res=mysqli_query($link, "select * from subject_list where subject_no = $intake");
      echo "<select>";
      echo "<option>" ; echo "Select" ; echo "</option>";
      while($value = mysqli_fetch_assoc($res)) {

        echo "<option value=".$value['ID'].">";
        echo $value["subjects"];
        echo "</option>";
      }   
      echo "</select>";
    }

?>

1 个答案:

答案 0 :(得分:0)

您的错误是您检查按钮点击的位置。

更改为

如果(isset($ _ POST [&#39;发送&#39;]))

为了将来的目的和参考,在执行上述操作时,请在按钮中包含name属性,而不是值属性