I am working on creating a searchable database of all the students and associated information (for a larger project involving managing a iPad deployment). The database has 7 columns:
- first_name
- last_name
- id_number
- grade
- grad_year
- student_email
- dob
What I need to do is return the entire row if the input matches any of the values in the row.
The PHP that I am working with is:
<?php
$conn = mysql_connect ("localhost", "blmrvftl_ipdb", "ipad-db") or die ('I cannot connect to the database because: ' . mysql_error());
$selected = mysql_select_db ("blmrvftl_ipdb")
or die ("Could not select database because: " . mysql_error());
// PHP Search Script
$sql = "select * from phonebook, where first_name = '".$_GET['seek']."' or last_name = '". $_GET['seek'] ."' or id_number = '". $_GET['seek'] ."'";
//$sql = "select * from student_database, where first_name = '".$_GET['seek']."' or last_name = '". $_GET['seek'] ."' or id_number = '". $_GET['seek'] ."' or grade = '". $_GET['seek'] ."' or grad_year = '". $_GET['seek'] ."' or student_email = '". $_GET['seek'] ."' or dob = '". $_GET['seek'] ."'";
// $sql = "select * from student_database, where first_name = '".$_POST['seek']."' or last_name = '". $_POST['seek'] ."' or id_number = '". $_POST['seek'] ."' or grade = '". $_POST['seek'] ."' or grad_year = '". $_POST['seek'] ."' or student_email = '". $_POST['seek'] ."' or dob = '". $_POST['seek'] ."'";
$result = mysql_query($sql,$conn)or die (mysql_error());
if (mysql_num_rows($result)==0){
echo "No Match Found";
}else{
while ($row = mysql_fetch_array($result)){
echo "Name: " .$row['first_name']." ".$row["last_name"]."<br>";
echo "Student ID: ".$row['id_number']."<br>";
// echo "Department: ".$row['department_name']."<br>";
// echo "Directorate: ".$row['directorate_name']."<br>";
//echo "Site: ".$row['site_name']."<br>";
//echo "Phone #: ".$row['pb_tel_ext']."<br>";
//echo "Email Address: ".$row['pb_email_address']."<br>";
echo "<br>";
echo "---------------------------------------------------------------------"."<br>";
}
}
mysql_close();
?>
What i am having issues with is that I can't quite get the format right to get it to search all the columns. (the uncommented $sql line is a modified version of the original from the site i snatched this bit of code from.) The error i get is:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where first_name = 'Elliott' or last_name = 'Elliott' or id_number = 'Elliott'' at line 1