简体   繁体   中英

PHP PDO Simple insert does not work

I am converting all of my query's to PDO, and i'm new to it. It's properly a very stupid question but why does the following code not work?

try {
    $conn = new PDO('mysql:host=localhost;dbname=ddd', $user, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}
                $id = $_SESSION['id'];
                $name = $_POST['name'];
                $stmt = $pdo->prepare('INSERT INTO projects 
                                      (group_id, project_name)
                                       VALUES (:id, :name)');
                $stmt->execute(array(
                             ':id'=>$id,
                             ':name'=>$name
                             ));

Thanks.

Your connection variable is $conn and you are preparing your PDO Statement using $pdo->prepare .

Change to $conn->prepare()

$stmt = $conn->prepare('INSERT INTO projects 
      (group_id, project_name)
       VALUES (:id, :name)');

You're initializing a variable for your database connection called $conn yet later call $pdo that's not mentioned anywhere. That's the first thing I'd start with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM