简体   繁体   English

数据无法保存到数据库PHP

[英]Data not being able to be saved into database PHP

I have this problem here where when I press the Add button, it should save my selected choices into my table in database. 我在这里遇到这个问题,当我按下“ Add按钮时,它将选择的选项保存到数据库的表中。

But when I press it, my table in database did not receive any data. 但是当我按下它时,数据库中的表没有收到任何数据。

Did I do something wrong with my code? 我的代码做错了吗? I need to find a right way to save the data into my designated table. 我需要找到一种将数据保存到指定表中的正确方法。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks 谢谢

<?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>";
    }

?>

Your error is where you check for button click. 您的错误是您检查按钮单击的地方。

Change to this 改成这个

If(isset($_POST['send'])) 如果(isset($ _ POST [ '发送']))

For future purposes and references, When doing the above, include the name attribute from your button and not the value attribute 为了将来使用和参考,在执行上述操作时,请包含按钮的name属性,而不要包含value属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM