2

I would like to display a drop down list with data retrieved from a table with only one column

this is my form

<html>
<head>

</head>
<body>

<form action="insert_customer_complaint.php" method="post">

Name: <input type="text" name="name"><br>
Complaint: <input type="text" name="comp"><br>
Reason: <select name="reason">
Add <input type="submit">

</form>

</body>
</html>

this the php file i use to insert data

    <?php
// Create connection
$con=mysqli_connect("localhost","ccc","ccc","ccc");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO reason (reason_name)
VALUES
('$_POST[reason_name]')";


if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);

?>

Inserting part works very well I need to get data for the drop down list from a table with one column using the same php file

table name = reason, column name = reason_name

please help me out

I have manged to insert data and every thing works well. now i want to generate report in a table from where the the column name should be retrieved from from the data in a particular row here's what i need

please see click for image

http://testserverforprojects.tk/CC/tables.JPG

table should be dynamic because it should have a latest year at the end for instance this year only will have data upto 2013 so 2013 will be the last column.. so in 2015, 2014 will be the last column

user1873058
  • 23
  • 1
  • 5
  • Your present code is open to [**SQL injection**](http://stackoverflow.com/q/60174/). Use [**prepared statements**](http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php), or [**PDO**](http://php.net/pdo) – Funk Forty Niner Apr 17 '14 at 18:45
  • Thank you I will do as you mentioned. – user1873058 Apr 18 '14 at 08:00

2 Answers2

4

Use this:

    <html>
<head>

</head>
<body>

<form action="insert_customer_complaint.php" method="post">

Name: <input type="text" name="name"><br>
Complaint: <input type="text" name="comp"><br>
Reason: <select name="reason">
    <?php
    $con=mysqli_connect("localhost","ccc","ccc","ccc");
    if (mysqli_connect_errno())
    {echo "Failed to connect to MySQL: " . mysqli_connect_error();}
    $sql="SELECT * FROM reason";
    $result = mysqli_query($con, $sql);
    while($row = mysqli_fetch_array($result))
  {    
    echo '<option value='.$row['reason_name'].'>'.$row['reason_name'].'</option>';
      }
?>
    </select>
Add <input type="submit">

</form>

</body>
</html>
artur99
  • 818
  • 6
  • 18
  • 1
    No permanent damage done ;-) I removed my comment and downvote soon as I saw your edit. Will +1 – Funk Forty Niner Apr 17 '14 at 18:46
  • @artur99 can you please help me out with auto genrated ID – user1873058 Apr 18 '14 at 14:43
  • I have manged to insert data and every thing works well. now i want to generate report in a table from where the the column name should be retrieved from from the data in a particular row here's what i need 2010 value 2011 – user1873058 Apr 28 '14 at 16:53
  • @user1873058 you need an auto generated ID? Go to phpmyadmin and set an autoincrement in the `id` field, and when inserting, simply omit that and every ID will be different and automatically increased – artur99 Aug 25 '14 at 09:59
1

Wouldn't it just be :

$sql="SELECT reason_name From reason";

? Then iterate over the returned set and build the related html to accomplish your interface requirements

Brandt Solovij
  • 2,124
  • 13
  • 24