简体   繁体   中英

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.

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']))

For future purposes and references, When doing the above, include the name attribute from your button and not the value attribute

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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