0

你好朋友//我有这2个代码用于简单的搜索,但它不起作用//你能帮我吗... SQL查询在PHPMYADMIN中工作正常。但是当我运行 php 代码时,它给了我这个错误 Unknown column 'a.orecid' in 'field list

这是recorddisplay.php的代码

<?php
require_once 'db.php'
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
body {
    background-image:url(images/back.jpg);
    background-repeat:!important;
    text-align: justify;
}
.button {
    background-color: #000;
    color: #FFF;
    font-weight: bold;
    text-transform: uppercase;
}
.title {
    letter-spacing: normal;
    word-spacing: normal;
}
.subtitle {
    background-image: url(images/transparent.png);
    background-repeat:inherit;
    font-size: x-large;
}
.container .content table tr td #form1 table tr td {
    text-align: center;
    font-weight: bold;
}
.container .content p {
    text-align: center;
    font-weight: bold;
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
}
#Myrules {
    margin-top: 100px;
    margin-right: auto;
    margin-bottom: auto;
    margin-left: 50px;
}
.container .content p {
    font-size: large;
    color: #FFF;
}
.container .footer {
    text-align: center;
}
</style>

<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
</head>
<body text="#FFFFFF">

 <div class="container">
  <div class="header"><a href="#"><img src="images/logocdts.jpg" alt="" name="Insert_logo" width="348" height="151" id="Insert_logo" style="background: #C6D580; display:block;" /></a>
  </div>

<?php
//session_start();
//$username = $_SESSION['username'];



    if ($_POST['Subject'] === '1')
        {


        $table = "criminalrecord";
        //$fields = "`crecid`,`staffname`,`date`";
        $title = "Criminalrecord";

        }
    else if ($_POST['Subject'] === '2')
        {
        $table = "offenderrecord";
        //$fields = "`orecid`,`staffname`,`date`";
        $title = "Offenderrecord";

        }

    else if ($_POST['Subject'] != '1' and $_POST['Subject'] != '2')
        {
               echo "<script>alert('Please choose subject to Proceed....')</script>";
               echo "<script language=javascript>history.back(1);</script>";
               echo "go back"; 
        }
            $subject      = $table;
            $id           = $_POST['id'];
            $staffname    = $_POST['staffname'];
            $date         = $_POST['date'];


    if ((($id) && ($staffname) || ($date)) || ((!$id) && ($staffname) || ($date))   ) {

        $field_array = array("staffname"=>"$staffname", "date"=>"$date"); 

        foreach ($field_array as $row => $value){

        if ($value != ""){  

                $query_string[] = "(`".$row."` LIKE '%".$value."%')"; 
            }
        }

        $where = implode(" || " , $query_string); //break apart the array into a string

                //SELECT a.crecid, e.staffname, date FROM criminalrecord a, criminal b, location c, unit d, staff e WHERE a.criminalid = b.criminalid AND b.locid = c.locid AND c.unitid = e.unitid
      $query = "SELECT a.crecid, b.criminalid, e.staffname, a.date FROM `".$table."` a, criminal b, location c, unit d,staff e WHERE a.criminalid = b.criminalid AND b.locid = c.locid AND c.unitid = e.unitid AND ((`".$table."id` LIKE '%".$id."%') && ".$where." )";
        //       echo "<br>".$query."<br>"; die();

        $search = mysql_query($query) or die(mysql_error()); 
    }   else if (($id) && (!$staffname) || (!$date)) {
      $query = "SELECT a.orecid, b.offenderid, e.staffname, a.date FROM `".$table."` a, offender b ,location c, unit d,staff e WHERE a.offenderid = b.offenderid AND b.locid = c.locid and c.unitid = e.unitid and `".$table."id` LIKE '%".$id."%' ";
    //  echo "<br>".$query."<br>"; die();
        $search = mysql_query($query) or die(mysql_error());
    }


        $num_rows = mysql_num_rows($search);
        if ($num_rows > 0) {
                echo '<h1>'.$title.'</h1>';
                print "<BR>";
                print "<table width=800 border=1>";
                print "<tr bgcolor=#FF9900>";
                print  "<td colspan=14>SUBJECT RECORDS INFO</td>";
                print "</tr>";
                print "<tr bgcolor=black>";
                print "<td width=108>ID</td>";
                print "<td width=108>Staff Name</td>";
                print "<td width=108>Date</td>";

                print "</tr>";

                while ($row= mysql_fetch_array($search)) {
                print "<tr>";

                print "<td>";
                print $row[0];
                print "</td>";
                print "<td>";
                print $row[1];
                print "</td>";
                print "<td>";
                print $row[2];
                print "</td>";

                print "</tr>";
            }
                print "</table>";
        } else {
                echo "<script>alert('Please go back to continue or to the Instructions menu for more info....')</script>";
                echo "<script language=javascript>history.back(1);</script>";
                echo "go back";
        }?>



<div class="footer">Copyright 2013 - CDTS </div>
</div>



</body>



</html>

这是recordsearch.php的代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>


<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script language="JavaScript" type="text/javascript">
function swapContent(cv) {
    $("#mydiv").html('<img src="gmap2.jpg"/>').show();
    var url= "ProfilePs_script.php"

        $.post(url, {contentVar: cv} ,function(data){
        $("#mydiv").html(data).show();
        });

}

function swapVar(cv) {
    $("#mydiv").html('<img src="gmap2.jpg"/>').show();
    var url= "ProfileSp_script.php"

        $.post(url, {swapVar: cv} ,function(data){
        $("#mydiv").html(data).show();
        });
}
</script>

<title>ProfilePC_PS</title>

<style type="text/css">
body {
    background-image: url(images/back.jpg);
    background-repeat:!important;
    text-align: center;
}
.button {
    background-color: #000;
    color: #FFF;
    font-weight: bold;
    text-transform: uppercase;
}
.title {
    letter-spacing: normal;
    word-spacing: normal;
}
.subtitle {
    background-image: url(images/transparent.png);
    background-repeat:inherit;
    font-size: x-large;
}
.container .content table tr td #form1 table tr td {
    text-align: left;
    font-weight: bold;
}
.container .content p {
    text-align: center;
    font-size: large;
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    font-weight: bold;
}
#Myrules {
    margin-top: 100px;
    margin-right: auto;
    margin-bottom: auto;
    margin-left: 50px;
}
.container .content .overlay table tr td #form2 table tr td {
    text-align: left;
    font-weight: bold;
}
</style>

<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />


</head>



<body text="#FFFFFF">
  <div class="content">
    <p>Search records here</p>
    <div class="overlay">
      <table width="514" border="0">
      <tr>
      <td width="508" height="204"><form action="recorddisplay.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
      <table width="400" border="0">
      <tr>
      <td width="178" align="center"><div align="left">Choose your Subject</div></td>
      <td width="212"><label for="id"></label>
              <label for="Subject"></label>
              <select name="Subject" id="Subject">
              <option value="">--please select--</option>
              <option value="1">Criminalrecord</option>
              <option value="2">Offenderrecord</option>
              </select></td>
      </tr>
      <tr>
      <td align="center" valign="middle"><div align="left">Subject ID</div></td>
      <td><input type="text" name="id" id="id" />
      </td>
      </tr>
      <tr>
      <td height="21"><div align="left">Staff Name</div></td>
      <td><input type="text" name="staffname" id="staffname" /></td>
      </tr>
      <tr>
      <td height="21"><div align="left">Date</div></td>
      <td><input type="date" name="date" id="date" /></td>
      </tr>
       <tr>
      <td height="26">&nbsp;</td>
      <td><input name="Clear" type="submit" class="button" id="Clear" value="Clear" />
          <input name="Search" type="submit" class="button" id="Search" value="Search" /></td>
     </tr>
      </table>
        </form></td>
      </tr>
      </table>

</div>
<div class="overlay"></div>
</div>





</body>



</html> 

这是我的数据库结构:

员工有 Staffid 和 unitid(fk)

单位有unitid

位置有 locid unitid(fk)

罪犯有criminid和locid(fk)

犯罪记录有 orecid、date 和 crimeid(fk)

按照要求:

当我回显查询时,当我选择犯罪记录时我得到了这个:

SELECT a.orecid, b.offenderid, e.staffname, a.date FROM `criminalrecord` a, offender b ,location c, unit d,staff e WHERE a.offenderid = b.offenderid AND b.locid = c.locid and c.unitid = e.unitid and `a.id` LIKE '%%' 

当我选择 offenderrecord 时,我得到了这个:

SELECT a.orecid, b.offenderid, e.staffname, a.date FROM `offenderrecord` a, offender b ,location c, unit d,staff e WHERE a.offenderid = b.offenderid AND b.locid = c.locid and c.unitid = e.unitid and `a.id` LIKE '%%' 
4

3 回答 3

0

您能否添加对 offenderrecord 表的描述?为什么你有 2 种不同的 Select 语句?第一个是 crecid,第二个是 orecid,这是正确的吗?因此,如果我没记错的话,您的代码为两个表运行相同的 Select-Statement,但应该为犯罪记录运行第一个,为犯罪记录运行第二个。

  1. $query = "SELECT a.crecid, b.criminalid, e.staffname, a.date FROM ".$table."a, 犯罪 b, 位置 c, 单位 d, 人员 e WHERE a.criminalid = b.criminalid AND b.locid = c.locid AND c.unitid = e.unitid AND (( ".$table."idLIKE '%".$id."%') && ".$where." )";

  2. $query = "SELECT a.orecid, b.offenderid, e.staffname, a.date FROM ".$table."a, offender b , location c, unit d,staff e WHERE a.offenderid = b.offenderid AND b.locid = c.locid和 c.unitid = e.unitid 和".$table."idLIKE '%".$id."%'";

如果我是正确的,问题不是 Select-Statement,而是运行了 Wrong 语句。

萨拉约格

于 2013-07-24T06:19:50.937 回答
0

当您为表名提供FROM ".$table." a别名时,请使用别名来引用其列而不是表名

`".$table."id`  change this to a.`id`
于 2013-07-24T06:20:13.627 回答
0

我在代码中注意到您有 a.crecid 但您给出了报告 a.orecid-typo 或问题来源的错误消息?

于 2013-07-24T06:27:20.000 回答