Louffeman Posted August 16, 2014 Share Posted August 16, 2014 Hi,I have a problem of my shopping cart script,my server indicate the error in this line: $produit = $result->fetch_assoc();error: Fatal error: Call to a member function fetch_assoc() on a non-object in C:wampserver32wwwsaleajout_panier.php on line 7je d'ont know how to solve it,anyone can help me please ?<?phpsession_start();$mysql = new mysqli('localhost', 'root', '', 'testebase');$result = $mysql->query('SELECT * FROM trybase WHERE name= '.$_POST['name']);$produit = $result->fetch_assoc();require_once 'panier.php';$valeur = array('name'=>$produit['name'],'price'=>$produit['price'],'quantity'=>$_POST['quantity']);$panier->set($_POST['name'], $valeur);header('location: votre_panier.php');?> Link to comment Share on other sites More sharing options...
JamesB Posted August 16, 2014 Share Posted August 16, 2014 you need apostrophes around the value to indicate a MySQL string: $result = $mysql->query("SELECT * FROM trybase WHERE name= '".$_POST['name']."'"); Link to comment Share on other sites More sharing options...
Louffeman Posted August 16, 2014 Author Share Posted August 16, 2014 @JamesBthank you for your reply, I did as you said,but now, the error is in the line 5: $mysql = new mysqli('localhost', 'root', '', 'testebase');Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE in C:wampserver32wwwsalepanier.php on line 5 Link to comment Share on other sites More sharing options...
JamesB Posted August 16, 2014 Share Posted August 16, 2014 do you have an apostrophe in your password? if so you need to escape all apostrophes used by putting a backslash before them, eg. $mysql = new mysqli('localhost', 'root', 'my'pass'word', 'testebase'); Link to comment Share on other sites More sharing options...
Louffeman Posted August 17, 2014 Author Share Posted August 17, 2014 yes, as you see:$mysql = new mysqli('localhost', 'root', '', 'testebase'); Link to comment Share on other sites More sharing options...
Ingolme Posted August 17, 2014 Share Posted August 17, 2014 Make sure that the syntax is correct on the lines before line 5. I don't see an error on that line in particular. Link to comment Share on other sites More sharing options...
Louffeman Posted August 17, 2014 Author Share Posted August 17, 2014 @Ingolmethe server has no any error indicate before the line 5 Link to comment Share on other sites More sharing options...
JamesB Posted August 17, 2014 Share Posted August 17, 2014 yes, as you see:$mysql = new mysqli('localhost', 'root', '', 'testebase'); When i said to escape the apostrophes, this excludes the first and last per string, as shown in my example: 'my'pass'word' @Ingolmethe server has no any error indicate before the line 5 Sometimes the error line is off by 1 line, can you post your previous line of code. Link to comment Share on other sites More sharing options...
Louffeman Posted August 17, 2014 Author Share Posted August 17, 2014 I have corrected my script ajout_panier.phpnow, it is OK, but the last script(votre_script) still has problem in the line 7:Undefined variable: listproduit in C:wampserver32wwwsalevotre_panier.php on line 7the line 7 is: <?php if(!$listproduit){?>the output is the cart is empty, which mean's it didn't receive anything ?it is votre_script.php<?phpsession_start();require_once 'panier.php';$panier = new Panier('produits');?><?php if(!$listproduit){?><p> The cart is empty </p><?php } else {?><table border="1" width="50%"><tr><td>Name</td><td>Price</td><td>Quantity</td></tr><?php foreach($listproduit as $produit) { ?><tr><td><?php print $produit['name'] ?></td><td><?php print $produit['price'] ?></td><td><?php print $produit['quantity'] ?></td></tr><?php } ?></table><?php } ?><p><a href="index.php">Home</a></p> Link to comment Share on other sites More sharing options...
JamesB Posted August 17, 2014 Share Posted August 17, 2014 You can either set $listproduit to false before it is conditionally set to true, or use if(!isset($listproduit)) Link to comment Share on other sites More sharing options...
Louffeman Posted August 17, 2014 Author Share Posted August 17, 2014 I have already solved the problem, thanks a lot Link to comment Share on other sites More sharing options...
justsomeguy Posted August 18, 2014 Share Posted August 18, 2014 $result = $mysql->query("SELECT * FROM trybase WHERE name= '".$_POST['name']."'");This is terrible practice, this is no better than using the deprecated mysql extension. Use prepared statements so that you don't have problems like this.https://www.google.com/search?client=opera&q=mysqli+prepared+statements+tutorial&sourceid=opera&ie=UTF-8&oe=UTF-8 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now