我无法用此插入数据。
这是我的代码:
<?php
include('includes/config.php');
if(isset($_POST['update'])) {
$sampleid=$_POST['sampleid'];
$aba11=$_POST['aba11'];
$aba12=$_POST['aba12'];
$sql="INSERT INTO 'aba1'(sampleid,aba11,aba12) VALUES(:sampleid,:aba11,:aba12)";
$query = $dbh->prepare($sql);
$query->bindParam(':sampleid',$sampleid,PDO::PARAM_STR);
$query->bindParam(':aba11',$aba11,PDO::PARAM_STR);
$query->bindParam(':aba12',$aba12,PDO::PARAM_STR);
$query->execute();
$lastInsertId = $dbh->lastInsertId();
}
?>
<html>
<form>
<body>
<label> Sampleid </label> <input type="text" name="sampleid"><br>
<label> Start time </label> <input type="text" name="aba11"><br>
<label> Stoptime </label> <input type="text" name="aba12"><br>
<button type="submit" name="update" >Update</button>
</Form>
</body>
</html>
我的数据库连接是正确的。 config.php 文件没有错误。
最佳答案
您的表单方法缺失,并且更新按钮中的值缺失
此外,您必须使用 ` 而不是 ' 作为数据库表名称
尝试下面的代码
<?php
include('includes/config.php');
if(isset($_POST['update'])) {
$sampleid=$_POST['sampleid'];
$aba11=$_POST['aba11'];
$aba12=$_POST['aba12'];
$sql="INSERT INTO `aba1` (sampleid,aba11,aba12) VALUES(:sampleid,:aba11,:aba12)";
$query = $dbh->prepare($sql);
$query->bindParam(':sampleid',$sampleid,PDO::PARAM_STR);
$query->bindParam(':aba11',$aba11,PDO::PARAM_STR);
$query->bindParam(':aba12',$aba12,PDO::PARAM_STR);
$query->execute();
$lastInsertId = $dbh->lastInsertId();
}
?>
<html>
<body>
<form method="POST" action="">
<label> Sampleid </label> <input type="text" name="sampleid"><br>
<label> Start time </label> <input type="text" name="aba11"><br>
<label> Stoptime </label> <input type="text" name="aba12"><br>
<button type="submit" name="update" value="1">Update</button>
</Form>
</body>
</html>
关于php - 向mysql表中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58335196/