0

I'm pretty new in PHP coding, and I cannot find the error in the following code... could you help me please ?

Fatal error: Cannot use object of type PDOStatement as array in C:\wamp\www\membre\inscription_post.php on line 14 Call Stack # Time Memory Function Location 1 0.0006 682688 {main}( ) ..\inscription_post.php:0

<?php
try
{
$bdd = new PDO('mysql:host=localhost;dbname=test', 'root', '');
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
$mem = $bdd -> query('SELECT * FROM membres');

while ($data = $mem -> fetch())
{
    if($mem['pseudo'] == $_POST['pseudo'])
    {
        echo "Pseudo existant";
    }
    else
    {
        $pass_hache = sha1($_POST['pass']);
        $req = $bdd->prepare('INSERT INTO membres (pseudo, pass, email, date_inscription) VALUES(?, ?, ?, CURDATE())');
        $req->execute(array($_POST['pseudo'], $pass_hache, $_POST['email']));
        header('Location: inscription.php');
        echo "Membre ajouté";
    }
}
$mem -> closeCursor();

?>

membres table structure is the following

id(=INT, primary key), 
pseudo (VARCHAR(255)), 
pass (VARCHAR(255)), 
date_inscription (date)

Thank you for your help

2
  • here is the error : ( ! ) Fatal error: Cannot use object of type PDOStatement as array in C:\wamp\www\membre\inscription_post.php on line 14 Call Stack # Time Memory Function Location 1 0.0006 682688 {main}( ) ..\inscription_post.php:0 Commented Mar 4, 2013 at 15:18
  • while ($data = $mem -> fetch(PDO::FETCH_ASSOC)) Commented Mar 4, 2013 at 15:22

3 Answers 3

2

something like this

<?php
$dsn = 'mysql:host=localhost;dbname=test;charset=utf8';
$opt = array(
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$bdd = new PDO($dsn,'root','', $opt);

$stm = $bdd->prepare('SELECT 1 FROM membres WHERE pseudo=?');
$stm->execute(array($_POST['pseudo']));
$row = $stm->fetch();
if ($row) {
    echo "Pseudo existant";
} else {
    $pass_hache = sha1($_POST['pass']);
    $sql = 'INSERT INTO membres VALUES(NULL, ?, ?, ?, CURDATE())';
    $req = $bdd->prepare($sql);
    $req->execute(array($_POST['pseudo'], $pass_hache, $_POST['email']));
    header('Location: inscription.php');
}

insert query can be wrong, depends on the table schema

Sign up to request clarification or add additional context in comments.

Comments

2
if($mem['pseudo'] == $_POST['pseudo'])
    ^^^--- should be $data instead

But this is bad code. You're basically buying up the entire contents of a grocery store (your members table), driving it all home, then throwing away everything EXCEPT the one chocolate bar you wanted. You should be doing this in the DB, essentially

SELECT * FROM members WHERE pseudo=...

Comments

0

if($mem['pseudo'] == $_POST['pseudo']) should be if($data['pseudo'] == $_POST['pseudo']) as you are reading each line from $mem as $data. However, this doesn't seem like the best option for doing what it looks like you are trying to do - why not search for $_POST['pseudo'] in the database directly instead of looping through them all?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.