简体   繁体   English

基于$ _GET结果执行MYSQL查询

[英]Performing a MYSQL query based off of $_GET results

When a user clicks an item on my items page, it takes them to blank page template using $_GET to pass the item brand and model through. 当用户单击我的项目页面上的项目时,它会使用$ _GET将项目品牌和模型传递给空白页面模板。

I'd like to perform another MYSQL query when that user clicks through to populate the blank page with the product details from my database. 当用户点击以使用我的数据库中的产品详细信息填充空白页时,我想执行另一个MYSQL查询。 I'd like to retrieve the single row using the model number (unique ID) to populate the page with the information. 我想使用型号(唯一ID)检索单行,以使用信息填充页面。 I've tried a couple of things but am having a little difficulty. 我尝试了几件事,但遇到了一些困难。

On my blank item page, I have 在我的空白项目页面上,我有

                $brand = $_GET['Brand'];
                $modelnumber = $_GET['ModelNumber'];

                $query = mysql_query("SELECT * FROM items WHERE `Model Number` = '$modelnumber'");
                $results = mysql_fetch_row($query);

                echo $results;

I think having ''s around Model Number is causing troubles, but without them, I get a Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given error. 我认为在模型编号周围会引起麻烦,但没有它们,我会得到一个警告:mysql_fetch_row()期望参数1是资源,布尔给定错误。

My database columns looks like 我的数据库列看起来像

Brand | 品牌| Model Number | 型号| Price | 价格| Description | 说明| Image 图片

A few other things I have tried include 我尝试过的其他一些事情包括

$query = mysql_query("SELECT * FROM item WHERE Model Number = $_GET['ModelNumber']");

Which gave me a syntax error. 这给了我一个语法错误。 I've also tried concatenating the $_GET which gives me a mysql_fetch_row() expects parameter 1 to be resource, boolean given error 我也尝试连接$ _GET,这给了我一个mysql_fetch_row()期望参数1是资源,布尔给定错误

Which leads me to believe that I'm also going about displaying the results incorrectly. 这让我相信我也会错误地显示结果。 I'm not sure if I need to put it in a where loop like I have with my previous page which displays all items in the database because this is just displaying one. 我不确定是否需要将它放在where循环中,就像我上一页显示数据库中的所有项目一样,因为这只是显示一个。

First, notice the first comment on your question. 首先,请注意您的问题的第一条评论。 You definitely want to add some sort of sanitation, mysql_real_escape_string at the very least, although PDO or Mysqli would be preferred. 你肯定想要添加某种卫生设施,至少是mysql_real_escape_string,尽管PDO或Mysqli会是首选。

Second, before getting a real answer to your question, let's get some more information about your error. 其次,在得到您问题的真实答案之前,让我们获取有关您的错误的更多信息。

Try the following: 请尝试以下方法:

$brand = mysql_real_escape_string($_GET['Brand']);
$modelnumber = mysql_real_escape_string($_GET['ModelNumber']);

$query = mysql_query("SELECT * FROM items WHERE `Model Number` = '$modelnumber'")OR die(mysql_error());
$results = mysql_fetch_row($query);

var_dump($results);

mysql_error tell us what is going on behind the scenes. mysql_error告诉我们幕后发生了什么。

It seems that your "result" is pulling in too much information for a single variable. 您的“结果”似乎为单个变量提供了太多信息。 Also, I don't think your table should have column names with spaces, I would suggest filling them all with dashes or underscores. 另外,我不认为你的表应该有空格的列名,我建议用破折号或下划线填充它们。

Here's my suggestion: 这是我的建议:

$qry = mysql_query("SELECT * FROM items WHERE Model_Number = '$modelnumber'"); //REMEMBER TO MAKE THE CHANGE "Model Number" -> "Model_Number"
while($row = mysql_fetch_array($qry)){
    $brand = $row['Brand'];
    $modelnumber = $row['Model_Number'];
    $price = $row['Price'];
    $description = $row['Description'];
    $image = $row['Image'];
}

This will populate all of these variables with whatever you have in your tables. 这将使用表中的所有变量填充所有这些变量。

If you are using mysql_fetch_row it will only returns a numerical array of strings that corresponds to the fetched row, or FALSE if there are no more rows http://php.net/mysql_fetch_row 如果您使用的是mysql_fetch_row,它将只返回与获取的行对应的数字字符串数组,如果没有更多的行,则返回FALSE http://php.net/mysql_fetch_row

If you need to retrieve the data inside that row. 如果需要检索该行内的数据。 You need to use mysql_fetch_array() 你需要使用mysql_fetch_array()

also try using mysql_error(); 也尝试使用mysql_error();

 $results = mysql_fetch_row($query) or die(mysql_error());

you will see the error output produce by your code 您将看到代码生成的错误输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM