I have a form where a user can update several data inside the database and one of the fields is using a multiple select. I'm stuck with the selected part, where the system will display back the keyword values from the database in the option. The updating process works fine, the problem is only to display the existing values from the database inside the multiple select options.
<form action="" method="post" enctype="multipart/form-data">
<?php
if(isset($_GET['edit'])) {
$id = $_GET['edit'];
$query = "SELECT * FROM record WHERE id = $id";
$select_id = mysqli_query($con,$query);
while($row = mysqli_fetch_assoc($select_id)){
$keywords = explode (', ', $row['record_keywords']) ;
?>
<label class="control-label">Keywords</label>
<select multiple name="keywords[]" type="next" class="selectpicker" data-style="btn btn-default btn-block">
<?php
$query = "SELECT * FROM key";
$select_categories = mysqli_query($con,$query);
while($row = mysqli_fetch_assoc($select_categories)) {
$kk = $row['key'] == $keywords;
echo '<option '.($kk ? 'selected="selected"' : '').'>' . $row['key'] . '</option>';
} ?>
</select>
<button class="btn btn-warning btn-fill btn-wd btn-finish pull-right" name="update" type="submit">Update</button>
<?php
}//end of while
}//end of if
?>
</form>
Related
I have a PHP form which saves user data to a MySQL database.
I want to show the user which information is held about them and display it in a form in order for them to update or edit the values.
I have a problem in getting the user's saved data from the database in a PHP loop and show that to user in order for them to update or edit it.
Below is the piece of code:
<?php
$id = $_GET['id'];
$conn = mysqli_connect('localhost', 'phpmyadmin', 'Test#2000', 'user');
$sql1 = "SELECT * FROM usr WHERE id='$id'";
$result = mysqli_query($conn, $sql1);
$row = mysqli_fetch_assoc($result);
?>
<fieldset><label>Birthday</label>
<select name="birthday">
<?php
for ($i = 1300; $i <= 1397; $i++) {
echo "<option >$i</option>";
}
?>
</select>
<fieldset>
<button name="btn" type="submit" id="contact-submit">Submit</button>
</fieldset>
</form>
</div>
</body>
</html>
I want to show into the form's Select input the birthday value that user selected originally, in order to edit or update by user.
The <select> children elements <option> does support a selected tag to indicate that it was the selected value, so by adding the selected tag like so <option value='1' selected> you can have that as the selected value.
You'll also want to probably add the $i value into your option element to ensure that the values are being submitted properly.
Mozilla documentation:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select
I have edited your code in this way.
<?php
$id = $_GET['id'];
$conn = mysqli_connect('localhost', 'phpmyadmin', 'Test#2000', 'user');
$sql1 = "SELECT * FROM usr WHERE id='$id'";
$result = mysqli_query($conn, $sql1);
$row = mysqli_fetch_assoc($result);
?>
<fieldset><label>Birthday</label>
<select name="birthday">
<?php for ($i = 1300; $i <= 1397; $i++) {
echo "<option" . (($i == $row['birthdayYear']) ? 'selected="true"' : '') . ">$i</option>";
}
?>
</select>
<fieldset>
<button name="btn" type="submit" id="contact-submit">Submit</button>
</fieldset>
</form>
</div>
</body>
</html>
Hope this helps, thanks.
A.) WITH PDO MODULE
It is best practice today to use prepared statements to avoid SQL injection. This is done through the PDO object.
Set for select the autocomplete="off" attribute, because Firefox apparently has a bug with the selected="selected" that needs this to be set.
See if it is the user's birthday, we can use intval to compare.
<?php
$id = $_GET['id'];
$host = 'localhost';
$username = 'phpmyadmin';
$password = 'Test#2000';
$db_name = 'user';
$conn = new PDO('mysql:host:=' . $host . '; dbname=' . $db_name, $username, $password);
$sql1 = "SELECT * FROM usr WHERE id = :id;";
$stmt = $conn->prepare($sql1);
$stmt->bindParam(':id', $id);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
?>
<fieldset><label>Birthday</label>
<select name="birthday" autocomplete="off">
<?php for ($i = 1300; $i <= 1397; $i++) {
if($i === intval($row['birthdayYear'])){
echo "<option selected='selected'>$i</option>";
} else {
echo "<option>$i</option>";
}
}
?>
</select>
<fieldset>
<button name="btn" type="submit" id="contact-submit">Submit</button>
</fieldset>
</form>
</div>
</body>
</html>
B.) Please at least try option A.), but if it doesn't work:
<?php
$id = $_GET['id'];
$conn = mysqli_connect('localhost', 'phpmyadmin', 'Test#2000', 'user');
$sql1 = "SELECT * FROM usr WHERE id='$id'";
$result = mysqli_query($conn, $sql1);
$row = mysqli_fetch_assoc($result);
?>
<fieldset><label>Birthday</label>
<select name="birthday" autocomplete="off">
<?php for ($i = 1300; $i <= 1397; $i++) {
if($i === intval($row['birthdayYear'])){
echo "<option selected='selected'>$i</option>";
} else {
echo "<option>$i</option>";
}
}
?>
</select>
<fieldset>
<button name="btn" type="submit" id="contact-submit">Submit</button>
</fieldset>
</form>
</div>
</body>
</html>
I want to retain the value that I selected after form submission.
<select name="student[]" class="selectpicker" multiple title="Click here.." data-width="auto" data-live-search="true" required>
<?php
if (isset($_POST['submit'])){
$selected = $_POST['studname'];
$qry = "Select studtbl.stud_id,concat(studtbl.fname,' ',
substring(studtbl.mname, 1,1),'. ',studtbl.lname) as Name from studtbl";
$result = mysqli_query($conn, $qry);
while($row = mysqli_fetch_array($result))
{
extract($row);
echo '<option value="'.$stud_id.'" '.(($stud_id ==
$selected)? 'selected="selected"':"" >'.$Name.'</option>';
}
}else{
$qry = "Select studtbl.stud_id,concat(studtbl.fname,' ',
substring(studtbl.mname, 1,1),'. ',studtbl.lname) as Name from studtbl";
$result = mysqli_query($conn, $qry);
while($row = mysqli_fetch_array($result))
{
extract($row);
echo '<option value="'.$stud_id.'" >'.$Name.'</option>';
}
}
?>
</select>
<input type='submit' class="btn btn-primary" name='submit' value='Create
Account' />
My problem is it is not retaining after submitting
<select name="student[]" class="selectpicker" multiple title="Click here.." data-width="auto" data-live-search="true" required>
<?php
if (isset($_POST['submit'])){
$selected = $_POST['student']; // <---- multi-selects come in as Array
$qry = "Select studtbl.stud_id,concat(studtbl.fname,' ',
substring(studtbl.mname, 1,1),'. ',studtbl.lname) as Name from studtbl";
$result = mysqli_query($conn, $qry);
while($row = mysqli_fetch_array($result))
{
extract($row);
// if the current ID is inside the POST-ed Array - mark as SELECTED
echo '<option value="'.$stud_id.'" '.(in_array($stud_id, $selected) ? 'selected="selected"': "").'>'.$Name.'</option>';
}
}else{
$qry = "Select studtbl.stud_id,concat(studtbl.fname,' ',
substring(studtbl.mname, 1,1),'. ',studtbl.lname) as Name from studtbl";
$result = mysqli_query($conn, $qry);
while($row = mysqli_fetch_array($result))
{
extract($row);
echo '<option value="'.$stud_id.'" >'.$Name.'</option>';
}
}
?>
</select>
<input type='submit' class="btn btn-primary" name='submit' value='Create
Account' />
In my database I have 2 tables:
To insert data, I have a form that populates dropdown options from the table formulation. This is what the insert form for formulation dropdown looks like:
<?php
$formulation = '';
$query = "SELECT * FROM formulation";
$result = mysqli_query($connect, $query);
while ($row = mysqli_fetch_array($result)) {
$formulation .= '<option value="' . $row["formulationID"] . '">' . $row["formulation_name"] . '</option>';
}
?>
<select>
<option value="">Select formulation</option>
<?php echo $formulation; ?>
</select>
Now I am working on the ‘Update’ form. But my question is how can I populate the ‘Formulation’ field dropdown with the data from the formulation table (like as the insert form) but pre-selected with the existing formulation value for the name from the items table? Like this image below:
I am having problem with how I should build the form. How should I proceed with this form?
<?php
$output = array('data' => array());
$sql = "SELECT * FROM items";
$query = $connect->query($sql);
while ($row = $query->fetch_assoc()) {
$output['data'][] = array(
$row['name'],
);
}
echo json_encode($output);
?>
<form action=" " method="POST">
<div>
<label>Name</label>
<input type="text"><br>
<label>Formulation</label>
<select >
<!--What should be the codes here? -->
</select>
</div>
<button type = "submit">Save changes</button>
</form>
Thanks in advance for your suggestion.
Note: I'm not a user of mysqli so maybe there will be some error, but you will get the idea. This will not tackle the update part, just the populate part
Since you are editing a certain item, I will assume that you have something to get the item's itemID.
<?php
$sql = "SELECT * FROM items WHERE itemID = ?";
$query = $connect->prepare($sql);
$query->bind_param("s", $yourItemID);
$query->execute();
$result = $query->fetch_assoc();
$itemName = $result['name'];
$itemFormulation = $result['formulation_fk'];
//now you have the name and the formulation of that certain item
?>
<form action=" " method="POST">
<div>
<label>Name</label>
<input type="text" value="<?php echo $itemName; ?>"><br>
<label>Formulation</label>
<select >
<?php
$query = "SELECT * FROM formulation";
$result = mysqli_query($connect, $query);
while ($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row['formulationID']; ?>" <?php echo ($row['formulationID'] == $itemFormulation) ? 'selected' : ''; ?>>
<?php echo $row['formulation_name']; ?>
</option>
<?php
}
?>
</select>
</div>
<button type = "submit">Save changes</button>
</form>
I changed the code to better suit the problem, there may be typos, just comment for clarification
If I have understand Your question... You have to put Your result into a string. For example:
<?php
$output = array('data' => array());
$sql = "SELECT * FROM items";
$query = $connect->query($sql);
$option = '';
while ($row = $query->fetch_assoc()) {
$name=$row['name'],
$option.='<option value="$name">$name</option>'
}
echo json_encode($output);
?>
<form action=" " method="POST">
<div>
<label>Name</label>
<input type="text"><br>
<label>Formulation</label>
<select >
<?=$option?>
</select>
</div>
<button type = "submit">Save changes</button>
</form>
I hope to be of help
This should do the trick:
<?php
$itemsSql = "SELECT * FROM items WHERE itemId = 5";
$itemQuery = $connect->query($sql);
$item = $itemQuery->fetch_assoc();
$formulationsSql = "SELECT * FROM formulation";
$formulationsQuery = $connect->query($sql);
$formulations = $itemQuery->fetch_assoc();
?>
<form action="updateItem" method="POST">
<div>
<label>Item Name</label>
<input type="text" value="<?= $item[0]['name']; ?>"><br>
<label>Formulation</label>
<select>
<?php foreach($formulations as $formulation){
echo '<option value="'. $formulation['formulationId'].'">' .
$formulation['formulation_name'] . '</option>';
} ?>
</select>
</div>
<button type = "submit">Save changes</button>
</form>
I want create form in html+php. I have got 2 tables in my database.
Categories: id_category, category
Dairy: id_dairy, id_category, product_name
And now is question. How can I create 1 form with 2 select dropdowns. Is it possible to do it in PHP without AJAX?
I mean that - I want choose Category and then choose Products without refresh page.
Code:
<form role="form" action="kalorie.php" method="post">
<div class="form-group">
<label for="kategoria">Kategoria:</label>
<select class="form-control" name="kategoria">
<option>Wybierz kategorie</option>
<?php
$sql_1 = "SELECT * FROM produkty";
$result_1 = mysqli_query($conn, $sql_1);
while($row_1 = mysqli_fetch_assoc($result_1)) {
echo '<option>'.$row_1["produkt"].'</option>';
}
$kat = $_POST['kategoria'];
?>
</select>
<select class="form-control" name="produkt">
<option>Wybierz produkt</option>
<?php
$sql_2 = "SELECT * FROM $kat";
$result_2 = mysqli_query($conn, $sql_2);
while($row_2 = mysqli_fetch_assoc($result_2)) {
echo '<option>'.$row_2["produkty"].'</option>';
}
?>
</select>
</div>
<button type="submit" class="btn btn-default" name="oblicz">Wyślij</button>
</form>
Ajax is best solution for this.
But if you want an alternative than you can use "iframe" and submit ypur form in that frame.
Non Ajax solution:
Load your product subcategories into a JavaScript object keyed by the category id. When the category is changed in the category select list, swap out the options in the product list.
<script>
var produkts = {
<?php
$sql_2 = "SELECT * FROM $kat";
$result_2 = mysqli_query($conn, $sql_2);
$id_category = 0;
list($delim1, $delim2) = array('', '');
while($row_2 = mysqli_fetch_assoc($result_2)) {
$delim2 = '"';
$row_2 = $row['value'];
if ( $id_category != $row_2['id_category'] ) {
$id_category = $row_2['id_category'];
echo $delim1 . $id_category . ': "';
$delim1 = '", ';
}
echo '<option>' . $row_2['produkty'] . '</option>';
}
echo $delim2;
?>
};
// example: {1: "<option>cheese</option><option>butter</option>", 2: "<option>ice cream</option>"}
</script>
<form role="form" action="kalorie.php" method="post">
<div class="form-group">
<label for="kategoria">Kategoria:</label>
<select class="form-control" name="kategoria">
<option>Wybierz kategorie</option>
<option value="1">Me</option>
<option value="2">You</option>
<?php
$sql_1 = "SELECT * FROM produkty";
$result_1 = mysqli_query($conn, $sql_1);
while($row_1 = mysqli_fetch_assoc($result_1)) {
echo '<option>'.$row_1["produkt"].'</option>';
}
$kat = $_POST['kategoria'];
?>
</select>
<select class="form-control" name="produkt">
<option>Wybierz produkt</option>
</select>
</div>
<button type="submit" class="btn btn-default" name="oblicz">Wyślij</button>
</form>
<script>
document.querySelector('select[name=kategoria]').addEventListener('change', function (evt) {
var kat = evt.target;
var kat_sel = kat.options[kat.selectedIndex].value
document.querySelector('select[name=produkt]').innerHTML = produkts[kat_sel];
})
</script>
Mocked up JSFiddle demo.
I want to selected items from mytable when using three tables and &_GET another id to open in this page so i want to use where and where to complete fetch my data by using two roles .
<?php
$sel = "SELECT * FROM `informations` where `cate_id` =".$_GET['info_id'];
$done = mysql_query($sel);
?>
<form action="" method="post" enctype="multipart/form-data">
<label for="location"></label>
<select name="location" id="location"><?php
$sel_cate = "SELECT * FROM locations";
$done_cate = mysql_query($sel_cate);
while($get_cate = mysql_fetch_array($done_cate)){
echo '<option value="'.$get_cate['id'].'">'.$get_cate['location'].'</option>';
$loc=$get_cate['id'];
}
?>
</select>
<input type="submit" name="go" id="go" value="Go">
<input type="submit" name="all" id="all" value="Show All...">
</form>
<?php
if(isset($_POST['go'])){
$sel ='SELECT * FROM `pharmacies` WHERE `cate_id` ="'.$_GET['info_id'].'" || `location_id` = "'.$_POST['location'].'"';
?>
I tried this code and when isset($_POST['go']) variable $sel got $_GET['info_id'] and $_POST['location'] values. Query generated without errors, and must fetch information.
I not see mysql_query in your: if(isset($_POST['go'])). Maybe you forget query:
if(isset($_POST['go']))
{
$sel = 'SELECT * FROM `pharmacies` WHERE `cate_id` ="'.addslashes($_GET['info_id']).'" or `location_id` = "'.addslashes($_POST['location']).'"';
$selRslt = mysql_query($sel);
while($row = mysql_fetch_array($selRslt))
{
var_dump($row);
}
}