Home  >  Q&A  >  body text

Insertion into database failed

<?php

// pdo: preprocessing

// The essence of preprocessing: the data in the sql statement is dynamically bound

// Dynamic binding: real data is bound only when sql is executed

// Static binding: data is written directly to sql

// 1. Static: select * from staff where id > 10

// 2. Dynamic (preprocessing): select * from staff where id > ?

##// 1. Anonymous parameter index array

namespace pdo_edu;

use PDO;

// Connection

$db = new PDO('mysql:dbname=bittel', 'root', 'root');

// CURD: INSERT

// Anonymous parameters: ?

$sql = 'INSERT `staff` SET `name`= ?,`sex`= ?,`email`= ?;';

// sql statement->sql statement template object->preprocessing object

$stmt = $db->prepare($sql);

/ / Placeholder in sql statement?, bind real data to it

// Index array

$data = ['Yangguo', 0, 'yangguo@qq.com'] ;

//Execute sql

$stmt->execute($data);

// Verification: Print sql preprocessing command

// $stmt->debugDumpParams();

echo 'Added successfully, id = ' . $db->lastInsertId () . '<br>';

QQ图片20220819212656.png

P粉314265155P粉314265155707 days ago1103

reply all(3)I'll reply

  • autoload

    autoload2022-08-19 22:06:43

    QQ截图20220819220306.png

    This is my field type, you can insert it normally using your code

    image.png

    reply
    0
  • P粉314265155

    If the ID is equal to 0, the insertion has not been successful. There is no data in the database and the iD value has not changed.

    P粉314265155 · 2022-08-20 07:50:22
  • autoload

    autoload2022-08-19 21:56:41

    What error was reported?

    reply
    0
  • Cancelreply