I have a code that brings data idnumber from a database and puts it into a select box.
If I select an idnumber from the select box and press a confirm button how can I write the code to update a column called "confirm" in the database from No to Yes?
The code below is the one for the select box
<select>
<?php
$mysqli = new mysqli("localhost","root","","volunteer");
/* check connection */
if (mysqli_connect_errno()){
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM registrations";
$result1 = mysqli_query($mysqli,$query);
while($rows = mysqli_fetch_assoc($result1)){
echo "<option>" . $rows['idnumber'] . "</option>" ;
}
?>
</select>
<form action="confirm.php" method="post">
<button type="submit" name="confirm-btn">Confirm</button>
</form>
CodePudding user response:
- Put your select input under form tag
- Give name to select box :
- Click on confirm button.
- Write code in : confirm.php
if (!empty($_POST['registrations'])) {
if (!empty($_POST['registrations'])) {
$sql = "UPDATE TABLE_NAME SET COLUMN_NAME=".$_POST['registrations']." where YOUR_CONDITION";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
CodePudding user response:
Form Input:
<form action="confirm.php" method="post">
<select name="idnumber">
<?php
$mysqli = new mysqli("localhost","root","","volunteer");
/* check connection */
if (mysqli_connect_errno()){
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM registrations";
$result1 = mysqli_query($mysqli,$query);
while($rows = mysqli_fetch_assoc($result1)){
echo "<option value=". $rows['idnumber'] .">" . $rows['idnumber'] . "</option>" ;
}
?>
</select>
<button type="submit" name="confirm-btn">Confirm</button>
</form>
Write code in : confirm.php
$mysqli = new mysqli("localhost","root","","volunteer");
/* check connection */
if (mysqli_connect_errno()){
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$sql = "UPDATE TABLE_NAME SET COLUMN_NAME=".$_POST['idnumber']." where YOUR_CONDITION";
if ($conn->query($sql) === TRUE) {
echo "Updated successfully";
} else {
echo "Updating record: " . $conn->error;
}
