Momenteel heb ik een edit formulier, die op een paar punten na verder prima werkt.
Het formulier heeft een paar input fields waarmee ik gegevens inzet, nadat ze uit DB zijn gehaald een paar drop down lists.
En ik merk dat alle input fields voor de drop down list gevuld worden met gegevens en alle input fields na de drop down list leeg zijn/blijven.
Iemand een idee voor dit vreemd gedrag?
De volgende code gebruik ik voor mijn edit formulier
<?php
require('includes/config.inc.php');
$page_title = 'Overview of all medicines saved to the database';
include('includes/header.php');
// If no user_id session variable exists, redirect the user:
if (!isset($_SESSION['user_id'])) {
$url = BASE_URL . 'index.php'; // Define the URL.
ob_end_clean(); // Delete the buffer.
header("Location: $url");
exit(); // Quit the script.
}
// Need the database connection:
require(MYSQL);
if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form.
//creating veriables for medicinescompany, medicinescountry, medicinetype_id and supplier_id
$medicinescompany_id = $_POST['companyname'];
$medicinescountry_id = $_POST['medicinescountry'];
$medicinetype_id = $_POST['medicinetypename'] ;
$supplier_id = $_POST['suppliername'];
// Trim all the incoming data:
$trimmed = array_map('trim', $_POST);
// Validate and secure the form data:
$errors = array();
// Check for a medicine name:
if (!empty($trimmed['medicinename'])) {
$medicinename = mysqli_real_escape_string ($dbc, $trimmed['medicinename']);
} else {
$errors['medicinename'] = "<p class='error'>Medicine name can't be empty</p>";
}
// Check for a generic name:
if (!empty($trimmed['genericname'])) {
$genericname = mysqli_real_escape_string ($dbc, $trimmed['genericname']);
} else {
$errors['genericname'] = "<p class='error'>Generic name can't be empty</p>";
}
//check for a medicine type name
if(isset($trimmed['medicinetypename']) && $trimmed['medicinetypename'] > 0){
$medicinetypename = $trimmed['medicinetypename'];
} else {
$errors['medicinetypename'] = "<p class='error'>Please select a medicine type name from the list</p>";
}
// Check for a expiration date name:
if (!empty($trimmed['expiredate'])) {
$expiredate = mysqli_real_escape_string ($dbc, $trimmed['expiredate']);
} else {
$errors['expiredate'] = "<p class='error'>Please select a date from the calendar</p>";
}
// Check for quantity:
if (!empty($trimmed['quantity'])) {
$quantity = mysqli_real_escape_string ($dbc, $trimmed['quantity']);
} else {
$errors['quantity'] = "<p class='error'>Quantity can't be empty</p>";
}
$sellingprice = mysqli_real_escape_string ($dbc, $trimmed['sellingprice']);
$purchaseprice = mysqli_real_escape_string ($dbc, $trimmed['purchaseprice']);
//check for medicine companyname
if (!empty($trimmed['companyname'])) {
$medicinecompany = mysqli_real_escape_string ($dbc, $trimmed['companyname']);
} else {
$errors['companyname'] = "<p class='error'>Please select a company name from the list</p>";
}
//check for medicine countryname
if (!empty($trimmed['medicinescountry'])) {
$country = mysqli_real_escape_string ($dbc, $trimmed['medicinescountry']);
} else {
$errors['medicinescountry'] = "<p class='error'>Please select a medicine country from the list</p>";
}
// Check for a suppliername:
if(!empty($trimmed['suppliername'])){
$suppliername = $trimmed['suppliername'];
} else {
$errors['suppliername'] = "<p class='error'>Please select a supplier name from the list</p>";
}
if (count($errors) == 0) {
// Define the query.
$query = "UPDATE medicines SET medicinename='$medicinename', genericname='$genericname', expiredate='$expiredate', quantity='$quantity', sellingprice='$sellingprice', purchaseprice='$purchaseprice' WHERE id={$_POST['id']}";
$r = mysqli_query($dbc, $query); // Execute the query.
// Report on the result:
if (mysqli_affected_rows($dbc) == 1) {
header ('Refresh:5; url=medicines.php');
echo '<p class="addmedtext">This medicine has been updated.</br>
Within 5 seconds you will redirected to Medicines page...
</p>';
include ('includes/footer.php'); // Include the HTML footer.
exit(); // Stop the page.
} else {
echo '<p style="color: red;">Could not update this medicine</p>';
}
}else { // If one of the data tests failed.
echo '<p class="error">Please try again.</p>';
} // No problem!
}
// op basis van een id worden gegevens van de vorige pagina geselecteerd en hieronder in het form weergegeven
if (isset($_GET['id']) && !empty($_GET['id']) && is_numeric($_GET['id'])) {
// Define the query.
$query = "SELECT id, medicinename, genericname, expiredate, quantity, sellingprice, purchaseprice FROM medicines WHERE id='" . mysqli_real_escape_string($dbc, $_GET['id']) . "'";
if ($result = mysqli_query($dbc, $query)) { // Run the query.
$row = mysqli_fetch_array($result);// Retrieve the information.
?>
<form action="med_edit.php?id=<?php echo $_GET['id']; ?>" method="post">
<h2>Medicine Edit Form</h2>
<table>
<input type="hidden" name="id" value="<?php echo htmlentities($row['id']);?>">
<tr>
<td><label for="medicinename"><b>Medicine Name:</b></label></td>
<td><input type="text" name="medicinename" id="medicinename" value="<?php echo htmlentities($row['medicinename']); ?>" /></td>
<td><?php if(isset($errors['medicinename'])) echo $errors['medicinename']; ?></td>
</tr>
<tr>
<td><label for="genericname"><b>Generic Name:</b></label></td>
<td><input type="text" name="genericname" id="genericname" value="<?php echo htmlentities($row['genericname']); ?>" /></td>
<td><?php if(isset($errors['genericname'])) echo $errors['genericname']; ?></td>
</tr>
<tr>
<td><label for="medicinetypename"><b>Select medicine type:</b></label></td>
<td><select name="medicinetypename">
<option value="">---Select---</option>
<?php
require_once (MYSQL);
$query = "SELECT * from medicinestype";
$result = mysqli_query($dbc,$query);
if(!$result){
die ("query failed" . mysqli_error($dbc));
}
while ($row = mysqli_fetch_assoc($result)){
$medicinetype_id = $row['id'];
$medicinetypename = $row['medicinetypename'];
echo "<option value='$medicinetype_id'>{$medicinetypename}</option>";
}
?>
</select></td>
<td><?php if(isset($errors['medicinetypename'])) echo $errors['medicinetypename']; ?></td>
</tr>
<tr>
<td><label for="expiredate"><b>ExpireDate:</b></label></td>
<td><input type="date" name="expiredate" id="expiredate" value="<?php echo htmlentities($row['expiredate']); ?>" /></td>
<td><?php if(isset($errors['expiredate'])) echo $errors['expiredate']; ?></td>
</tr>
<tr>
<td><label for="companyname"><b>This medicine belgons to company:</b></label></td>
<td>
<?php
require_once (MYSQL);
$query = "SELECT * from medicinescompany where userid= '{$_SESSION['user_id']}'";
$result = mysqli_query($dbc,$query);
if(!$result){
die ("query failed" . mysqli_error($dbc));
}
if(mysqli_num_rows($result) == 0){
echo "Before adding any medicine to the database, please add a <a href='medcom_insert.php'>Medicine Company</a>";
} else {?>
<select name="companyname">
<option value="">---Select---</option>
<?php while ($row = mysqli_fetch_assoc($result)){
$medicinescompany_id = $row['id'];
$companyname = $row['companyname'];
echo "<option value='$medicinescompany_id'>{$companyname}</option>";
}
}
?>
</select></td>
<td><?php if(isset($errors['companyname'])) echo $errors['companyname']; ?></td>
</tr>
<tr>
<td><label for="medicinescountry"><b>This medicine is made in country:</b></label></td>
<td>
<?php
$query2 = "SELECT * from medicinescountry where userid= '{$_SESSION['user_id']}'";
$result2 = mysqli_query($dbc,$query2);
if(!$result2){
die ("query failed" . mysqli_error($dbc));
}
if (mysqli_num_rows($result2) == 0){
echo "Before adding any medicine to the database, please add a <a href='medcountry_insert.php'>Medicine Country</a>";
} else { ?>
<select name="medicinescountry">
<option value="">---Select---</option>
<?php while ($row = mysqli_fetch_assoc($result2)){
$medicinescountry_id = $row['id'];
$countryname = $row['countryname'];
echo "<option value='$medicinescountry_id'>{$countryname}</option>";
}
}
?>
</select>
</td>
<td><?php if(isset($errors['medicinescountry'])) echo $errors['medicinescountry']; ?></td>
</tr>
<tr>
<td><label for="quantity"><b>Quantity:</b></label></td>
<td><input type="text" name="quantity" id="quantity" value="<?php echo htmlentities($row['quantity']); ?>" /></td>
<td><?php if(isset($errors['quantity'])) echo $errors['quantity']; ?></td>
</tr>
<tr>
<td><label for="sellingprice"><b>SellingPrice:</b></label></td>
<td><input type="text" name="sellingprice" id="sellingprice" value="<?php echo htmlentities($row['sellingprice']); ?>" /></td>
<td></td>
</tr>
<tr>
<td><label for="purchaseprice"><b>PurchasePrice:</b></label></td>
<td><input type="text" name="purchaseprice" id="purchaseprice" value="<?php echo htmlentities($row['purchaseprice']); ?>" /></td>
<td></td>
</tr>
<tr>
<td><label for="suppliername"><b>This medicine is brought to you by supplier:</b></label></td>
<td>
<?php
$querysupplier = "SELECT * from suppliers where userid= '{$_SESSION['user_id']}'";
$resultsupplier = mysqli_query($dbc,$querysupplier);
if(!$resultsupplier){
die ("query failed" . mysqli_error($dbc));
}
if (mysqli_num_rows($resultsupplier) == 0){
echo "Before adding any medicine to the database, please add a <a href='medsuppliers_insert.php'>Medicine Supplier</a> first!";
} else { ?>
<select name="suppliername">
<option value="">---Select---</option>
<?php while ($row = mysqli_fetch_assoc($resultsupplier)){
$supplier_id = $row['id'];
$suppliername = $row['suppliername'];
echo "<option value='$supplier_id'>{$suppliername}</option>";
}
}
?>
</select></td>
<td><?php if(isset($errors['suppliername'])) echo $errors['suppliername']; ?></td>
</tr>
<tr>
<td></td>
<td colspan="2"><input type="submit" name="submit" value="Update Medicine" class="btn"></td>
</tr>
</table>
</form>
<?php
} else { // Couldn't get the information.
echo '<p style="color: red;">Could not retrieve the medicine ID</p>';
}
}// End of main IF.
mysqli_close($dbc); // Close the connection.
?>
<?php
include "includes/footer.php";
?>