gpt4 book ai didi

php - 在 php mysql 查询中不会合并包含表中的所有字段

转载 作者:行者123 更新时间:2023-11-29 11:15:59 25 4
gpt4 key购买 nike

这个项目的想法是创建一个 sql 查询,该查询将根据下拉框中选择的项目创建。例如,如果我单击作者姓名,我将获取所有作者记录,那么如果我想要作者+流派,我会从作者下拉列表中选择作者,从流派下拉列表中选择流派,并显示该流派的作者,依此类推。我的想法是用变量替换作者姓名,而不是查询阅读:

$sql = "SELECT * FROM books WHERE author = 'Ken Davies'

我用变量 $bird 替换了名称,而变量 $bird 已在此处声明为作者

$bird = ( ! empty($_POST['author'])) ? $_POST['author'] : null;

但是代码只部分工作。每个框单独工作,但它们不会组合起来。因此,如果我选择作者肯·戴维斯,那么我会选择流派,我会选择流派,并且所有作者不仅仅是肯·戴维斯和他的流派。任何人都可以看到我做错了什么,非常感谢任何帮助,我已将所有代码放在这里查看。

 <html>
<head>
<title>My Page</title>
</head>
<body>
<br>
<form name="myform" action="authors3.php" method="POST">

<select name="author" size="2">
<option value="ken davies">ken davies</option>
<option value= "arthur smith">arthur smith</option>
<option value="gill rafferty">gill rafferty</option><br />
<option value="molly brown">molly brown</option><br />
<option value="gilbert riley">gilbert riley</option><br />
<input type = "submit" name = "submit" value = "go">

<select name="genre" size="4">
<option value="adventure">adventure</option>
<option value="biography">biography</option>
<option value="crime">crime</option><br />
<option value="romance">romance</option>
<option value="thriller">thriller</option>

<input type = "submit" name = "submit" value = "go">
<select name="year" size="4">
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>

<input type = "submit" name = "submit" value = "go">

<select name="publisher" size="4">
<option value="blue parrot">blue parrot</option>
<option value="yonkers">yonkers</option>
<option value="zoot">zoot</option>

<input type = "submit" name = "submit" value = "go">


<?php

#variables created and tested
$bird = ( ! empty($_POST['author'])) ? $_POST['author'] : null;
$cat = ( ! empty($_POST['genre'])) ? $_POST['genre'] : null;
$mouse = ( ! empty($_POST['year'])) ? $_POST['year'] : null;
$goat = ( ! empty($_POST['publisher'])) ? $_POST['publisher'] : null;


$con = mysql_connect("localhost","root","");
If (!$con){
die("Can not Connect with database" . mysql_error());
}
Mysql_select_db("authors",$con);


if (isset($bird) && isset($cat) && isset($mouse) && isset($goat))
{
$sql = "SELECT * FROM books
WHERE author = '$bird'
AND genre ='$cat'
AND year= '$mouse'
AND publisher = '$goat' ";
}
'$cat'
AND year= '$mouse'
AND publisher = '$goat' ";}


else if (isset($bird))
{
$sql = "SELECT * FROM books WHERE author = '$bird' ";
}

if(!is_null($author)){

$sql.="AND author = $author";
}
else if (isset($cat))
{
$sql = "SELECT * FROM books WHERE genre = '$cat' ";

}

if(!is_null($genre)){

$sql.="AND genre = $genre";
}
else if (isset($mouse))
{
$sql = "SELECT * FROM books WHERE year = '$mouse' ";
}

if(!is_null($year)){
$sql.="AND year = $year";
}
else if (isset($goat))
{
$sql = "SELECT * FROM books WHERE publisher = '$goat' ";
}
if(!is_null($publisher)){

$sql.="AND publisher = $publisher";
}

$myData = mysql_query($sql,$con);

echo"<table border=3>

<tr>
<th>id</th>
<th>author</th>
<th>title</th>
<th>publisher</th>
<th>year</th>
<th>genre</th>
<th>sold</th>
</tr>";

while($record = mysql_fetch_array($myData)){
echo "<tr>";
echo "<td>" . $record['id'] . "</td>";
echo "<td>" . $record['author'] . "</td>";
echo "<td>" . $record['title'] . "</td>";
echo "<td>" . $record['publisher'] . "</td>";
echo "<td>" . $record['year'] . "</td>";
echo "<td>" . $record['genre'] . "</td>";
echo "<td>" . $record['sold'] . "</td>";

echo "<tr />";
}
echo "</table>";



mysql_close($con);



?>
note: all four are working individually<br />
not working when combined<br />


</form>
</body>
</html>

最佳答案

$cond = "SELECT * FROM books where 1";

if (isset($bird))
{
$cond .= "author = '$bird'";
}
if(!is_null($author)){

$cond .= " AND author = $author";
}

if (isset($cat))
{
$cond .= " and genre = '$cat' ";

}

if(!is_null($genre)){

$cond .= " AND genre = $genre";
}

等等......

$myData = mysql_query($sql,$con);

现在我的建议是

  1. 请使用mysqli_*()
  2. 还要预防 SQL 注入(inject)。

关于php - 在 php mysql 查询中不会合并包含表中的所有字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39744411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com