I am learning PHP these days and have designed a test website. I'm now going to create a PHP script to display all database from MySQL database. I have created an HTML forum, with <select> tag in order to select one of three columns "First Name", "Last Name" and "Age". When I select all the three column in the from, and click the retrieve button, the code works well and displays all the database available in MySQL. But the problem occurs only, when I leave one or more than one select options. I have attached the screenshots which will clearly define what I want to say. Please provide me the solution for this.
PHP Error When retriewing data from MySQL database
6
I am learning PHP these days and have designed a test website. I'm now going to create a PHP script to display all database from MySQL database. I have created an HTML forum, with <select> tag in order to select one of three columns "First Name", "Last Name" and "Age". When I select all the three column in the from, and click the retrieve button, the code works well and displays all the database available in MySQL. But the problem occurs only, when I leave one or more than one select options. I have attached the screenshots which will clearly define what I want to say. Please provide me the solution for this.
And below is the code for HTML form and PHP and MySQL.
And below is the code for HTML form and PHP and MySQL.
Code:
<?php
include "header.php";
if (isset($_POST['submit']))
{
// connect database
$con = mysql_connect("mysql5.000webhost.com", "xxxxx", "xxxxx");
// select database and create table
mysql_select_db("a8995753_db1", $con);
if(!empty($_POST['col1'])) // tutorial for this is here: http://www.webdesignerforum.co.uk/topic/36142-help-with-html-select-drop-down-and-phpmysql/
{
$col1=$_POST['col1'];
}
else
{
$col1=NULL;
}
if(!empty($_POST['col2']))
{
$col2=$_POST['col2'];
}
else
{
$col2=NULL;
}
if(!empty($_POST['col3']))
{
$col3=$_POST['col3'];
}
else
{
$col3=NULL;
}
$result = mysql_query("SELECT $col1, $col2, $col3 FROM Persons");
?>
<table border='1' border-color='#000000'>
<tr>
<?php
if (isset($col1))
{
echo "<th>First Name</th>";
}
if (isset($col2))
{
echo "<th>Last Name</th>";
}
if
(isset($col3))
{
echo "<th>Age</th>";
}
?>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
?>
<tr><td> <?php echo $row['FirstName'] ?> </td> <td> <?php echo $row['LastName'] ?> </td> <td> <?php echo $row['Age'] ?> </td></tr>
<?php
echo "<br />";
}
mysql_close($con);
}
else
{
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<!-- radio buttons names should be the same as column names in the database. -->
<select name = "col1">
<option></option>
<option>FirstName</option>
<option>LastName</option>
<option>Age</option>
</select>
<select name = "col2">
<option></option>
<option>FirstName</option>
<option>LastName</option>
<option>Age</option>
</select>
<select name = "col3">
<option></option>
<option>FirstName</option>
<option>LastName</option>
<option>Age</option>
<input type="submit" name="submit" value="Retrieve/Update Data"></input>
</select></form>
</body>
</html>
<?php
}
?> - lightswitch
- KirkMcD
- chretit
- Chrismartyn Banned
- SmartWeb
Next Topics on Trending Feed
-
6