Home > Backend Development > PHP Tutorial > Why can't I insert data?

Why can't I insert data?

WBOY
Release: 2016-08-20 09:04:07
Original
1188 people have browsed it

<code>$pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
$txt=$_POST["txt"];
$sql="insert into user(txt)values($txt)";
$pdo->exec($sql);</code>
Copy after login
Copy after login

Reply content:

<code>$pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
$txt=$_POST["txt"];
$sql="insert into user(txt)values($txt)";
$pdo->exec($sql);</code>
Copy after login
Copy after login

<code>$sql="insert into user(txt)values('$txt')";</code>
Copy after login

Your txt should be a string type field, remember to add single quotes

Is there any error message? Try another way of writing

<code>// 预处理
$sql = "INSERT INTO `user`(`txt`) VALUES (:txt)";
$stmt = $pdo->prepare($sql);
$stmt->execute([':txt' => $txt]);</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template