Jump to content

Recommended Posts

Okay, here is the URL:

http://www.olevetpossehideout.com/test/populate-list.php

 

As you can see, it is for testing purposes.

 

Now here is the code (which I shortened to save time) I got, forgive me if it is not very tidy:

 

<?php 
$user = "username";  
$host = "localhost"; 
$password = "password"; 
$dbName = "databasename"; 

/* make connection to database */  
mysql_connect($host, $user, $password) OR DIE( "Unable to connect 
to database"); 
mysql_select_db($dbName) or die(mysql_error()); //did you forget this line? 

$sql = "SELECT DISTINCT Name FROM Teams"; 
$result = mysql_query($sql) or die(mysql_error() . "<br>$sql"); //use the or die(...) part ONLY IN DEVELOPMENT 
?> 
 <form action="process.php" method="post" name="vote">
<table border="0" cellpadding="2" cellspacing="0" align="center" width="400">
<tr>
  <td colspan="2" align="center"><b><font size="4">Top 25 poll</font></b><br> Please select all spots. <br>All teams are listed in alphabetical order.<br> No duplicates will be allowed!!!</td>
</tr>
<tr>
  <td>1.</td>
  <td>
  
<select name="one">
<option>Who is #1?</option>  

<?php  

while ($row = mysql_fetch_array($result)) {  
    echo "<option value=\"" . $row['Name'] . "\">" . $row['Name'] . "</option>\n";  
}  
?>
  </td>
</tr>
<tr>
  <td>2.</td>
  <td>
  
<select name="two">  
<option>Choose #2:</option>

<?php  
$sql = "SELECT DISTINCT Name FROM Teams"; 
$result = mysql_query($sql) or die(mysql_error() . "<br>$sql"); //use the or die(...) part ONLY IN DEVELOPMENT

while ($row = mysql_fetch_array($result)) {  
    echo "<option value=\"" . $row['Name'] . "\">" . $row['Name'] . "</option>\n";  
}  
?>
  </td>
</tr> 
  <tr>
  <td>3.</td>
  <td>
  
<select name="three">  
<option>Choose #3:</option>

<?php  

$sql = "SELECT DISTINCT Name FROM Teams"; 
$result = mysql_query($sql) or die(mysql_error() . "<br>$sql"); //use the or die(...) part ONLY IN DEVELOPMENT

while ($row = mysql_fetch_array($result)) {  
    echo "<option value=\"" . $row['Name'] . "\">" . $row['Name'] . "</option>\n";  
}  
?>
  </td>
</tr>
  <tr>
  <td>4.</td>
  <td>
  
<select name="four">  
<option>Choose #4:</option>

<?php  
$sql = "SELECT DISTINCT Name FROM Teams"; 
$result = mysql_query($sql) or die(mysql_error() . "<br>$sql"); //use the or die(...) part ONLY IN DEVELOPMENT

while ($row = mysql_fetch_array($result)) {  
    echo "<option value=\"" . $row['Name'] . "\">" . $row['Name'] . "</option>\n";  
}  
?>
  </td>
</tr>
  <tr>
  <td>5.</td>
  <td>
  
<select name="five">
<option>Choose #5:</option>  

<?php  
$sql = "SELECT DISTINCT Name FROM Teams"; 
$result = mysql_query($sql) or die(mysql_error() . "<br>$sql"); //use the or die(...) part ONLY IN DEVELOPMENT

while ($row = mysql_fetch_array($result)) {  
    echo "<option value=\"" . $row['Name'] . "\">" . $row['Name'] . "</option>\n";  
}  
?>
  </td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr>
  <td colspan="2" align="center">
</select>  
<input type="Submit">  

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

It seems to work fine...but...

 

Where I need help is:

1. Does this appear to be coded correctly for using multiple drop down lists?

2. How do I prevent someone from submitting the same team twice (I thought SELECT DISTINCT would do it, but I think I am wrong)? and...

3. How do I get what is submitted to go to a database?

 

Link to comment
https://forums.phpfreaks.com/topic/145064-solved-form-help-needed/
Share on other sites

sorry, just realised how blunt that was lol.

 

What are you needing help with when you say "How do I get what is submitted to go to a database?"

 

The script looks alright at a glance, everyone has there own way of coding which is what makes it more complicated  ;)

sorry, just realised how blunt that was lol.

 

What are you needing help with when you say "How do I get what is submitted to go to a database?"

 

The script looks alright at a glance, everyone has there own way of coding which is what makes it more complicated  ;)

 

The code I have used to attempt that feat:

<?php
$con = mysql_connect("(database_name).mysql.aplus.net","yourDatabaseUsername","yourDatabasePassword"); //Replace with your actual MySQL DB Username and Password
if (!$con) 
{ 
die('Could not connect: ' . mysql_error()); 
} 
mysql_select_db("yourDatabaseName", $con); //Replace with your MySQL DB Name
$name=mysql_real_escape_string($_POST['name']); //This value has to be the same as in the HTML form file
$email=mysql_real_escape_string($_POST['email']); //This value has to be the same as in the HTML form file
$sql="INSERT INTO form_data (name,email) VALUES ('$name','$email')"; /*form_data is the name of the MySQL table where the form data will be saved.

name and email are the respective table fields*/
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error()); 
} 
echo "The form data was successfully added to your database."; 
mysql_close($con);
?>

 

Of course somethings were changed around...sent to the database, but the DB field was blank when I looked at it.

 

Also, what is a good code to use when I do not want users to repeat their choices? SELECT DISTINCT did not work.

Yeah, that's the right sort of script but where you have $_POST['name'] and $_POST['email'] you'd change it to $_POST['one'] and $_POST['two'] ... etc. (same names as your drop boxes)

 

e.g.;

 

<?php

$firstbox=mysql_real_escape_string($_POST['one']); /*This value has to match the value in the HTML form file*/
$secondbox=mysql_real_escape_string($_POST['two']); /*This value has to match the value in the HTML form file*/
$sql="INSERT INTO form_data (first,second) VALUES ('$firstbox','$secondbox')"; /*with the brackets should match the relevant table fields in your DB and the $'s should match the varibles created above. make sure the values within the brackets are in the same order as each other, otherwise you'll end up with the data in the wrong fields*/

?>

 

Hope that helps

Seeing your using the same result, it would be better to grab that result set (1) time and then use it to fill your form where needed! That's (1) query and (1) loop instead (5) queries and (5) result loops! I'll give you a working example based on what you are doing...

 


<?php

$user     = 'user';

$host     = 'localhost';

$password = 'pass';

$dbName   = 'database';

/* options */

$options = array ( 'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'five' => 5 );

/* make connection to database */

mysql_connect ( $host, $user, $password ) or die ( 'Connection Error: ' . mysql_error () );

mysql_select_db ( $dbName ) or die ( 'Select Database Error: ' . mysql_error () );

$sql = 'SELECT Name FROM Teams GROUP BY Name ORDER BY Name;';

$result = mysql_query ( $sql ) or die ( 'Query Database Error: ' . mysql_error () );

if ( mysql_num_rows ( $result ) > 0 )
{
$select  = "<tr>\n";
$select .= "<td>{2}.</td>\n";
$select .= "<td>\n";
$select .= "<select name='{1}'>\n";
$select .= "<option value=''>Who is #{2}?</option>\n";

while ( $row = mysql_fetch_array ( $result ) )
{
	$select .= "<option value='" . htmlentities ( $row['Name'], ENT_QUOTES ) . "'>" . $row['Name'] . "</option>\n";
}

$select .= "</select>\n";
$select .= "</td>\n";
$select .= "</tr>\n";
?>
<form action='process.php' method='post' name='vote'>
	<table border='0' cellpadding='2' cellspacing='0' align='center' width='400'>
		<tr>
			<td colspan='2' align='center'>
				<b>
					<font size='4'>Top 25 poll</font>
				</b>
				<br>
					Please select all spots.
				<br>
					All teams are listed in alphabetical order.
				<br>
					No duplicates will be allowed!!!
		</td>
	</tr>
<?php
foreach ( $options AS $k => $v )
{
	echo str_replace ( array ( '{1}', '{2}' ), array ( $k, $v ), $select );
}
?>
	<tr>
		<td colspan='2'>
			 
	</td>
	</tr>
	<tr>
		<td colspan='2' align='center'>
			<input type='Submit'> 

		</td>
	</tr> 
</table>
</form>
<?php
}
else
{
echo 'no results';
}
?>

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.