php - use SQL data selected from html dropdown list
Get the solution ↓↓↓NEWBIE ALERT Ive been reading for hours but cannot see how to do what I want. I have an SQL DB with a list of medication and dosages etc. Im trying to create an 'edit' page so i can select the medication from a dropdown list(this appears to work), and edit certain parts of it(size, box quantity, doseage etc.
the problem is i cant get the selected medication item(from dropdown) to become a variable or if neccessary, forward to a 2nd PHP page. Ive left a couple of attempted edits in there but they caused errors (TRIED NOT WORK) As it is, It is loading editmed.php to load my edit page on submit. (not sure if this is the best way of doing it).
Any help would be great thanks
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Editor</title>
<link rel="stylesheet" href="global.css">
</head>
<body>
<div class="header">
<h1>Edit Medication Item</h1>
</div>
<div class="topnav">
<a href="indexhl.php">Home</a>
<a class="active" href="edit.php">Edit Medication</a>
<a href="presc_coll.php">Prescription Collection</a>
<a href="add-new.php">Add New Medication</a>
<a href="pillbox.php">Pillbox refill</a>
<a href="individualtopup.php">Individual refill</a>
</div>
<?php
// server connect
$mysqli = new mysqli("localhost:port", "USERNAME", "PASSWORD", "prescription")
or die ('Cannot connect to db');
// select medication name field from db
echo "<p><b><U><i><p style='color:red'>Select Medication</I></U></p><p style='color:black'></b></p>";
$sqlSelect="select Medication from general";
$result = $mysqli -> query ($sqlSelect);
//$medselect=$result['Medication'];**TRIED NOT WORK**
echo "<form action='editmed.php'>";
echo "<select id=`Medication` name=`medselect`>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['Medication'] . "'>" . $row['Medication'] . "</option>";
}
//$medselect=$result['Medication'];**TRIED NOT WORK**
echo "</select>";
echo "<input type='submit' value='submit'>";
// echo "<input type='submit' value='<$medselect>' name='medselect'/>" ; **TRIED NOT WORK**
echo "</form>" ;
echo "</body>";
echo "</html>";
?>
</body>
</html>
editmed.php >
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Editor</title>
<link rel="stylesheet" href="global.css">
</head>
<body>
<div class="header">
<h1>Edit Medication Item</h1>
</div>
<div class="topnav">
<a href="indexhl.php">Home</a>
<a class="active" href="edit.php">Edit Medication</a>
<a href="presc_coll.php">Prescription Collection</a>
<a href="add-new.php">Add New Medication</a>
<a href="pillbox.php">Pillbox refill</a>
<a href="individualtopup.php">Individual refill</a>
</div>
<?php
// server connect
$link = mysqli_connect("localhost:port", "user", "password",
"prescription")
or die ('Cannot connect to db');
// $sql = "SELECT ID, Medication, dosagedaily, Box_Size, Boxed_remain,
grandtotal, last_collected, Dosage, countdown FROM general where
`Medication` = $medselect";
$sql = "SELECT ID, Medication, dosagedaily, Box_Size, Boxed_remain,
grandtotal, last_collected, Dosage, countdown FROM general where
`Medication` = 'Esomeprazole 40mg'";
// you check sql and link true
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Index</th>";
echo "<th>Medication</th>";
echo "<th>Daily Dosage</th>";
echo "<th>Box Size</th>" ;
echo "<th>Boxed Remaining</th>" ;
echo "<th>Grand Total</th>";
echo "<th>Last presc collection</th>";
echo "<th>Dosage</th>";
echo "<th>Pills Remaining</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Medication'] . "</td>";
echo "<td>" . $row['dosagedaily'] . "</td>";
echo "<td>" . $row['Box_Size'] . "</td>";
echo "<td>" . $row['Boxed_remain'] . "</td>";
echo "<td>" . $row['grandtotal'] . "</td>";
echo "<td>" . $row['last_collected'] . "</td>";
echo "<td>" . $row['Dosage'] . "</td>";
echo "<td>" . $row['countdown'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
}
else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
i dont know how to assign the variables from the input form, thats where my issue is.If i get a variable assigned then i can create the actual editing part.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: sqlstate[hy000] [1698] access denied for user 'root'@'localhost'
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.