Jump to content

help please im newbie in PHP


UnknownZ

Recommended Posts

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\cart\product.php on line 72

 

 

<?php
if(isset($_SESSION['cart'])){
$sql = "SELECT * FROM products WHERE id IN(";
foreach($_SESSION['cart'] as $id => $value){
$sql .= $id. ",";
}
$sql = substr($sql,0,-1). ") ORDER BY id ASC";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)){          [size=6]  <<<<< Line 72 HERE[/size]
?>
<p><?php echo $row['product_name']; ?><?php echo" " .$_SESSION['cart'][$row['id']]['quantity'];?></p>
<?php
}
}else {
echo "<p>Your cart is empty. <br />Please add some products</p>";
}
echo   "<a href='product.php?page=cart'>Go to Cart</a>";
?>
Edited by ignace
Code tags please
Link to comment
Share on other sites

The issue is not really on line 72, its on line 70:

 

$query = mysql_query($sql);

 

this is returning FALSE, which is a BOOLEAN and not a resource. mysql_fetch_assoc() can not proccess such boolean and in return, it fails. Use this line and see what pops up:

 

$query = mysql_query($sql) or die(mysql_error());

 

This will echo the mysql_error() when mysql_query() fails. I presume you are facing a query issue.

Link to comment
Share on other sites

The issue is not really on line 72, its on line 70:

 

$query = mysql_query($sql);

 

this is returning FALSE, which is a BOOLEAN and not a resource. mysql_fetch_assoc() can not proccess such boolean and in return, it fails. Use this line and see what pops up:

 

$query = mysql_query($sql) or die(mysql_error());

 

This will echo the mysql_error() when mysql_query() fails. I presume you are facing a query issue.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY id ASC' at line 1

 

now this one shows up after putting it. 

here is my code

<?php
 session_start();
 require_once("storescripts/connect_to_mysql.php");
 if(isset($_GET['page'])){
$pages = array("prod","cart");
if(in_array($_GET['page'],$pages)){
$page = $_GET['page'];
} else {
$page = "prod";
}
}else {
$page = "prod";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[url=http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/url]"[]>
<html xmlns="[url=http://www.w3.org/1999/xhtml]http://www.w3.org/1999/xhtml[/url]" dir="ltr" lang="en-US" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Home</title>
    <link rel="stylesheet" href="style.css" type="text/css" media="screen" />
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="script.js"></script>
 
</head>
<body>
<div id="art-main">
    <div class="cleared reset-box"></div>
    <div class="art-box art-sheet">
        <div class="art-box-body art-sheet-body">
            <div class="art-header">
                <div class="art-logo">
                                                                        </div>
                
            </div>
            <div class="cleared reset-box"></div>
<div class="art-bar art-nav">
<div class="art-nav-outer">
<ul class="art-hmenu">
<li>
<a href="index.php" class="active">Home</a>
</li> 
<li>
<a href="product.php">Product</a>
</li>
 
<li>
<a href="about.html">About</a>
</li> 
</ul>
</div>
</div>
<div class="cleared reset-box"></div>
<div class="art-layout-wrapper">
                <div class="art-content-layout">
                    <div class="art-content-layout-row">
                        <div class="art-layout-cell art-sidebar1">
<div class="art-box art-vmenublock">
    <div class="art-box-body art-vmenublock-body">
                <div class="art-bar art-vmenublockheader">
<h3 class="t">Cart</h3>
                </div>
                <div class="art-box art-vmenublockcontent">
                    <div class="art-box-body art-vmenublockcontent-body">
                 <?php
if(isset($_SESSION['cart'])){
$sql = "SELECT * FROM products WHERE id IN(";
foreach($_SESSION['cart'] as $id => $value){
$sql .= $id. ",";
}
$sql = substr($sql,0,-1). ") ORDER BY id ASC";
$query = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($query)){
?>
<p><?php echo $row['product_name']; ?><?php echo" " .$_SESSION['cart'][$row['id']]['quantity'];?></p>
<?php
}
}else {
echo "<p>Your cart is empty. <br />Please add some products</p>";
}
echo   "<a href='product.php?page=cart'>Go to Cart</a>";
?>
                   <div class="cleared"></div>
                    </div>
                </div>
<div class="cleared"></div>
    </div>
</div>
<div class="art-box art-block">
    <div class="art-box-body art-block-body">
                <div class="art-bar art-blockheader">
                    <h3 class="t">Login</h3>
                </div>
                <div class="art-box art-blockcontent">
                    <div class="art-box-body art-blockcontent-body">
                <div>
</div>
                
                                 <div class="cleared"></div>
                    </div>
                </div>
<div class="cleared"></div>
    </div>
</div>
 
                          <div class="cleared"></div>
                        </div>
                        <div class="art-layout-cell art-content">
<div class="art-box art-post">
<div class="art-box-body art-post-body"><?php require($page . ".php");?>
                              
                <div class="cleared"></div>
 
 
 
    </div>
</div>
<div class="art-box art-post">
    <div class="cleared"></div>
 
<div class="cleared"></div>
    </div>
</div>
 
                          <div class="cleared"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="cleared"></div>
            <div class="art-footer">
                <div class="art-footer-body">
                            <div class="art-footer-text">anonymous</div>
                    <div class="cleared"></div>
                </div>
            </div>
     <div class="cleared"></div>
        </div>
    </div>
    <div class="cleared"></div>
</div>
 
</body>
</html>
Edited by ignace
2x code tags please
Link to comment
Share on other sites

Well, its obvious that the SQL is indeed not correct. It would be easier to recognize if you did this:

 

$sql = substr($sql,0,-1). ") ORDER BY id ASC";
$query = mysql_query($sql) or die(mysql_error());
 
In between those lines, perform a var_dump($sql);die();. So:
 
$sql = substr($sql,0,-1). ") ORDER BY id ASC";
var_dump($sql);die();
$query = mysql_query($sql) or die(mysql_error());
 
This will show the entire SQL query without actually running it. Please post it back here!
Link to comment
Share on other sites

Well, its obvious that the SQL is indeed not correct. It would be easier to recognize if you did this:

 

 

$sql = substr($sql,0,-1). ") ORDER BY id ASC";
$query = mysql_query($sql) or die(mysql_error());

 

In between those lines, perform a var_dump($sql);die();. So:

 

 

$sql = substr($sql,0,-1). ") ORDER BY id ASC";
var_dump($sql);die();
$query = mysql_query($sql) or die(mysql_error());

 

This will show the entire SQL query without actually running it. Please post it back here!

 

 

 

 

 

 

 

Cart

 

string(51) "SELECT * FROM products WHERE id IN) ORDER BY id ASC" 

 

this is the error now 

 

 

$sql = substr($sql,0,-1). ") ORDER BY id ASC";

var_dump($sql);die(); <<<after putting this

$query = mysql_query($sql) or die(mysql_error());

Edited by ignace
Link to comment
Share on other sites

I see. The issue you are facing is caused because you have nothing in your cart

 

"SELECT * FROM products WHERE id IN) ORDER BY id ASC"

 

is not a valid SQL query. It is supposed to be:

 

"SELECT * FROM products WHERE id IN (id1, id2) ORDER BY id ASC" <-- In case of Ids

 

OR

 

"SELECT * FROM products ORDER BY id ASC" <-- in case of no Ids

 

So you need to edit this block of code to get the above result:

 

 

if(isset($_SESSION['cart'])){

$sql = "SELECT * FROM products WHERE id IN(";
foreach($_SESSION['cart'] as $id => $value){
$sql .= $id. ",";
}
$sql = substr($sql,0,-1). ") ORDER BY id ASC";
 
In summary: Right now you re always adding the WHERE IN, while in fact you are not supposed to add that if there are no IDs in the cart.

 

Let me know if you do not understand or dont know where to start.

Edited by ignace
Link to comment
Share on other sites

I see. The issue you are facing is caused because you have nothing in your cart

 

"SELECT * FROM products WHERE id IN) ORDER BY id ASC"

 

is not a valid SQL query. It is supposed to be:

 

"SELECT * FROM products WHERE id IN (id1, id2) ORDER BY id ASC" <-- In case of Ids

 

OR

 

"SELECT * FROM products ORDER BY id ASC" <-- in case of no Ids

 

So you need to edit this block of code to get the above result:

 

 

if(isset($_SESSION['cart'])){

$sql = "SELECT * FROM products WHERE id IN(";
foreach($_SESSION['cart'] as $id => $value){
$sql .= $id. ",";
}
$sql = substr($sql,0,-1). ") ORDER BY id ASC";
 

In summary: Right now you re always adding the WHERE IN, while in fact you are not supposed to add that if there are no IDs in the cart.

 

Let me know if you do not understand or dont know where to start.

sorry for late reply.. the power was out yesterday. where to start exactly?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.