Page 1 of 1
PHP mysql connection
Posted: Sat Aug 16, 2014 9:41 am
by terrenuit
Hi,
I have a problem in my shopping cart script,
my server indicated this line: $result = mysql->query('SELECT * FROM trybase WHERE name= '.$_POST['name']);
error:
SCREAM: Error suppression ignored for
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in C:\wampserver32\www\sale\ajout_panier.php on line 6
I wish someone can help me !
Code: Select all
<?php
session_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');
?>
Re: PHP mysql connection
Posted: Sat Aug 16, 2014 11:30 am
by Celauran
The class is mysqli, not Mysqli. More importantly, you've omitted the $ before the variable name on line 6. mysql->query should be $mysql->query.
Finally, and this is unrelated to the error you've encountered but is very important anyway, you're not escaping your inputs. Whatever is in $_POST['name'] goes straight into your query. If someone were to use, say, '; DROP TABLE users; as their name, you'd have a bad time. At the very least escape your inputs, but you really ought to be using prepared statements.
Re: PHP mysql connection
Posted: Sat Aug 16, 2014 2:19 pm
by terrenuit
@Celauran
thank you for your reply,
I have modified serveral times,
now, the result is the line 6 is OK, but the error is in the line 7,
error: Fatal error: Call to a member function fetch_assoc() on a non-object in C:\wampserver32\www\sale\ajout_panier.php on line 7
it's not easy for me.
Code: Select all
<?php
session_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');
?>
Re: PHP mysql connection
Posted: Sat Aug 16, 2014 2:42 pm
by Celauran
Which means $result isn't an object, so $mysql->query probably returned false. This typically indicates a problem with the query itself. In this case, there should probably be quotes around the username. $mysql->error should provide you with information about what went wrong.
Re: PHP mysql connection
Posted: Sun Aug 17, 2014 6:52 am
by terrenuit
I have corrected my script ajout_panier.php
now, it is OK, but the last script(votre_script) still has problem in the line 7:
Undefined variable: listproduit in C:\wampserver32\www\sale\votre_panier.php on line 7
the 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
Code: Select all
<?php
session_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>
Re: PHP mysql connection
Posted: Sun Aug 17, 2014 7:08 am
by Celauran
Where is it defined? I don't see it having been initialized anywhere, so unless it's defined in panier.php and imported, that notice is exactly what I'd expect.
Re: PHP mysql connection
Posted: Sun Aug 17, 2014 9:24 am
by terrenuit
I have already solved the problem, thanks a lot