这个为什么插入不了数据?
程序员文章站
2022-04-29 19:24:52
...
$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')";
你的txt应该是字符串类型的字段,记得加单引号
没有什么错误提示么?换种写法试试
// 预处理
$sql = "INSERT INTO `user`(`txt`) VALUES (:txt)";
$stmt = $pdo->prepare($sql);
$stmt->execute([':txt' => $txt]);