Jump to content

packets

New Members
  • Posts

    5
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

packets's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. @Muddy_Funster Thanks for all the help. I was now able to insert data to mysql. Also, all your recommendation has been implemented and noted. I would not be able to finish and check all the errors without your help. I've also corrected the $sql2 syntax.
  2. I'm now able to insert to mysql. However, username field was not inserted in mysql. It was NULL. test.php <?php function database_connect($select_database) { $resource_link = mysql_connect("localhost", "root", "root"); if (mysql_select_db($select_database, $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 name="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="lock.php">'; //The important bit echo print_dropdown("SELECT username FROM mailbox where status = 'active'", database_connect("users")); //Some other form elements, or just end the form. echo '<input type="submit" name="submit" value="submit"/> </form>'; lock.php <?php // open the connection $conn = mysql_connect("localhost", "root", "root"); // pick the database to use mysql_select_db("users",$conn); // create the SQL statement $sql1 = "SELECT username from mailbox where username = '$_POST[username]'"; $result1 = mysql_query($sql1, $conn) or die(mysql_error()); $number_of_rows = mysql_num_rows($result1); if ( $number_of_rows != 0 ){ print "Account already exists"; $page = "index.html"; header("Refresh: 5; URL=\"" . $page . "\""); } if ( $number_of_rows == 0 ){ $username = mysql_real_escape_string($_POST['username']); $sql2 = "INSERT INTO mailbox values ('','lockuser','','{$username}','',CURRENT_TIMESTAMP(),'','active','')"; // for troubleshooting //$result = mysql_query($sql2, $conn) or die(mysql_error()); // execute the SQL statement if (mysql_query($sql2, $conn)) { echo "Successfully created mailbox"; $page = "index.html"; header("Refresh: 5; URL=\"" . $page . "\""); } else { echo "Failed creating mailbox"; $page = "index.html"; header("Refresh: 5; URL=\"" . $page . "\""); } } ?> Anyone can give a hint?
  3. I think so because when I check the page, I saw all the infos on column username on my dropdown. Some of them has default data. I'm very sorry guys. I'm so noob on this part. I just studied php mysql yesterday and I'm stuck.
  4. @Buddski I change username to name but nothing happens function print_dropdown($query, $link){ $queried = mysql_query($query, $link); $menu = '<select name="username">'; while ($result = mysql_fetch_array($queried)) { $menu .= ' <option value="' . $result['id'] . '">' . $result['username'] . '</option>'; } $menu .= '</select>'; return $menu; } @Ivan Ivković Thanks for the recommendation and the variables/curly bracket.
  5. 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.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.