php - how do i insert an option from dropdown menu into database
Get the solution ↓↓↓im trying to insert an option from a dropdown menu into a new table in my database called user that has 2 columns id and row1. with this code u get only the index of the selected option in the table for example in the dropdown bar i have
Apples
Oranges
Bananas
If i select Oranges and click insert I will get this is the table
id row1
5 2
i want oranges to show up in the database not just the index. not sure what to do. any help will be appreciated. The code is below and in php. Thanks.
<?php include('index2.php')?>
<?php include('cserver.php')?>
<?php
// php select option value from database
$hostname = "";
$username = "";
$password = "";
$databaseName = "";
// connect to mysql database
$connect = mysqli_connect('', '', '', '');
// mysql select query
$query = "SELECT * FROM `info`";
$result1 = mysqli_query($connect, $query);
if(isset($_REQUEST["submit"]))
{
$row1[0]=$_REQUEST["row1"];
$query ="INSERT INTO user (row1) VALUES ('$row1[0]')";
mysqli_query($db,$query);
// print_r(array_values($row1));
header('location:select.php');
}
?>
<form method="post" enctype="multipart/form-data">
<select name="row1">
<?php while($row1 = mysqli_fetch_array($result1)):;?>
<option value="<?php echo $row1[0];?>"><?php echo $row1[1];?></option>
<?php endwhile;?>
</select>
<br><br>
<input type="submit" value="insert" name="submit">
</form>
Answer
Solution:
Solution Here !!!
//Replace and Work
if(isset($_REQUEST["submit"]))
{
$selected_val = $_POST['row1']; // Storing Selected Value In Variable
$query ="INSERT INTO user (row1) VALUES ('$selected_val')";
mysqli_query($db,$query);
// print_r(array_values($row1));
header('location:select.php');
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: malformed utf-8 characters, possibly incorrectly encoded
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.