Jump to content

Using PHP to Create Dynamic Forms


nshaw
Go to solution Solved by nshaw,

Recommended Posts

I've searched and found numerous examples for creating dynamic forms using PHP and MySQL; however, each seems to be different and I have been unsuccessful with all of them. What I get is an empty pulldown menu instead of the contents from the MySQL table. I've worked this till I'm blue in the face and am now turning to the forums for help.  I'm developing/testing code in NetBeans 8.0.2. Here is my code (name of file is 'domains.php':

<body>
<form method="post" action="">
<select id="domain" name="domain">

<?php

// define connection variables
$DBServer = "localhost"; // server name or IP address
$DBUser = "xxxxxxxxx";
$DBPass = "xxxxxxxxx";
$DBName = "country";
$DBPort = "3306";        // I include the port because I have two instances of MySQL - the other is 3309

// create a connection to mysql
$conn = mysqli_connect ($DBServer, $DBUser, $DBPass, $DBName, $DBPort);

// check to see if a connection was made and, if yes, proceed
if (mysqli_connect_errno()) { // connection failed
     echo "Database connection failed: " . mysqli_connect_error();
}

// the domain query to get all domain names
$domainQuery = "select domains from domains_subdomains_cop";

// run the query -- this works elsewhere to display the contents of a table
$resultD = mysqli_query($conn, $domainQuery) or die ("Query to get data from domain failed: " . mysql_error());

// with the exception of a pulldown menu, the while loop works well to display all records, etc.
// I've seen examples with MYSQLI_ASSOC and without and I've tried both without success - I use it because I

// haven't had any issues elsewhere in my program
while ($row=mysql_fetch_array($resultD, MYSQLI_ASSOC)) {
     $domainName=$row[DOMAINS]; // DOMAINS is the table attribute I'm trying to pull
     echo "<option>
          $domainName  // I accidentely put a ';' here once and that did show up in the pulldown menu
     </option>";
}

?>

</select>
</form>
</body>

In advance, thank you for any help/insight you can provide!!!

Nick.

Link to comment
Share on other sites

you are using a mysql_fetch_array() statement with a mysqli_ database connection and query statement. you cannot mix mysql_ (no i) and mysqli_ (with an i) statements.

 

if you had php's error_reporting set to E_ALL and display_errors set to ON, you would be getting an error at the mysql_fetch_array() statement alerting you to the mismatch.

Link to comment
Share on other sites

Turn on PHP error checking (see my sign.) and you'll see a problem. You start out using myslqi functions but then you do a fetch with MySQL. Can't mix the two different interfaces. Stick with mysqlI.

 

Also - remember that php is CASE_SENSITIVE!! You query for 'domains' but then you try and use an index called DOMAINS - which besides being in the wrong case should also be enclosed in quotes. Also your option tags don't have an 'value=' clauses in them.

Link to comment
Share on other sites

Also

$domainName=$row[DOMAINS];

 

1) in your query you are retrieving "domains" (lowercase)

2) It should be $row['domains'], with quotes around the array index.

 

Further, your <option> is wrong. The format should be:

<option value="this-is-the-value-submitted-with-the-form">Text that is displayed in the dropdown</option>

Link to comment
Share on other sites

  • Solution

Thank you, all! Yes, I was both mixing APIs and tenses (both corrected). It is now working!! Again, thank you!  I pulled pieces of code from various places (obviously wrong). I'm now using only the mysqli APIs and have the APIs bookmarked.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.