PHP,SQL代码无法正常工作

时间:2015-05-21 21:02:11

标签: php sql

我遇到了一些问题,希望你的家伙可以帮助我。

我有这个代码,我无法开始工作。

看,奇怪的是,如果我改变了

$query2 = mysqli_query($dblogin, 
    "SELECT * FROM devices 
        RIGHT JOIN repair ON devices.name = repair.modelid 
        WHERE repair.modelid=$check");

$query2 = mysqli_query($dblogin, 
    "SELECT * FROM devices 
        RIGHT JOIN repair ON devices.name = repair.modelid 
        WHERE repair.modelid LIKE %iPad%");

我会工作,很好: - /

我真的输了,我试着回应$ check,看看$ check变量是否有错误。但这样做很好。

<div id="slidingDiv<?=$row["id"] ?>" class="toggleDiv row-fluid single-project">
   <div class="span6">
      <img src="adm/images/<?=$row["image"] ?>" alt="project <?=$row["id"] ?>" />
   </div>
   <div class="span6">
      <div class="project-description">
         <div class="project-title clearfix">
            <h3><?=$row["name"] ?></h3>
            <span class="show_hide close">
            <i class="icon-cancel"></i>
            </span>
         </div>
         <div class="project-info">
            <?php
               $check = $row["name"];
               $query2 = mysqli_query($dblogin, 
               "SELECT * FROM devices 
                   RIGHT JOIN repair ON devices.name = repair.modelid 
                   WHERE repair.modelid=$check");
               while($row = mysqli_fetch_assoc($query2))
                          {
                          ?>
            <div>
               <span><?=$row["price"] ?> ,-</span><?=$row["name"] ?>
            </div>
            <?php
               }
               ?>
            <div>
               <span>Client</span>Some Client Name
            </div>
            <div>
               <span>Date</span>July 2013
            </div>
            <div>
               <span>Skills</span>HTML5, CSS3, JavaScript
            </div>
            <div>
               <span>Link</span>http://examplecomp.com
            </div>
         </div>
         <p>Believe in yourself! Have faith in your abilities! Without a humble but reasonable confidence in your own powers you cannot be successful or happy.</p>
      </div>
   </div>
</div>

如果我更改了它,它运行得很好

<?
   $query = mysqli_query($dblogin, "select * from brands ORDER BY id");
   while($row = mysqli_fetch_assoc($query))
   {
   ?>
<li class="filter" data-filter="<?=$row["name"] ?>">
   <a href="#noAction"><img class="aimg" style="height:50px;" src="adm/images/<?=$row["image"] ?>" /></a>
</li>
<?
   }
   ?>
</ul>
<!-- Start details for portfolio project 1 -->
<div id="single-project">
<?
   $query = mysqli_query($dblogin, "select * from devices ORDER BY name DESC");
   while($row = mysqli_fetch_assoc($query))
              {
              ?>        
<div id="slidingDiv<?=$row["id"] ?>" class="toggleDiv row-fluid single-project">
   <div class="span6">
      <img src="adm/images/<?=$row["image"] ?>" alt="project <?=$row["id"] ?>" />
   </div>
   <div class="span6">
      <div class="project-description">
         <div class="project-title clearfix">
            <h3><?=$row["name"] ?></h3>
            <span class="show_hide close">
            <i class="icon-cancel"></i>
            </span>
         </div>
         <div class="project-info">
            <?php
               $check = $row["name"];
               $query2 = mysqli_query($dblogin, "select * from devices right join repair on devices.name = repair.modelid WHERE repair.modelid LIKE '%iPhone 6%'");
               while($row = mysqli_fetch_assoc($query2))
                          {
                          ?>
            <div>
               <span><?=$row["price"] ?> ,-</span><?=$row["name"] ?>
            </div>
            <?php
               }
               ?>
         </div>
      </div>
   </div>
</div>
<?
   }
   ?>

修改

SELECT * FROM devices
  RIGHT JOIN repair ON devices.name = repair.modelid
  WHERE repair.modelid LIKE '%iPhone 6%'
  LIMIT 0 , 30

给出这个结果

id  name    brand   image   model-id    id  name    modelid price   brand   date
7   iPhone 6    Apple   iphone6_small.jpg       217 Front Glas Udskiftning  iPhone 6    1300        1432215986
7   iPhone 6    Apple   iphone6_small.jpg       218 Bagside (komplet)   iPhone 6    2500        1432216016
7   iPhone 6    Apple   iphone6_small.jpg       219 Tænd/sluk Udskiftning  iPhone 6    500     1432216041
7   iPhone 6    Apple   iphone6_small.jpg       220 Homeknap    iPhone 6    500     1432216064
7   iPhone 6    Apple   iphone6_small.jpg       221 Ørehøjtaler   iPhone 6    500     1432216085
7   iPhone 6    Apple   iphone6_small.jpg       222 Ladestik    iPhone 6    500     1432216107
7   iPhone 6    Apple   iphone6_small.jpg       223 Batteri iPhone 6    500     1432216124
7   iPhone 6    Apple   iphone6_small.jpg       224 Vibrator    iPhone 6    500     1432216136
7   iPhone 6    Apple   iphone6_small.jpg       225 Mikrofon    iPhone 6    500     1432216165
7   iPhone 6    Apple   iphone6_small.jpg       226 Kamera  iPhone 6    500     1432216177
7   iPhone 6    Apple   iphone6_small.jpg       227 Højtaler (musik/lyd)   iPhone 6    500     1432216191
7   iPhone 6    Apple   iphone6_small.jpg       228 WIFI Antenne    iPhone 6    500     1432218537
7   iPhone 6    Apple   iphone6_small.jpg       229 Jackstik    iPhone 6    500     1432218564
7   iPhone 6    Apple   iphone6_small.jpg       230 Jailbreak   iPhone 6    500     1432218593
7   iPhone 6    Apple   iphone6_small.jpg       231 Bagside (komplet)   iPhone 6    2500        1432218612

4 个答案:

答案 0 :(得分:1)

错别字:

... WHERE repair.modelid LIKE %iPad%"

您没有引用LIKE条款:

... WHERE repair.modelid LIKE '%iPad%'"
                              ^------^--

没有引号意味着%iPad%将被视为未知/非法字段名称,从而导致SQL语法错误。由于您的查询绝对没有错误处理,因此您只是盲目地假设没有错。从不假设成功。总是假设失败,检查失败,并将成功视为一个惊喜。

即使是像这样的一些超级骨干错误处理也会有所帮助:

$result = mysql_query(...) or die(mysql_error());
                          ^^^^^^^^^^^^^^^^^^^^^^

答案 1 :(得分:0)

请原谅我,如果我的PHP有点生疏,但不应该这样:

$query2 = mysqli_query($dblogin, "select * from devices right join repair on devices.name = repair.modelid WHERE repair.modelid='" . $check . "'");

因此,在$ check为“bucket”的示例中,这将运行SQL:

select * from devices right join repair on devices.name = repair.modelid WHERE repaid.modelid='bucket'

答案 2 :(得分:0)

如果$ check仅包含产品名称,例如“iPad”,请更改您的查询:

$query2 = mysqli_query($dblogin, "select * from devices right join repair on devices.name = repair.modelid WHERE repair.modelid='$check'");

更新:

或者LIKE:

$query2 = mysqli_query($dblogin, "select * from devices right join repair on devices.name = repair.modelid WHERE repair.modelid LIKE '%$check%'");

请注意,如果$ check来自用户,则此语句易受SQL注入攻击。

答案 3 :(得分:0)

试试这个。括号周围的括号。虽然你把它称为id,但它等于name,所以我假设不是数字。

$query2 = mysqli_query($dblogin, 
    "SELECT * FROM devices 
        RIGHT JOIN repair ON devices.name = repair.modelid 
        WHERE repair.modelid='{$check}'");