Hi,

I have no trouble pulling data by a first name with the following code, but when I want to use both first and last name as means to access data, it won't work...

Here's the code:

<?php
// Include connection to your database
$con = mysql_connect("localhost","root","");
mysql_select_db("RUNNERS", $con) or die(mysql_error());

$name = $_POST['first_name']; 
$query = "SELECT * FROM runners WHERE first_name ='".$name."'"; 
$result = mysql_query($query) or die(mysql_error());



echo "<table class='sortable'>";
while ($list = mysql_fetch_assoc($result)) {
echo "<thead>";
echo "<tr>";
echo "<th>ID #</th>";
echo "<th>First Name</th>";
echo "<th>Last Name</th>";
echo "<th>1 Mile</th>";
echo "<th>2 Mile</th>";
echo "<th>5k</th>";
echo "<th>10k</th>";
echo "<th>15k</th>";
echo "<th>20k</th>";
echo "<th>13.1</th>";
echo "<th>26.2</th>";
echo "</thead>";

echo "<tbody>";
echo "<tr>";
echo "<th>" . $list['id'] . "</th>";
echo "<th>" . $list['first_name'] . "</th>";
echo "<th>" . $list['last_name'] . "</th>";
echo "<th>" . $list['one'] . "</th>";
echo "<th>" . $list['two'] . "</th>";
echo "<th>" . $list['five'] . "</th>";
echo "<th>" . $list['ten'] . "</th>";
echo "<th>" . $list['fifteen'] . "</th>";
echo "<th>" . $list['twenty'] . "</th>";
echo "<th>" . $list['half'] . "</th>";
echo "<th>" . $list['full'] . "</th>";
echo "</tr>";


echo "</tbody>";
}
?>

Thanks for any advice...

Recommended Answers

All 5 Replies

It should be something like:

$query = "SELECT * FROM runners WHERE first_name ='".$name."' OR last_name='".$lname."'";

Or if you want the two fields to be searched then go for AND:

$query = "SELECT * FROM runners WHERE first_name ='".$name."' AND last_name='".$lname."'";
$name = $_POST['first_name']; 
$query = "SELECT * FROM runners WHERE first_name ='".$name."'"; 
$result = mysql_query($query) or die(mysql_error());

Would I have to create another variable, $lname? $lname = $_POST['last_name']; thanks

$name = $_POST['first_name']; 
$query = "SELECT * FROM runners WHERE first_name ='".$name."'"; 
$result = mysql_query($query) or die(mysql_error());

Would I have to create another variable, $lname? $lname = $_POST['last_name']; thanks

yes:)

Actually, there is a better way to do this, which will give your users only one input to worry about, allow you the flexibility of keeping the first and last in separate fields, but still give the power of the search you are trying to do (or what i think you're trying to do with the value $name):

$query = "SELECT * FROM runners WHERE CONCAT(first_name,' ',last_name) = '$name'";

If it's a user search, this is especially handy, but should be changed to LIKE instead of = with % on each side of the comparison value:

$query = "SELECT * FROM runners WHERE CONCAT(first_name,' ',last_name) LIKE '%$name%'";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.