I'm a newbie on php. I'm really a system administrator and I was just task to do this simple task. For me its hard but I'm sure for a programmer this is very simple. My agenda is to pull out data on one of my column in mysql, select it and dump it on mysql. Here is the php for retrieving mysql data
<?php
function database_connect($users)
{
$resource_link = mysql_connect("localhost", "root", "root");
if (mysql_select_db($users, $resource_link)) {
return $resource_link;
} else {
echo "Cannot connect to DB";
return false;
}
}
function print_dropdown($query, $link){
$queried = mysql_query($query, $link);
$menu = '<select username="username">';
while ($result = mysql_fetch_array($queried)) {
$menu .= '
<option value="' . $result['id'] . '">' . $result['username'] . '</option>';
}
$menu .= '</select>';
return $menu;
}
//Some other form elements, or just start a form.
echo '<form method="post" action="create2.php">';
//The important bit
echo print_dropdown("SELECT username FROM mailbox;", database_connect("users"));
//Some other form elements, or just end the form.
echo '<input type="submit" name="submit" value="submit"/></form>';
Here is the content of my create2.php. This is the php page who do the insert on my mysql.
<?php
// open the connection
$conn = mysql_connect("localhost", "root", "root");
// pick the database to use
mysql_select_db("users",$conn);
// create the SQL statement
$sql2 = "INSERT INTO mailbox values ('','locked','','$_POST[username]','',NOW(),'','locked','')";
// for troubleshooting
$result = mysql_query($sql2, $conn) or die(mysql_error());
// execute the SQL statement
//if (mysql_query($sql2, $conn)) {
// echo "Success";
//} else {
// echo "Fail";
//}
}
?>
When I click the submit button, I don't see any record being inserted on my table. I'm using the create2.php on my other page though it is only an insert/fill up form not like this one that I need to pull up the date, select and insert to mysql.