0

Can someone with experience with PDO help me on this? I've spent like 6 hours but I am still unable to get a query running.

Here is my code:

<?php

include('database.php');

//print_r($_GET);

if(isset($_GET['ctype']))
{
    $ctype = $_GET['ctype'];
}
else
{
    die("Could determine type of input");
}

if($ctype==1)
{
//it is client
    if((isset($_GET['uid'])) && (isset($_GET['name'])) && (isset($_GET['address'])) && (isset($_GET['city'])) && (isset($_GET['state'])) && (isset($_GET['zip'])))
    {
        try 
        {

            $conn = new PDO("mysql:host=$host;dbname=$database", $username, $password);         
            //$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);           
            $sql = "INSERT INTO clients (uid, name, address, city, state, zip) VALUES(:uid, :name, :address, :city, :state, :zip)";                         
            $stmt = $conn->prepare($sql);                               
            $stmt->execute(array(
            ':uid' => $_GET['uid'],
            ':name' => $_GET['name'],
            ':address' => $_GET['address'],
            ':city' => $_GET['city'],
            ':state' => $_GET['state'],
            ':zip' => $_GET['zip']      
            ));
            echo $stmt->rowCount(); // 1            
        } 
        catch(Exception $e) 
        {
            echo 'Error: ' . $e->getMessage();
        }

    }   
}?>

database.php being:

<?php
//Please Enter database settings here
$username = "root";
$password = "";
$database="letters";
$host="localhost";
?>

I hit the page with this GET request

http://localhost/letters/contact_add.php?ctype=1&uid=mmm&name=test&address=6182&city=xyz&state=abc&zip=123456

Error I am getting:

Parse error: in C:\xampp\xampp\htdocs\letters\contact_add.php on line 26

Line 26 being:

$sql = "INSERT INTO clients (uid, name, address, city, state, zip) VALUES (:uid, :name, :address, :city, :state, :zip)";

I have browsed almost any link I could get on google and still unable to find the problem. the link which I followed is this. I am running xampp 1.8.1 and extension=php_pdo_mysql.dll is enabled.

Ajit
  • 336
  • 2
  • 12
  • Parse error means you have an error in your **PHP code**, not in whatever PDO interaction – Your Common Sense Feb 07 '13 at 16:04
  • Yup that was my first though but look at line 26, its basically a string assignment so I thought those values with ":" might be responsible for it? – Ajit Feb 07 '13 at 16:07
  • I have tested your code on a local MAMP server and worked first time. I think your issue is more likely to be your setup. More specifically PDO. Maybe see http://stackoverflow.com/questions/6113262/how-to-determine-if-pdo-is-enabled-in-php/6113496#6113496 – Damon Skelhorn Feb 07 '13 at 16:12
  • what version of PHP are you running? Also, does it include the PDO extension? (check what extensions are loaded with `php_info()`). – SDC Feb 07 '13 at 16:28
  • A parse error will usually have more info than you've given -- eg it might say `parse error: unexpected EOF at line X` or something like that. Can you tell us the rest of the error message? Also, many parse errors are actually due to errors on previous lines; it's unlikely to be the actual line that is reported; that's just where the earlier errors finally got to the point where it was unparseable, and PHP gave up. – SDC Feb 07 '13 at 16:31
  • I copy pasted the exacted Error message from browser. – Ajit Feb 07 '13 at 16:42
  • And I guess xampp is being faulty here because when I run php_info() I get : Fatal error: Call to undefined function php_info() in C:\xampp\htdocs\letters\contact_add.php on line 4. – Ajit Feb 07 '13 at 16:43
  • 1
    it's `phpinfo()` - without an underscore – Your Common Sense Feb 07 '13 at 17:50

1 Answers1

0

There are no parse errors in the code you posted. So, you're running some other code then.
Please, double-check the file you're actually running.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • I'm checking the code with new installation of xampp now. Unfortunately, the file is correct ( this is my only project in htdocs) and If I press enter before the line 26, error shifts to line 27 ( I do have some common sense :)). I really appreciate your help, gonna post results in few minutes! – Ajit Feb 07 '13 at 16:23