Home > Article > Backend Development > Why can't I insert data?
$pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
$txt=$_POST["txt"];
$sql="insert into user(txt)values($txt)";
$pdo->exec($sql);
$pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
$txt=$_POST["txt"];
$sql="insert into user(txt)values($txt)";
$pdo->exec($sql);
$sql="insert into user(txt)values('$txt')";
Your txt should be a string type field, remember to add single quotes
Is there any error message? Try another way of writing
// 预处理
$sql = "INSERT INTO `user`(`txt`) VALUES (:txt)";
$stmt = $pdo->prepare($sql);
$stmt->execute([':txt' => $txt]);