Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7612615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:57:32+00:00 2026-05-31T01:57:32+00:00

I’m making a web app to help determine whose turn it is to make

  • 0

I’m making a web app to help determine whose turn it is to make tea in my office, giving me a focus to learn the use of PHP/MySQL. Apologies for newbie ignorance.

For new users signing up I need to populate the user table with their selections from a dropdown, which is itself populated from a separate table. So when a user signs up, I wan them to select the name of their favourite drink from the dropdown/drinks table and I want the ID of that drink saved in the defaultdrink field of the user table. I also understand this should be done using POST, not GET.

Have so far successfully made a form that populates the DB and have made a dropdown populated from the DB – but no success yet in doing both.

The form page is…

   <?php 
require "insert_dropdown.php";
?>

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="insert_ac.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><strong>Sign up to the Tea App</strong></td>
</tr>

<tr>
<td width="71">Name</td>
<td width="6">:</td>
<td width="301"><input name="name" type="text" id="name"></td>
</tr>


<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>

</table>
</form>
</td>
</tr>
</table>

<?php
$dropdown = "<select name='drinkname'>";
while($row = mysql_fetch_assoc($dresult)) {
  $dropdown .= "\r\n<option value='{$row['drinkname']}'>{$row['drinkname']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
?>

The form actions are led by insert_ac.php…

<?php

$host=""; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="tea"; // Database name 
$tbl_name="users"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form 
$name=$_POST['name'];
$pref=$_POST['pref']; // Drink preference


// Insert data into mysql 
$sql="INSERT INTO $tbl_name(name, pref)VALUES('$name', '$pref')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
echo "Successful";
}

else {
echo "ERROR";
}

// close connection 
mysql_close();
?>

And I’m populating the dropdown using insert_dropdown.php…

<?php

$host=""; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 


// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Write out our query.
$dquery = "SELECT drinkname FROM drinks";
// Execute it, or return the error message if there's a problem.
$dresult = mysql_query($dquery) or die(mysql_error());

// if successfully insert data into database, displays message "Successful". 
if($dresult){
echo "Drink Successful";
echo "<BR />";
}

else {
echo "ERROR";
}

// close connection 
mysql_close();
?>

Am I beyond saving?

Cheers,

Alex

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:57:33+00:00Added an answer on May 31, 2026 at 1:57 am

    Do not close mysql connection.
    Or – even better – store the actual db rows into array and use that array to populate drop-down.
    and put your select box inside of the form.

    Well if you want yo learn something useful yet simple

    config.php

    <?php
    $host=""; // Host name 
    $username=""; // Mysql username 
    $password=""; // Mysql password 
    $db_name="tea"; // Database name 
    
    // Connect to server and select database.
    mysql_connect($host, $username, $password); 
    mysql_select_db($db_name);
    
    // A function! greatest invention since wheel.
    function dbgetarr($query){
      $a   = array();
      $res = mysql_query($query);
      if (!$res) {
        trigger_error("dbget: ".mysql_error()." in ".$query);
      } else {
        while($row = mysql_fetch_assoc($res)) $a[]=$row;
      }
      return $a;
    }
    

    main page.

    <?php
    include 'config.php';
    $data = dbGetArr("SELECT drinkname FROM drinks");
    $tpl = 'tea.tpl.php';
    include 'main.tpl.php';
    

    main site template main.tpl.php

    <html>
    <body>
    <?php include $tpl ?>
    </body>
    </html>
    

    tea page template tea.tpl.php

    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
    <tr>
    <td><form name="form1" method="post" action="insert_ac.php">
    <table width="100%" border="0" cellspacing="1" cellpadding="3">
    <tr>
    <td colspan="3"><strong>Sign up to the Tea App</strong></td>
    </tr>
    
    <tr>
    <td width="71">Name</td>
    <td width="6">:</td>
    <td width="301"><input name="name" type="text" id="name"></td>
    </tr>
    <tr>
      <td width="71">Name</td>
      <td width="6">:</td>
      <td width="301"><input name="drink" type="text" id="name">
        <select name="drinkname">
    <?php foreach($data as $row)): ?>
          <option value="<?=$row['drinkname']?>"><?=$row['drinkname']?></option>
    <?php endforeach ?>
        </select>
      </td>
    </tr>
    <tr>
      <td colspan="3" align="center">
        <input type="submit" name="Submit" value="Submit">
      </td>
    </tr>
    </table>
    </form>
    </td>
    </tr>
    </table>
    

    insert_ac.php

    <?php
    include 'config.php';
    $tbl_name="users"; // Table name
    // Get values from form and formatting them as SQL strings
    $name = mysql_real_escape_string($_POST['name']);
    $pref = mysql_real_escape_string($_POST['pref']); // Drink preference
    
    // Insert data into mysql 
    $sql="INSERT INTO `$tbl_name` (name, pref) VALUES('$name', '$pref')";
    $result=mysql_query($sql);
    
    // if successfully insert data into database, displays message "Successful". 
    if($result){
    echo "Successful";
    }else {
    echo "ERROR";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want use html5's new tag to play a wav file (currently only supported
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am writing an app with both english and french support. The app requests

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.