简体   繁体   中英

how do i insert an option from dropdown menu into database

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>

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');
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM