Jump to content

[SOLVED] php get data from MySQL


denoteone

Recommended Posts

this questions would fall under both forums. I think...

I am writing a php page that shows data from a DB after the user selects and option from a drop down. here is what I have so far any help would be awesome this is my first time writing a page of this kind.

<? 
echo " 
<html> 
<head> "; 
include 'connect.php';


if(isset($_POST["button"])) {


// This is where I am haiving issues I think.
$result = mysql_query("SELECT * FROM example
WHERE name='$_POST['mydropdown']'") 
or die(mysql_error());  

echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Age</th> </tr>";

while($row = mysql_fetch_array( $result )) {

echo "<tr><td>"; 
echo $row['name'];
echo "</td><td>"; 
echo $row['age'];
echo "</td></tr>"; 
} 

echo "</table>";

}else{ 
?>
</head>
<body>
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<select name="mydropdown">
<option value="docrequest">Document downloads</option>
<option value="contactrequest">Contact Request</option>
<option value="mediarequest">Media Info</option>
</select>
<input type="submit" value="Submit" border="0" name="button">
</form>
</body>
</html>
<?
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/136686-solved-php-get-data-from-mysql/
Share on other sites

There are a few things I see wrong with this. First of all you should not use short open tags "<?" you should use "<?php" because it is more universal. Try this code:

 

<html> 
<head>
<?php 
include("connect.php");


if(isset($_POST['button']))
{
//make the query
$query = "SELECT * FROM example WHERE name='" . $_POST['mydropdown'] ."'";

//get the results
$result = mysql_query($query) or die(mysql_error());  

echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Age</th> </tr>";

while($row = mysql_fetch_array($result))
{

echo "<tr><td>"; 
echo $row['name'];
echo "</td><td>"; 
echo $row['age'];
echo "</td></tr>"; 
} 

echo "</table>";

}
else{ 
?>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="Post">
<select name="mydropdown">
<option value="docrequest">Document downloads</option>
<option value="contactrequest">Contact Request</option>
<option value="mediarequest">Media Info</option>
</select>
<input type="submit" value="Submit" border="0" name="button">
</form>
</body>
</html>
<?php
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.