gpt4 book ai didi

php - 将列表中的变量放入 sql 字符串中

转载 作者:行者123 更新时间:2023-11-30 00:22:06 25 4
gpt4 key购买 nike

我创建了一个名为 myauthors 的 mySQL 数据库,在 myauthors 中我创建了一个名为表 1 的表。然后我创建了 4 个选择框(请参阅代码)。之后我创建了一些代码和一个带有 while 循环的 sql 查询来填充输出(打印或回显)。它工作正常并将表格的内容打印到屏幕上。但这就是我想要做的。当用户单击选择框中的某个值时,我希望将其放置在一个变量中,然后将其放置到 sql 查询行中。我尝试将其添加到下面的代码

    $aa = "author" 
$bb = "publisher"
$cc = "year"
$dd = "genre"

其中“”中的所有内容都是选择框的名称。这用于查询

'"$aa"' AND publisher= '"$bb"' AND year = '"$cc"' AND genre ='"$dd"' " "; 

但是没有成功。我是不是找错了树,还有其他方法可以做到这一点吗?非常感谢吉姆

  <!DOCTYPE HTML>
<html>
<body>


<select name = "author">
<option value="kendavies">ken davies</option>
<option value="arthursmith">arthur smith</option>
<option value="gillrafferty">gill rafferty</option>
<option value="mollybrown">molly brown</option>
<option value="gilbert riley">gilbert riley</option>
<option value="colinwilson">colin wilson</option>
<option value="jamesgreen">james green</option>
<option value="arnoldlaing">arnold laing</option>
<option value="cathyellis">cathy ellis</option>
<option value="carolreed">carol reed</option>

</select>

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

</select>
<select name = "year">
<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>

</select>

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

</select>

</body>
</html>
<?php
mysql_connect ("localhost","root","") or die(mysql_error());
mysql_select_db ("myauthors") or die(mysql_error());
$strSQL = "SELECT * FROM `table 1`";
$rs = mysql_query($strSQL);



while($row = mysql_fetch_array($rs) ) {

print $row ['ID']."<br/>";
print $row ['author']."<br/>";
print $row ['booktitle']."<br/>";
print $row ['publisher']."<br/>";
print $row ['yearpublished']."<br/>";
print $row ['genre']."<br/>";
print $row ['copiessold']."<br/>";

}
mysql_close();
?>
</body>
</html>

我已将变量和新的 sql 行添加到代码中,但它仍然不起作用,这就是代码现在的样子。

 <!DOCTYPE HTML>
<html>
<body>


<select name = "author">
<option value="kendavies">ken davies</option>
<option value="arthursmith">arthur smith</option>
<option value="gillrafferty">gill rafferty</option>
<option value="mollybrown">molly brown</option>
<option value="gilbert riley">gilbert riley</option>
<option value="colinwilson">colin wilson</option>
<option value="jamesgreen">james green</option>
<option value="arnoldlaing">arnold laing</option>
<option value="cathyellis">cathy ellis</option>
<option value="carolreed">carol reed</option>

</select>

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

</select>
<select name = "year">
<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>

</select>

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

</select>
$aa = "author"
$bb = "publisher"
$cc = "year"
$dd = "genre"
</body>
</html>
<?php

$aa = "author";
$bb = "publisher";
$cc = "year";
$dd = "genre";
mysql_connect ("localhost","root","") or die(mysql_error());
mysql_select_db ("myauthors") or die(mysql_error());
$strSQL = "SELECT * FROM `table 1`WHERE author = '$aa' AND publisher = '$bb' AND year = '$cc' AND genre ='$dd' ";
$rs = mysql_query($strSQL);



while($row = mysql_fetch_array($rs) ) {

print $row ['ID']."<br/>";
print $row ['author']."<br/>";
print $row ['booktitle']."<br/>";
print $row ['publisher']."<br/>";
print $row ['yearpublished']."<br/>";
print $row ['genre']."<br/>";
print $row ['copiessold']."<br/>";


}
mysql_close();
?>
</body>
</html>

最佳答案

试试这个

"'$aa' AND publisher= '$bb' AND year = '$cc' AND genre = '$dd'"; 

关于php - 将列表中的变量放入 sql 字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23153163/

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