在php中搜索编码

时间:2014-10-29 00:32:00

标签: php mysql

编译此代码时出错。有人可以帮帮我吗?

search.html

<form name="form" method="post" action="search0.php">
                Search by Matric No.:
                <input type="text" name="patron_ID" placeholder="Student No.">
                <input type="submit" name="Submit" value="Search">
          </form>

search0.php

<?php
/* include db connection file*/
include("dbconnect.php");

/* capture student number */
//$idsaja = $_POST['idsaja'];
$patron_ID = $_POST['patron_ID'];

/* execute SQL statement */
$sql= " SELECT br.patron_ID, p.patron_Name, br.book_Accession, b.book_Title, br.borrowed_Date,
                    br.discharged_Date, br.due_Date, b.book_Status

                    FROM borrow br
                    INNER JOIN patrons p 
                    ON p.patron_ID = br.patron_ID
                    JOIN book b
                    ON b.book_Accession = br.book_Accession
                    WHERE patron_ID = '$patron_ID'";

//"SELECT * FROM patrons WHERE patron_ID= '$patron_ID'";
$query = mysql_query($sql) or die ("Error: ".mysql_error());
$row = mysql_num_rows($query);

if($row == 0){
echo "No record found";
}
else{
$r = mysql_fetch_assoc($query);
$patron_ID = $r['patron_ID'];
$patron_Name = $r['patron_Name'];
$book_Accession = $r['book_Accession'];
$book_Title = $r['book_Title'];
$borrowed_Date = $r['borrowed_Date'];
$discharged_Date = $r['discharged_Date'];
$due_Date = $r['due_Date'];
$book_Status = $r['book_Status'];
?>

错误是&#34;错误:列&#39; patron_ID&#39;在where where子句中含糊不清&#34; ..这是什么意思?有人可以向我解释..

3 个答案:

答案 0 :(得分:3)

我认为这条信息不言自明。

您的SQL正在查询多个表:我看到borrowpatrons。这两个表都包含一个名为patron_ID的列,并且MySQL不知道您是指borrow.patron_ID还是patrons.patron_ID(事实上,外键关系没有帮助MySQL的)。

您需要通过在表名称前添加完整列名称来消除歧义歧义 - 您可以使用全名或别名。

答案 1 :(得分:0)

你肯定在其他(s)表中有另一个patron_ID,在where子句中为你的列添加前缀&#34; p.patron_ID&#34;

答案 2 :(得分:0)

我想念一下......代码应该是这样的:

$sql= " SELECT br.patron_ID, p.patron_Name, br.book_Accession, b.book_Title, br.borrowed_Date,
                    br.discharged_Date, br.due_Date, b.book_Status

                    FROM borrow br
                    INNER JOIN patrons p 
                    ON p.patron_ID = br.patron_ID
                    JOIN book b
                    ON b.book_Accession = br.book_Accession
                    **WHERE br.patron_ID = '$patron_ID'";**

我的错误已经解决了。谢谢大家:)