Updating data in MYSQL database with select option values
تبليغيرجى شرح بإيجاز لمإذا تشعر أنك ينبغي الإبلاغ عن هذا السؤال.
It’s worth noting that the users can change their gender in their profile settings if they want too, but when they are registered at first they get the “Unknown” value from this array
$gender = array(“Male”, “Female”, “Other”, “Unknown”);
This array however is created separately from the html and exists in a function that creates users. I don’t know if this is the issue but I now want input from a selection in which whatever they choose shall be updated in the database, but it’s currently not working as the website gets refreshed and nothing is updated.
profile.php
<form action=”<?php echo htmlspecialchars(“includes/updatingData.inc.php”);?>” id =”genderform” method=”post”>
<input type=”hidden” name=”users_id” value=”<?php echo $_SESSION[“userid”] ?>”>
<label for=”gender”>Gender</label> <br>
<select name=”gender” id=”gender”>
<?php
$id = $_SESSION[“userid”];
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$sql = “SELECT gender FROM users WHERE users_id=?;”;
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header(“location: ../home.php?error=sqlerror”);
exit();
}
mysqli_stmt_bind_param($stmt, “s”, $id);
mysqli_stmt_execute($stmt);
$resultData = mysqli_stmt_get_result($stmt);
if(mysqli_num_rows($resultData)){
while($row = mysqli_fetch_array($resultData)){
$realGender = $row[‘gender’];
echo “<option value=’$realGender’ selected>$realGender</option>”;
}
}
mysqli_stmt_close($stmt);
?>
<option value=”[‘Male’]”>Male</option>
<option value=”[‘Female’]”>Female</option>
<option value=”[‘Other’]”>Other</option>
<option value=”[‘Unknown’]”>Unknown</option>
</select>
<button type=”submit” class=”btn cancel” name=”submitgender” id=”savesettings”>Save</button>
<div id=”uname_response” ></div>
</form>
updatingData.inc.php
<?php
include “dbh.inc.php”;
if(isset($_POST[“submitgender”])){
$gender = $_POST[“gender”];
$users_id = $_POST[“users_id”];
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$sql = “UPDATE users SET gender = ? WHERE users_id = ?;”;
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header(“location: ../home.php?error=sqlerror”);
exit();
}
mysqli_stmt_bind_param($stmt, “is”, $users_id, $gender);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
header(“location: ../profile.php”);
echo “<h1> Changes has been made! </h1>”;
exit();
}
أضف إجابة