简体   繁体   中英

displaying data from a table into a drop down menu

i have 2 tables

domains_info and tb2

i have got the form working well and entering data into the database

here is the top of my page

  <?php
$action = isset($_POST['action']) ? $_POST['action'] : "";

if($action=='create'){
//include database connection
include 'db_connect.php';

//write query
$query = "insert into domains_info 
set
domain = '".$mysqli->real_escape_string($_POST['domain'])."', 
domain_account = '".$mysqli->real_escape_string($_POST['domain_account'])."',
renew_date  = '".$mysqli->real_escape_string($_POST['renew_date'])."'";

if( $mysqli->query($query) ) {
//if saving success
header("Location:domains.php");
}else{
echo "Database Error: Unable to create record.";
}
$mysqli->close();
}

here is the form

<select id="domain_account" name="domain_account" class="txtBox">
<option value="">-select-</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>                 
</select>

i tried and changed the top of my page like this

<?php
$action = isset($_POST['action']) ? $_POST['action'] : "";

if($action=='create'){
//include database connection
include 'db_connect.php';

//write query
$query = "insert into domains_info 
set
domain = '".$mysqli->real_escape_string($_POST['domain'])."', 
domain_account = '".$mysqli->real_escape_string($_POST['domain_account'])."',
renew_date  = '".$mysqli->real_escape_string($_POST['renew_date'])."'";

if( $mysqli->query($query) ) {
//if saving success
header("Location:domains.php");
}else{
echo "Database Error: Unable to create record.";
}
$mysqli->close();
}

$query = "select id, data 
                        from tb2
                        where id='".$mysqli->real_escape_string($_REQUEST['id'])."'
                        limit 0,1";

$result = $mysqli->query( $query );
$row = $result->fetch_assoc();

$id = $row['id'];
$data = $row['data'];

and updated my form as this

<select id="domain_account" name="domain_account" class="txtBox">
<option value="">-select-</option>
<option value="<?php echo$data; ?>"><?php echo$data; ?></option>    
</select>

as you can tell i am very new to this and its not working.

sorry i didnt explain what i was trying to achieve, i am trying to display a drop down form with data from a database.

Try this for populating the dropdown using database:

<?php
$query = "select id, data from tb2";
$result = $mysqli->query( $query );

echo '<select id="domain_account" name="domain_account" class="txtBox">';
echo '<option value="">-select-</option>';
while ($row = $result->fetch_assoc()){
?>
    <option value="<?php echo $row['data']; ?>"><?php echo $row['data']; ?></option>
<?php    
}
echo "</select>";
?>

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