Mysql / php选择使用2个表并从中搜索信息

时间:2011-12-12 00:10:22

标签: php mysql

在我的SQL中,我有两个表:film = filmid, filmnamereview = id, reviewtitle, filmreview, filmid

我基本上想要使用HTML创建一个表单来搜索filmname表中的film,然后在这之后我想从review表中检索评论filmidfilm table匹配,以便将filmname与评论相匹配。

有人知道这样做吗?任何帮助,将不胜感激。谢谢!

$searching=$_POST['searching'];
$searchfilm=$_POST['searchfilm'];


//This is only displayed if they have submitted the form
if ($searching =="yes")
 {
 echo "<h2>Film Review</h2><p>";


 //If they did not enter a search term we give them an error
 if ($searchfilm == "")
 {
 echo "<p>You forgot to enter a search term";
 echo
 exit;
 }


 // Otherwise we connect to our Database



 // We preform a bit of filtering
 $searchfilm = strtoupper($searchfilm);
 $searchfilm = strip_tags($searchfilm);
 $searchfilm = trim ($searchfilm);


  //Now we search for our search term, in the field the user specified
  $data = mysql_query("SELECT filmid, filmname FROM film WHERE filmname LIKE        '$searchfilm'"); WORKS 


 $result = mysql_query($data1) or die(mysql_error()); 


 //results
 while($row = mysql_fetch_array($data1))
 {
 echo $row['filmid'];
 echo "<br>";
 echo $row['filmname'];
 echo "<br>";
 echo $row['filmreview'];
 echo "<br>";
 }


  //This counts the number or results - and if there wasn't any it gives them a little message explaining that
 $anymatches = mysql_num_rows($data1); 
 if ($anymatches == 0)
 {
 echo "Sorry, the film name does not exist<br><br>";
 }


 //what was searched
 echo "<b>Searched For:</b> " .$searchfilm;
 }


////////////////////////////

$searchfilm=$_POST['searchfilm'];

/*connect to database*/


/*query database*/
$query = "SELECT review.* FROM film, review where film.filmid=".$searchfilm." AND        film.filmid=review.id";
$result = mysql_query($query);

/*handle result*/
if ($result)
while($reviews_from_db = mysql_fetch_array($result))
/* if reviews_from_db is not null it will contain a multi-dimensional array, with an     element for each review and each of those is an array with the database fields */
{
    echo $row['filmid'];
    echo "<br>";
    echo $row['filmname'];
    echo "<br>";
    echo $row['filmreview'];
    echo "<br>";

}

2 个答案:

答案 0 :(得分:0)

不清楚你在问什么 - 你在寻找SQL,用PHP来调用SQL,或者只是如何处理这个问题?

假设您没有在html表单中使用纯文本字段,而是使用用户选择的某种形式的列表并返回关联的film_id,PHP&amp; SQL应该类似于:

/*connect to database*/
mysql_connect("hostname","db_user","password") or die(mysql_error());
@mysql_select_db("database_name") or die( "Unable to select database. ".mysql_error());

/*query database*/
$query="SELECT review.* FROM film, review where film.filmid='$film_id_from_html' AND film.filmid=review.filmid";
$result=mysql_query($query);

/*handle result*/
if ($result){
    $reviews_from_db=mysql_fetch_array($result);
    /* if reviews_from_db is not null it will contain a multi-dimensional array, with an element for each review and each of those is an array with the database fields */
}

显然这需要更多的工作,例如它应该被分解成功能;我没有包括错误处理等等

如果你想要一个纯文本字段供用户输入,你会进入更复杂的领域,因为用户输入不可靠并需要验证 - 你需要管理标点符号,拼写空格甚至差异标题(例如“指环王”,“指环王”,“LOTR”,“指环王,第一部分”等等,只是可以输入的一些可能的版本),除此之外你也正在打开数据库以应对安全风险。

修改 如果您想进行名称比较,这是您需要的查询:

$query="SELECT review.* FROM film, review where film.filmname='$film_name_from_html' AND film.filmid=review.filmid";

答案 1 :(得分:0)

现在我们到了某个地方!您需要提供尽可能多的信息以获得具体(因此有用)的答案。既然您已经编辑了帖子以添加代码,我们就可以看到您出错的地方。首先,在我们查询数据库之前,您有一些基本错误。

$data = mysql_query("SELECT filmid, filmname FROM film WHERE filmname LIKE        '$searchfilm'"); 
$result = mysql_query($data1) or die(mysql_error()); 
while($row = mysql_fetch_array($data1)){...}
  1. 您有$data = my_query(...),但在其余代码中使用$data1
  2. 您将查询结果输入变量$result,但是将包含SQL($data1)的字符串传递给mysql_fetch_array()
  3. 其他问题: 从技术上讲,你不应该使用LIKE因为你没有进行通配符搜索 - 这是一个直接的比较所以你应该使用=。 (LIKE可能会起作用,但这里的错误和低效)

    修改 查看您添加的新代码:

    $query = "SELECT review.* FROM film, review where film.filmid=".$searchfilm." AND        film.filmid=review.id";
    $result = mysql_query($query);$result = mysql_query($query);
    if ($result)
    while($reviews_from_db = mysql_fetch_array($result)) {
        echo $row['filmid'];
    
    1. 您正在将searchfilm与id进行比较,而非filmname:film.filmid=".$searchfilm。它应该是:

      $ query =“SELECT review。* FROM film,review where film.filmname =”。$ searchfilm。“AND film.filmid = review.filmid”;

    2. 您将结果行保存在$reviews_from_db中,但使用错误的变量名$row来访问其中的数据。