所以我想做的是根据下拉列表中选择的选项从数据库中选择多个项目,
我有一个名为articles的表,我们在那里得到了id_article、流派。
类型有 Action 、冒险、戏剧......
在我的代码中,我得到了一个包含流派值的下拉列表,我想要的是当我选择每个示例的操作时,它会自动选择下拉列表下方具有流派操作的所有项目。
已经尝试弄清楚如何做到这一点,并用谷歌搜索,但没有得到任何信息。
请帮忙。
我已经做了什么:
<?php require_once('Connections/valesilveira.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_valesilveira, $valesilveira);
$query_artigos = "SELECT * FROM artigos WHERE fornecedor like '%" . mysql_real_escape_string($_POST['mySelection']) . "%'";
$artigos = mysql_query($query_artigos, $valesilveira) or die(mysql_error());
$row_artigos = mysql_fetch_assoc($artigos);
$totalRows_artigos = mysql_num_rows($artigos);
?>
<form id="myForm" action="samePage.php" method="post">
<select id="mySelection" name="mySelection" onChange="$('#myForm').submit();">
<option value="Jimo">Jimo</option>
<option value="Actipage">Actipage</option>
</select>
</form>
<div class="container-outer">
<div class="container-inner">
<img class="artigo_img" src="images/artigos/<?php echo $row_artigos['id_artigo']; ?>.png" data-artigo-id="<?php echo $row_artigos['id_artigo']; ?>" style="height:130px;">
</div>
</div>
<?php
mysql_free_result($artigos);
?>
它给了我这个错误注意:第 35 行的 C:\xampp\htdocs\valesilveira\test.php 中未定义索引:mySelection,为什么?
请帮忙。
最佳答案
使用 html 的最佳选择是使用 javascript(在 select 等上)提交 POST 或 GET,将参数发布到 .php 脚本并获取 json 响应,或者使用 javascript 和 AJAX 执行基本相同的操作。 然后,php 脚本将获取变量,对数据库执行查询并在需要时处理它们,然后返回您可以使用的数据。
关于php - 根据下拉列表从数据库中选择项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26990744/