php drop down to pick user in database

  • UniquelyYoursPC
  • Web Master
  • Web Master
  • User avatar
  • Posts: 2997
  • Loc: Canada "A"

Post 3+ Months Ago

i have a database with users and i want to be able to have a drop down menu to pick the user and hit submit and the data entered by that name (such as money they spent on food gas cloths and such) then under that a form to submit more data
i am just testing this on wampserver so that's why mysql log in has no pass..
this is what i have to take info
Code: [ Select ]
<?php

if (isset($_POST['submit'])):
$dbcnx = mysql_connect('localhost','root','');
mysql_select_db('money');

$name = $_POST['name'];
$gas = $_POST['gas'];
$food = $_POST['food'];
$other = $_POST['other'];
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];     
$sql = "INSERT INTO info SET name='$name', gas='$gas', food='$food', other='$other', day='$day', month='$month', year='$year'";
        
if (@mysql_query($sql)) {
    echo('<p>New moneys added</p>');
} else {
    echo('<p>Error adding new moneys: ' . mysql_error() . '</p>');
}
?>

<p><a href="index.php">Add another bill</a></p>

<?php
else: // Allow the user to enter a new author
?>

<form action="index.php" method="post">
<p>Enter the new bill:<br />
Name: <input type="text" name="name" size="20" maxlength="255"/><br />
date:day <input type="text" name="day" size="2" maxlength="2"/> month <input type="text" name="month" size="2" maxlength="2"/> year <input type="text" name="year" size="4" maxlength="4"/><br />
gas: <input type="text" name="gas" size="20" maxlength="255"/><br />
food: <input type="text" name="food" size="20" maxlength="255"<br />
other: <input type="text" name="other" size="20" maxlength="255"/><br />
<input type="submit" name="submit" value="SUBMIT" /></p>
</form>

<?php endif; ?>
  1. <?php
  2. if (isset($_POST['submit'])):
  3. $dbcnx = mysql_connect('localhost','root','');
  4. mysql_select_db('money');
  5. $name = $_POST['name'];
  6. $gas = $_POST['gas'];
  7. $food = $_POST['food'];
  8. $other = $_POST['other'];
  9. $day = $_POST['day'];
  10. $month = $_POST['month'];
  11. $year = $_POST['year'];     
  12. $sql = "INSERT INTO info SET name='$name', gas='$gas', food='$food', other='$other', day='$day', month='$month', year='$year'";
  13.         
  14. if (@mysql_query($sql)) {
  15.     echo('<p>New moneys added</p>');
  16. } else {
  17.     echo('<p>Error adding new moneys: ' . mysql_error() . '</p>');
  18. }
  19. ?>
  20. <p><a href="index.php">Add another bill</a></p>
  21. <?php
  22. else: // Allow the user to enter a new author
  23. ?>
  24. <form action="index.php" method="post">
  25. <p>Enter the new bill:<br />
  26. Name: <input type="text" name="name" size="20" maxlength="255"/><br />
  27. date:day <input type="text" name="day" size="2" maxlength="2"/> month <input type="text" name="month" size="2" maxlength="2"/> year <input type="text" name="year" size="4" maxlength="4"/><br />
  28. gas: <input type="text" name="gas" size="20" maxlength="255"/><br />
  29. food: <input type="text" name="food" size="20" maxlength="255"<br />
  30. other: <input type="text" name="other" size="20" maxlength="255"/><br />
  31. <input type="submit" name="submit" value="SUBMIT" /></p>
  32. </form>
  33. <?php endif; ?>


this is what i have to disply the data but it is all data i dont know how to sort by name.
Code: [ Select ]
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("money") or die(mysql_error());
$result = mysql_query("SELECT * FROM info");
while($row = mysql_fetch_assoc($result)){
echo "id: ".$row['id'].",
<br/>
name:".$row['name'].",
<br/>
date:".$row['date'].",
<br/>
gas: ".$row['gas'].",
<br/>
food: ".$row['food'].",
<br/>
other:".$row['other']."
<br/><br/><br/>";    
}
mysql_close();
?>
  1. <?php
  2. mysql_connect("localhost", "root", "") or die(mysql_error());
  3. mysql_select_db("money") or die(mysql_error());
  4. $result = mysql_query("SELECT * FROM info");
  5. while($row = mysql_fetch_assoc($result)){
  6. echo "id: ".$row['id'].",
  7. <br/>
  8. name:".$row['name'].",
  9. <br/>
  10. date:".$row['date'].",
  11. <br/>
  12. gas: ".$row['gas'].",
  13. <br/>
  14. food: ".$row['food'].",
  15. <br/>
  16. other:".$row['other']."
  17. <br/><br/><br/>";    
  18. }
  19. mysql_close();
  20. ?>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

To sort a set of replies by name, you could use the ORDER BY clause.
SQL Code: [ Select ]
SELECT * FROM info ORDER BY name ASC

ASC = Ascending
DESC = Descending

I may not be understanding your question here though.

Although to make it easier for you when you are echoing the results, you could do something like the following.
PHP Code: [ Select ]
echo "id: {$row['id']},
<br/>
name:{$row['name']},
<br/>
date:{$row['date']},
<br/>
gas: {$row['gas']},
<br/>
food: {$row['food']},
<br/>
other:{$row['other']}
<br/><br/><br/>";
  1. echo "id: {$row['id']},
  2. <br/>
  3. name:{$row['name']},
  4. <br/>
  5. date:{$row['date']},
  6. <br/>
  7. gas: {$row['gas']},
  8. <br/>
  9. food: {$row['food']},
  10. <br/>
  11. other:{$row['other']}
  12. <br/><br/><br/>";

Saves you about 2 characters a line...
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

You just need to loop through your array and enclose the results in html select options tags to use in your next form. You will now send varibleid in your next form as the user.

Code: [ Select ]

<select name="varibleid"><option value=''>Select User</option>
<?
while($row = mysql_fetch_array($result)){
echo "<option value='$row[id]'>$row[idname]</option>";
}
?>
</select>
  1. <select name="varibleid"><option value=''>Select User</option>
  2. <?
  3. while($row = mysql_fetch_array($result)){
  4. echo "<option value='$row[id]'>$row[idname]</option>";
  5. }
  6. ?>
  7. </select>
  • UniquelyYoursPC
  • Web Master
  • Web Master
  • User avatar
  • Posts: 2997
  • Loc: Canada "A"

Post 3+ Months Ago

thanks i will try it out..
thanks again...

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 54 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.