What problem with my code???

Discussion in 'PHP' started by lenhhoxung, Jan 1, 2010.

  1. #1
    Dear All,

    I have problem with SQL in PHP, it did not disply on the screen. Here is my code

    <form action="compare.php" method="get">
    Sản phẩm:
    <select name="fname">
    <option value="1001">Castrol Power1</option>
    <option value="1002">Castrol 4T</option>
    <option value="1007">Shell Advance SX</option>
    <option value="1008">Mobil Extra 4T</option>
    </select>
    VS <select name="fname2">
    <option value="1001">Castrol Power1</option>
    <option value="1002">Castrol 4T</option>
    <option value="1007">Shell Advance SX</option>
    <option value="1008">Mobil Extra 4T</option>
    </select>
    <input type="submit" value="So Sánh"/>
    </form>

    $result1 = mysql_query("SELECT * FROM product WHERE (product_sku=($fname))");
    $result2 = mysql_query("SELECT * FROM product WHERE (product_sku=($fname2))");

    echo "<table border='1'>";

    while($row1 = mysql_fetch_array($result1))
    while($row2 = mysql_fetch_array($result2))
    {
    echo "<tr>";
    echo "<td align=center width=150>So sánh</td>";
    echo "<td align=center>" . $row1['product_name'] . "</td>";
    echo "<td align=center>" . $row2['product_name'] . "</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>Nhãn hàng</td>";
    echo "<td align=center>" . $row1['brand_id'] . "</td>";
    echo "<td align=center>" . $row2['brand_id'] . "</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>Mã hàng</td>";
    echo "<td align=center>" . $row1['product_sku'] . "</td>";
    echo "<td align=center>" . $row2['product_sku'] . "</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>Mô tả sản phẩm</td>";
    echo "<td width=300>" . $row1['product_desc'] . "</td>";
    echo "<td width=300>" . $row2['product_desc'] . "</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>Hình Ảnh</td>";
    echo "<td align=center>" . $row1['product_image'] . "</td>";
    echo "<td align=center>" . $row2['product_image'] . "</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>Giá thị trường</td>";
    echo "<td align=center>" . $row1['product_mar_price'] . "</td>";
    echo "<td align=center>" . $row2['product_mar_price'] . "</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>LUBE CENTER bán</td>";
    echo "<td align=center>" . $row1['product_price'] . "</td>";
    echo "<td align=center>" . $row2['product_price'] . "</td>";
    echo "</tr>";

    }

    echo "</table>";

    Could you fix it for me? Thks a lot

    Sam
     
    lenhhoxung, Jan 1, 2010 IP
  2. CoreyPeerFly

    CoreyPeerFly Notable Member Affiliate Manager

    Messages:
    394
    Likes Received:
    24
    Best Answers:
    5
    Trophy Points:
    240
    #2
    Are any errors showing up? Is that one page or two? You need to echo out the form if that's one page.
     
    CoreyPeerFly, Jan 1, 2010 IP
  3. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    My guess would be this:

    $result1 = mysql_query("SELECT * FROM product WHERE (product_sku=($fname))");
    $result2 = mysql_query("SELECT * FROM product WHERE (product_sku=($fname2))");
    Code (markup):
    Change it to:

    $result1 = mysql_query("SELECT * FROM `product` WHERE `product_sku` = '{$_POST['fname']}';");
    $result2 = mysql_query("SELECT * FROM `product` WHERE `product_sku` = '{$_POST['fname2']}';");
    Code (markup):
    Also, try adding this code at the top of your PHP script while you debug:
    error_reporting(E_ALL);
    Code (markup):
     
    Last edited: Jan 2, 2010
    CodedCaffeine, Jan 1, 2010 IP
  4. GSXell

    GSXell Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hm what should your script being for? I think there is a more comfortable way to do this. But Caffeine should have posted the solution yet. But he should use the fucking ` the are only used if you use reserved words for rowname, what would a good databasedesigner never do.
     
    GSXell, Jan 2, 2010 IP
  5. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Lolwut? I'm not really understanding what you're saying.
     
    CodedCaffeine, Jan 2, 2010 IP
  6. GSXell

    GSXell Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Sorry forgotten a not! This is correct product not `product`. You use the second version if your column is named like a reserved word for example time etc. :)
     
    GSXell, Jan 2, 2010 IP
  7. jobykjoseph10

    jobykjoseph10 Well-Known Member

    Messages:
    194
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    130
    #7
    Check whether $result1 and $result2 is empty or not.
     
    jobykjoseph10, Jan 2, 2010 IP
  8. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I know that its not required, but still retrieves the data at the same speed, and I find that it improves the readability.
     
    CodedCaffeine, Jan 2, 2010 IP