Binding dropdown list to mysql database in php

时间:2019-03-17 23:03:31

标签: php mysql

Okay so this is my table 'timings' Table 'timings'

I'm showing this data in a table, and I want the availability to be updated whenever the availability is selected to a different value by the user. This is the code for the table:

<!-- Table for timings-->
<table id="tableCreneaux">
<tr>
<th> Créneau </th>
<th> Disponibilité</th>
</tr>
<?php
  $sql_check = 'SELECT * FROM timings ORDER BY timingOrder';
  $res_check = mysqli_query($conn, $sql_check);
  if(mysqli_num_rows($res_check) > 0){
    while($row =mysqli_fetch_assoc($res_check)){
    $creneau = $row["timing"];
    $statutCreneau = $row["available"];
    echo '<tr>';
    echo '<td>'.$creneau.'</td><td> <select id="statut'.$creneau.'">';
    if($statutCreneau == 1)
    {
      echo '<option value="available"> Disponible </option> <option value="unavailable"> Non disponible </option>  ';

    }
    else
    {
      echo '<option value="unavailable"> Non disponible </option><option value="available"> Disponible </option>   ';

    }
    echo '</select> </td> </tr>';
    }
  }
?>
</table>

How do i bind the database's 'available' value to the value of the dropdown list?

1 个答案:

答案 0 :(得分:0)

1-您应该使用JavaScript代码在更改时更新数据库

2-您的选择应该没有时间{}

HTML:

<select id="select" onchange="update_database()">
while(condition){
<option...>    </option>
}
</select>

AJAX脚本:

<script>
function updatae_database(val)
{
$.ajax({ 
url: 'send.php', // The PHP file that you want to send your user input to
type: 'POST',
data: {data: $('#select').val()}, // The data to be sent
dataType: 'text',
success: function(data) 
{
alert('update successful');
}
});
}
</script>

send.php:

<?php
include('database.php');
$name = $age = $id ="";
if(isset($_GET['data']))
{$data=$_GET['data'];}
$query = "update TABLE set COLUMN='$data'";
$sql = mysqli_query($con,$query);
?>