PHP mysql connection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
terrenuit
Forum Commoner
Posts: 53
Joined: Tue Jul 08, 2014 2:18 pm

PHP mysql connection

Post 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');
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP mysql connection

Post 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.
terrenuit
Forum Commoner
Posts: 53
Joined: Tue Jul 08, 2014 2:18 pm

Re: PHP mysql connection

Post 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');
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP mysql connection

Post 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.
terrenuit
Forum Commoner
Posts: 53
Joined: Tue Jul 08, 2014 2:18 pm

Re: PHP mysql connection

Post 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>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP mysql connection

Post 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.
terrenuit
Forum Commoner
Posts: 53
Joined: Tue Jul 08, 2014 2:18 pm

Re: PHP mysql connection

Post by terrenuit »

I have already solved the problem, thanks a lot
Post Reply