SQL查询只返回1行

I am working on a php project where users can select a make and model of a car from a form and this will trigger the search for products that apply for that particular combination. When I run the query in SQL, and there are multiple products, I get multiple rows. but when I try to display those results using PHP I only get 1 row. Do you have an idea what I am doing wrong? here's the code:

$query1 = "SELECT * FROM partmakes WHERE MAKE = '$_POST[make]' and MODEL ='$_POST[model]'";
$results1 = mysqli_query($cnx, $query1);
$row1 = mysqli_fetch_assoc($results1);


$query2 = "SELECT * FROM `products` WHERE `PARTNO`= '$row1[PARTMAKE1]' OR (`PARTNO`= 
'$row1[PARTMAKE2]' OR `PARTNO`= '$row1[PARTMAKE3]' OR `PARTNO`= '$row1[PARTMAKE4]');";

$results2 = mysqli_query($cnx, $query2);

echo "These are the products for your car:<br />";
while ($row2 = mysqli_fetch_assoc($results2)){
    extract($row2);
    echo "$row2[PARTNO]<br />";
}

Thanks a lot for your help


edit - content from OP's file http://standtek.com.mx/test/screenshots.docx in the comments

So the user will select from a drop down the make and the model of the car. The first query looks for the make and model in the table: enter image description here

Then I use the information from the PARTMAKE columns as reference for the second query

Here’s an example of the query in phpmyadmin and it shows 4 rows: enter image description here

My current script displays only 1 row for some reason: enter image description here

This is what I want displayed in the results: enter image description here

dpmopn8542
dpmopn8542 下次,将您的笔记和屏幕截图添加到您的问题中,而不是评论中的docx文件链接。我已将docx内容添加到您的问题中,并将很快发布回复。
大约 5 年之前 回复
douyu7210
douyu7210 对了谢谢。请看一下以下链接:链接在那里我放了一些数据库的屏幕截图和所需的结果
大约 5 年之前 回复
douke6424
douke6424 您能否从2个表格中提供一些样本数据,并显示您想要的结果。
大约 5 年之前 回复
douyongdao4046
douyongdao4046 这给了我一行,其余的PARTMAKES在行的末尾。我正在寻找的是为每个PARTMAKE获取单独的行。这样的事情:PARTNODESCPRICE1Something10.502其他东西7.50
大约 5 年之前 回复
duanlv4984
duanlv4984 您可以在JOIN查询中执行此操作,而不是2个单独的查询。类似于-SELECT*FROMproductsJOINpartmakesON(products.PARTNO=partmakes.PARTMAKE1ORproducts.PARTNO=partmakes.PARTMAKE2ORproducts.PARTNO=partmakes.PARTMAKE3ORproducts.PARTNO=partmakes.PARTMAKE4)WHEREpartmakes.MAKE='$_POST[make]'和partmakes.MODEL='$_POST[model]'。请注意,请务必清理您的用户数据。
大约 5 年之前 回复

1个回答

Use a JOIN query and you could return all the rows -

$query = "SELECT products.* 
          FROM products
          JOIN partmakes 
          ON (
             products.PARTNO = partmakes.PARTMAKE1 
             OR 
             products.PARTNO = partmakes.PARTMAKE2 
             OR 
             products.PARTNO = partmakes.PARTMAKE3 
             OR
             products.PARTNO = partmakes.PARTMAKE4) 
          WHERE partmakes.MAKE = '$_POST[make]' 
          AND partmakes.MODEL ='$_POST[model]'";

$results = mysqli_query($cnx, $query);

echo "These are the products for your car:<br />";

echo "<table><tr><th>PARTNO</th><th>TITLE</th><th>PRICE</th></tr>";

while ($row = mysqli_fetch_assoc($results)){
    echo "<tr><td>{$row[PARTNO]}</td><td>{$row[TITLE]}</td><td>{$row[PRICE]}</td></tr>";
}
echo "</table>";

Make sure to sanitize your $_POST user data.

You can see the JOIN query working in sqlFiddle - http://sqlfiddle.com/#!9/754feb/4

duanhuiuw12555
duanhuiuw12555 经过一段时间审查数据,解决方案是摆脱像你建议的额外的白色空间。 非常感谢您的时间和帮助!
大约 5 年之前 回复
dongsuichi6529
dongsuichi6529 那么您的数据可能存在一些问题。 例如,可能存在一些前导或尾随空格,因此基本=将不匹配。 您将需要查看未显示的3行,并找出它们未被返回的原因。
大约 5 年之前 回复
dongyunwei8596
dongyunwei8596 非常感谢所有的努力,我很感激..然而它仍然显示1行:(我知道sqlfiddle示例显示所有行但是当你在phpmyadmin中尝试查询它只返回1行。而且php不显示 任何错误或警告,只有一行
大约 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐