Jump to content

PDO Inserting Data?


Go to solution Solved by glassfish,

Recommended Posts

This UPDATE script works:

$query = $pdo->prepare("UPDATE posts SET headline = ? WHERE id = ?");
    $query->bindValue("1", $_POST['headline']);
    $query->bindValue("2", "2");
    $query->execute();

Though, this INSERT script does not work:

$query = $pdo->prepare("INSERT INTO posts (headline, post) VALUES (?, ?)");
    $query->bindValue("1", $_POST['headline']);
    $query->bindValue("2", $_POST['post']);
    $query->execute();

Nothing gets added, when executing this script.

 

The connection:

<?php

$dsn = "mysql:dbname=phpblog;host=localhost";
$user = "root";
$password = "";
try{
    $pdo = new PDO($dsn, $user, $password);
}catch(PDOException $e){
    echo "Connection failed: " . $e->getMessage();
}

?>

Any suggestions why the INSERT script is not working?

Edited by glassfish
Link to comment
https://forums.phpfreaks.com/topic/292703-pdo-inserting-data/
Share on other sites

i'm going to guess you don't have a column in your table named post and that the prepare() statement is failing with an error, assuming that emulated prepares are turned off, or the execute() statement is failing with an error, assuming that emulated prepared are not turned off (which unfortunately is the default.)

 

well written code is self-troubleshooting. it will tell you when, where, and why it is failing. to get your code to tell you when, where, and why it is failing, your code must have error checking logic in it.

 

you can either add conditional logic to test the result of each statement to find out if it is working or not or you can use exceptions. i recommend using exceptions for fatal problems.

Link to comment
https://forums.phpfreaks.com/topic/292703-pdo-inserting-data/#findComment-1497628
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.