Jump to content

Dynamic <select>


stonebergftw

Recommended Posts

I'm having trouble getting the dynamic data from my <select> menus to write into my MYSQL database.  Can anyone see what I'm doing wrong here?  First post btw :)

 

The output looks like this, which is obviously wrong:  i3dtvr.png

 

<html>
<head> </head>
<link rel="stylesheet" type="text/css" href="./css/newuser.css" />
<body>
<?php session_start();
require 'default.inc.php'; ?>

<?php if (isset($_POST['amount'])): 
	$host = 'localhost';
                $user = 'user';
                $pass = 'password';
                $conn = mysql_connect($host, $user, $pass);
                if (!$conn) {
                        exit('<p>Unable to connect to the database 
server</p>');
                }
                if (!@mysql_select_db('spikesusers')) {
                        exit('<p>Unable to locate the database</p>');
                }
        $locationname = $_POST['donor'];
        $donorid = mysql_query("SELECT id FROM donors WHERE locationname='$locationname'");

$amount = $_POST['amount'];
        $year = $_POST['year'];
        
$type = $_POST['type'];
        $typeid = mysql_query("SELECT id FROM donationtype WHERE type='$type'");

$player = $_POST['player'];
//$playerid = mysql_query("SELECT id FROM players WHERE 

        $sql = "INSERT INTO donations SET donorid='$donorid', amount='$amount', yearofdonation='$year', 
typeid='$typeid'";
mysql_query($sql);
?>
<div class='standard'>
<h1>Donation Management</h1>

<?php
if ($sql) {
                echo "New donation added ";
                echo "<p></p>";
	echo "<a href=managedonations.php>Back to donation management</a>";
                exit();
        } else {
        echo "Error adding new donation";
        echo "<a href=adddonation.php>Try again</a>";
        exit();
        }
        
?></div>
<?php else:
$host = 'localhost';
                $user = 'user';
                $pass = 'pass';
                $conn = mysql_connect($host, $user, $pass);
                if (!$conn) {
                        exit('<p>Unable to connect to the database
server</p>');
                }
                if (!@mysql_select_db('spikesusers')) {
                        exit('<p>Unable to locate the database</p>');
                }
$donor=@mysql_query('SELECT id, locationname FROM donors');
?>
<div class='standard'>
<h1>Donation Management</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <label>Donor:
    <select class="text" name="donor">
<option value=new>Add a new donor...</option>
<?php
while ($donors=mysql_fetch_array($donor)) {
$donorname=$donors['locationname'];
echo "<option value='<?php echo $donorname;?>'>$donorname</option>";
//echo "<option value='hi'>hi</option>";
}
?>
</option>
</select>
</label><br />
<label>Amount:
      <input class="text" type="text" name="amount" class="text" /></label><br />
<label>Year of donation:
      <select class="text" name="year">
<option value='2011'>2011</option>
<option value='2010'>2010</option>
<option value='2009'>2009</option>
      </select>
</label><br />
<?php $player=@mysql_query('SELECT id, firstname, lastname FROM players'); ?>
<label>Player:
<select class="text" name="player">
<option value="player" selected="selected"></option>
<?php
while ($players=mysql_fetch_array($player)) {
$playerfirstname=$players['firstname'];
$playerlastname=$players['lastname'];
echo "<option value=player>$playerfirstname $playerlastname</option>";
}
?>
</select>
</label><br />

<?php $type=@mysql_query('SELECT id, type FROM donationtype'); ?>
<label>Donation type:
<select class="text" name="type">
<?php
while ($types=mysql_fetch_array($type)) {
$donationtype=$types['type'];
echo "<option value=type>$donationtype</option>";
}
?>
</select>
</label><br />

<input type="submit" value="SUBMIT" class="buttons"/>
    <input type="button" name="Cancel" value="CANCEL"
onclick="window.location = 'managedonations.php'" class="buttons"/>
</form>
</div> <?php endif; ?> </body>
</html>

Link to comment
Share on other sites

Oops, that was a mistake, but...

 

Is there something wrong with the following code?  As far as I understand, the value should change with every iteration of the while loop.  Not working though :\

 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <label>Donor:
    <select class="text" name="donor">
<option value=new>Add a new donor...</option>
<?php
while ($donors=mysql_fetch_array($donor)) {
$donorname=$donors['locationname'];
echo "<option value='<?php echo $donorname;?>'>$donorname</option>";
//echo "<option value='hi'>hi</option>";
}
?>
</option>
</select>

Link to comment
Share on other sites

A couple of things, don't use $_SERVER['PHP_SELF'] as a form action. To submit a form to itself, use action="" instead.

There's really no need to assign the result to another variable, you can just echo it directly.

You have an extra <?php echo in  the <option value=

Also, there is an extra </option> closing tag at the end.

As long as the query is returning the expected results, your options should be populated.

 

Should end up looking like this:

<form action="" method="post">

    <label>Donor:

    <select class="text" name="donor">

<option value=new>Add a new donor...</option>

<?php

while ($donors=mysql_fetch_array($donor)) {

    echo "<option value=\"{$donors['donorname']};\">{$donors['donorname']}</option>";

}

?>

</select>

Link to comment
Share on other sites

I fixed the problem with the following code:

 

<?php
$donor=@mysql_query('SELECT id, locationname FROM donors');

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <label>Donor:
    <select class="text" name="donor">
<option value=new>Add a new donor...</option>
<?php
while ($donors=mysql_fetch_array($donor)) {
$donorname=$donors['locationname'];
$donorid=mysql_query("SELECT id FROM donors WHERE locationname='$donorname'");
$donorid=mysql_fetch_array($donorid);
echo "<option value='$donorid[id]'>$donorname</option>";
}
?>

 

I think my code is pretty sloppy though.  Oh well.

Link to comment
Share on other sites

There is no need for that query within your second while loop. You are already fetching the id and locationname from your original query (outside the loop)

$donor=@mysql_query('SELECT id, locationname FROM donors');

 

You can recode the while loop to just

<?php
while (list($id, $name) = mysql_fetch_row($donor))
{
     echo "<option value=\"$id\">$name</option>";
}
?>

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.