stonebergftw Posted August 11, 2010 Share Posted August 11, 2010 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: <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> Quote Link to comment https://forums.phpfreaks.com/topic/210472-dynamic/ Share on other sites More sharing options...
Pikachu2000 Posted August 11, 2010 Share Posted August 11, 2010 If you view the html source, you'll see that all of the <option>'s in the "donor", "year" and "player" <select> fields have the same value . . . Quote Link to comment https://forums.phpfreaks.com/topic/210472-dynamic/#findComment-1098296 Share on other sites More sharing options...
stonebergftw Posted August 12, 2010 Author Share Posted August 12, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/210472-dynamic/#findComment-1098327 Share on other sites More sharing options...
Pikachu2000 Posted August 12, 2010 Share Posted August 12, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/210472-dynamic/#findComment-1098364 Share on other sites More sharing options...
stonebergftw Posted August 12, 2010 Author Share Posted August 12, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/210472-dynamic/#findComment-1098553 Share on other sites More sharing options...
wildteen88 Posted August 12, 2010 Share Posted August 12, 2010 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/210472-dynamic/#findComment-1098633 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.