Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PDO prepared insert - does not insert data and show no errors [duplicate]

This problem is driving me crazy, i tried everything. Is does not give me any error, but it does not insert anything to the database either. Database connection is good, and there should be no typos. Please take a look, and see if you can find the problem:

$err = array();

if (isset($_POST['submit'])) {

    $ip = gethostbyname($_SERVER['REMOTE_ADDR']);
    $date = "2012-02-02 02:02:02"; //Example
    $uploader_name = $_POST['uploader_name'];

    // Validation happens here...


    if (empty($err)) {

        $host = "host";
        $dbname = "db";
        $user = "user";
        $pass = "pass";

        try {
            $dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);  

            $sql = "INSERT INTO `table` (`ip`, `date`, `uploader_name`) 
                    VALUES (:ip, :date, :uploader_name)";
            $stmt = $dbh->prepare($sql);

            # the data we want to insert  
            $params = array(
                ':ip' => $ip,
                ':date' => $date,
                ':uploader_name' => $uploader_name
            );


            $stmt->execute($params);

            $dbh = null;

        } catch(PDOException $pe) {
            die('SQL Error');
        }


        if (empty($err)) {
            $err[] = "Success!";
        }   
    }

}

Also, Im sure it gets to the insert part, because i get the 'Success' message.

like image 647
2by Avatar asked May 14 '26 20:05

2by


1 Answers

Use this code to execute your statement. If there is a non-fatal error, it will display it.

$stmt->execute($params) or die(print_r($stmt->errorInfo(), true));

Almost certainly your db user does not have permissions to execute the statement you're asking against the table you're trying to execute against.

like image 200
Nathaniel Ford Avatar answered May 17 '26 09:05

Nathaniel Ford



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!