0

Why PDO is not working in wamp ? I think my php is code is fine but I am not able to insert and get data from the MySQL database, how do I fix it ?

<?php 

$db = new PDO('mysql:host=localhost;dbname=ajaxdata','root','');

$page = isset($_GET['p'])?$_GET['p']:'';
if($page=='add') {
    $name = $_POST['nm'];
    $email = $_POST['em'];
    $phone = $_POST['pn'];
    $address = $_POST['ad'];
    $stmt = db->prepare("INSERT INTO info VALUES('',?,?,?,?)");
    $stmt->bindParam(1,$name);
    $stmt->bindParam(2,$email);
    $stmt->bindParam(3,$phone);
    $stmt->bindParam(4,$address);
    if($stmt->execute()){
        echo "Success add data";
    } else {
        echo "Fail add data";
    }
} else if($page=='edit'){

} else if($page=='del'){

} else{
    $stmt = $db->prepare("SELECT * FROM info");
    $stmt->execute();
    while($row = $stmt->fetch()){
        ?>
        <tr>
            <td><?php echo $row['id'] ?></td>
            <td><?php echo $row['name'] ?></td>
            <td><?php echo $row['email'] ?></td>
            <td><?php echo $row['phone'] ?></td>
            <td><?php echo $row['address'] ?></td>
            <td>
                <button class="btn btn-warning">Edit</button>
            </td>

        </tr>
        <?php
    }
}
?>

I don't know how to fix this error from wamp, so please tell me what to do.

martinBibo
  • 15
  • 2
  • Why is the character `\` after `;` in db connection statement? And what's the error that you are getting ? – Yaman Jain Mar 12 '17 at 07:47
  • Can you provide us the exact error you are getting or any log file. That will be more helpful – Aman Rawat Mar 12 '17 at 07:49
  • I can't insert of retrieve data from the database, I don't know what is the error. – martinBibo Mar 12 '17 at 07:51
  • @martinBibo if you dont know what errors,try turn on showing errors. ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); – DenisOgr Mar 12 '17 at 08:01
  • This is all wrong! You're mixing 'PDO with MySQLi' and 'PDO with Prepared Statements'! – The Codesee Mar 12 '17 at 09:24
  • http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – apokryfos Mar 12 '17 at 09:28
  • @TheCodesee For future reference using parameters `?` and numeric references in `$stmt->bindParam(1,$name);` is and has always been valid PDO. See [the manual examples](http://php.net/manual/en/pdostatement.bindparam.php) – RiggsFolly Mar 12 '17 at 16:11

1 Answers1

1

you are missing the dollar sign here in calling your db object variable:

$stmt =  db->prepare("INSERT INTO info VALUES('',?,?,?,?)");
//      ^

which is must be as follows:

$stmt = $db->prepare("INSERT INTO info VALUES('',?,?,?,?)");
hassan
  • 7,812
  • 2
  • 25
  • 36