0

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.

Rick Skeels
  • 513
  • 1
  • 11
  • 30

1 Answers1

2

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>";
?>
Maximus2012
  • 1,799
  • 2
  • 12
  • 15
  • thats great :D how would i have it show the default value for example if the data was 3 how would i show this instead of --select-- i have tried – Rick Skeels Oct 09 '13 at 21:43
  • 1
    you add an if condition inside your while loop and then use selected="selected" attribute for corresponding option: http://www.w3schools.com/tags/att_option_selected.asp – Maximus2012 Oct 09 '13 at 21:45
  • iwas thinking a easy way for this was to echo the value how would i replace --select-- with that? btw thx for the help :) – Rick Skeels Oct 09 '13 at 21:55