doushi9376 2017-05-15 15:24
浏览 398

SqL语句中的AND和OR不起作用

I am trying to retrieve some information from database but I guess I am doing something wrong because I am only able to retrieve half info not full. Please let me know where i am wrong

In Below code there are two SQL statements 1st) $sql and 2nd) $result. I have problem in $sql statement.

UPDATE- QUESTION HAS BEEN SOLVED.SEE Below for answer

<?php
include "head.html";
echo '</div>';
 $search_id =  $_GET["id"]; 

   $dbhost = 'localhost';
   $dbuser = 'lemonwork';
   $dbpass = 'lemonwork@2014';
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   if(! $conn ) {
      die('Could not connect: ' . mysql_error());
   }
   $sql = 'SELECT * FROM lemontable where STORE_ID=';
   $val=" AND (DESCRIPTION=%'Toy'% OR TITLE=%'TOY'% OR Category=%'TOY'%)";
   $sql=$sql.$search_id.$val;
   $result='SELECT  STORE_LOGO, Lemon_COUNT FROM lemoncount where STORE_ID=';
$result=$result.$search_id;
   mysql_select_db('lemondata');
   $retval = mysql_query( $sql, $conn );
   $retval1=mysql_query($result, $conn);
   $x=0;
   if ($result) {
  $row1 = mysql_fetch_assoc($retval1);

echo'<div style="text-align: center;">';
  echo'<img src="'.$row1['STORE_LOGO'].'"style="display: block; margin-left: auto;  margin-right: auto;">';
  echo 'Total Coupons: '.$row1['COUPON_COUNT'].'<br/>';
echo'</div>';
  }
   if(! $retval ) {
      die('Could not get data: ' . mysql_error());
   }
   while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {   
echo "<table>";
  echo '<tr><th><a href="'.$row["LINK"].'"><img src="'.$row1["STORE_LOGO"].'"></th>';
echo "<td style='padding-left: 6px !important;'><a href='".$row['LINK']."'><h1>".$row["title"]. "</h1></a>";
echo "<br/><strong>By</strong> ".$row["STORE_NAME"]."<br/><strong>Expire on:</strong> ".$row["Dates"]."<br/><strong>Category:</strong> ".$row["Category"]."<br/><strong>work:</strong> ".$row["work"]."<br/><strong>DESCRIPTION:</strong> ".$row["DESCRIPTION"]."<b style='color:Blue'> Read More"."<hr></td></tr>";

}       
echo '</table>';
   mysql_close($conn);
?>
  • 写回答

2条回答 默认 最新

  • donglvlao8367 2017-05-15 15:41
    关注

    I think you want an underlying SQL query like this:

    select * from lemontable
        where store_id=1192 and (
           description like '%TOY%' or
           title like '%TOY%'
        )
    

    This will return any records within the given store where the title or the description contain the word toy.

    However, rather than simply slightly changing your code to correct this syntax, you really ought to rewrite this in a more secure way, using parameterized queries. The code you have shown here would potentially allow malicious users to manipulate your web form to do things like delete or alter your data, steal private information, or bog down your site to take it off line. In short: not good.

    There were a number of helpful links in the comments, but this one in particular gives an example that should serve as a good starting point.

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建