在mysqli

编程入门 行业动态 更新时间:2024-10-21 05:33:34
mysqli_query中使用变量?(using a variable in a mysqli_query?)

我试图根据我的表单上的用户输入从我的数据库返回一个值。 当我使用值运行代码时它可以工作,但是当我输入变量时它不会。 我确信它很简单,但我不明白吗?

这是有效的代码:

$sql_beam = mysqli_query($link,"SELECT cost_ft FROM Beams WHERE number = '201'"); while($row = mysqli_fetch_array($sql_beam)) { echo "<p>" . $row['cost_ft'] . "</p>"; echo "<br>"; }

当我将其更改为此时它不会:

$beam_num = $_POST['Beam Number']; $sql_beam = mysqli_query($link,"SELECT cost_ft FROM Beams WHERE number = '$beam_num'"); while($row = mysqli_fetch_array($sql_beam)) { echo "<p>" . $row['cost_ft'] . "</p>"; echo "<br>"; }

I am trying to return a value from my database based on a user input on my form. When I run the code using a value it works but when I put in the variable it doesn't. I am sure it is something simple, but I just don't get it?

Here is the code that works:

$sql_beam = mysqli_query($link,"SELECT cost_ft FROM Beams WHERE number = '201'"); while($row = mysqli_fetch_array($sql_beam)) { echo "<p>" . $row['cost_ft'] . "</p>"; echo "<br>"; }

When I change it to this it doesn't:

$beam_num = $_POST['Beam Number']; $sql_beam = mysqli_query($link,"SELECT cost_ft FROM Beams WHERE number = '$beam_num'"); while($row = mysqli_fetch_array($sql_beam)) { echo "<p>" . $row['cost_ft'] . "</p>"; echo "<br>"; }

最满意答案

然后$_POST['Beam Number']为空。

此外,您不应该将任何用户输入(例如获取数据的帖子)直接放入查询中。 谷歌sql注入以及如何防范它。

尝试调试$_POST['Beam Number'] 。 你应该看到它是空的,然后你必须找出它为空的原因。

编辑如果有值,则首先将查询字符串放入变量中,然后调出它。

error_log($beam_num); $query = "SELECT cost_ft FROM Beams WHERE number = '$beam_num'"; error_log($query); $sql_beam = mysqli_query($link,$query);

如果您仍然遇到问题,请提供记录的值。

Then $_POST['Beam Number'] is empty.

Also you should never put any user input such as post of get data directly into a query. Google sql injection and how to prevent it.

Try debugging $_POST['Beam Number']. You should see that it is empty and then you have to find out why it's empty.

Edit If there is a value, then debug out the query string by first placing it into a variable.

error_log($beam_num); $query = "SELECT cost_ft FROM Beams WHERE number = '$beam_num'"; error_log($query); $sql_beam = mysqli_query($link,$query);

Then if you are still having trouble, please supply the values logged.

更多推荐

本文发布于:2023-07-31 01:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340494.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:mysqli

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!