Php 如何使用get方法显示默认帖子?
我想从浏览器中获取id,并显示数据库中的一些图片 如果找不到display2.php?productid=find,那么我想显示默认图像 我该怎么做 这是我的密码Php 如何使用get方法显示默认帖子?,php,sql,database,Php,Sql,Database,我想从浏览器中获取id,并显示数据库中的一些图片 如果找不到display2.php?productid=find,那么我想显示默认图像 我该怎么做 这是我的密码 $sql = "SELECT * FROM productlist where productid=".$_GET['productid']; $result = $mysqli->query($sql); while($myRow = $result->fetch_array()) {
$sql = "SELECT * FROM productlist where productid=".$_GET['productid'];
$result = $mysqli->query($sql);
while($myRow = $result->fetch_array())
{
if(null !==($_GET['productid']==$myRow["productid"])){
echo "<img src=".$myRow["productid"].">";
}
else {
echo "<img src="SELECT productimage FROM productlist where productid = 1;">";
}
}
现在我会让你更容易解释。。。
看看这个
//This part works without any problem
$sql = "SELECT * FROM productlista where productid=".$_GET['productid'];
$result = $mysqli->query($restwo);
while($myRow = $resulttwo->fetch_array())
{
if(null !==($_GET['productid']==$myRow["productid"])){
echo "<img src=".$myRow["productimage"].">";
}
//This part below (that should be default) does not work...
if (!$_GET){
echo "hello world"; }
Asah指出了SQL注入。您应该将参数绑定到google上,或者至少执行以下操作:
$defaultImage = "SELECT * FROM productlist WHERE imageSrc != '' OR IS NOT NULL ORDER BY productid DESC LIMIT 1";
// run the query, get the result, create a variable with default image...
$defaultImageSrc = ''; // what you get from the query result
$_GET['productid'] = preg_replace('#[^0-9]#', '', $_GET['productid']);
$sql = "SELECT * FROM productlist where productid=".$_GET['productid'];
$result = $mysqli->query($sql);
while($myRow = $result->fetch_array()) {
if(!$myRow['imageSrc']) $myRow['imageSrc'] = $defaultImageSrc;
echo '<img src="'.$path.'">';
}
如果在未设置$\u GET['productid']时需要$\u GET['productid']或maxproductid,则可以使用三元来更改sql查询
$productid = ! empty($_GET['productid']) ? " WHERE productid = ".(int)$_GET['productid'] : " ORDER BY productid DESC LIMIT 1";
$sql = "SELECT * FROM productlist".$productid
$result = $mysqli->query($sql);
while($myRow = $result->fetch_array())
{
echo "<img src=".$myRow["productimage"].">";
}
但如果不是这样的话,违约将是
SELECT * FROM productlist ORDER BY productid DESC LIMIT 1
仔细的第1行存在漏洞。上一个sql查询不起作用…上一个sql查询不起作用,因为您没有将其发送到数据库,您只是将字符串放入html属性中。如何使其看起来像从数据库获取最新帖子并默认显示图片?我编辑了我的问题。请再检查一次。这是一个动态图像。这是默认的最新帖子。我不能这样设置。图像必须上传到某个地方。图像源在哪里?如果没有找到某个产品,并且您希望显示最新的产品帖子,那么您只需查询最后一个产品。喜欢从productlist中选择*,其中imageSrc!=按ID顺序描述限制1。这将抓取最后一篇带有图像的文章。我编辑了我的答案来解释如何做到这一点
SELECT * FROM productlist ORDER BY productid DESC LIMIT 1