我正在创建一个包含所有学生和相关信息的可搜索数据库(用于涉及管理 iPad 部署的大型项目)。数据库有 7 列:
- 名
- 姓
- 身份证号码
- 年级
- 毕业年份
- 学生邮箱
- 出生日期
如果输入与行中的任何值匹配,我需要做的是返回整行。
我正在使用的 PHP 是:
<?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();
?>
我遇到的问题是我无法完全正确地获取格式来搜索所有列。(未注释的 $sql 行是我从网站抢夺这段代码的原始版本的修改版本。)我得到的错误是:
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