php - 向mysql表中插入数据

标签 php mysql

我无法用此插入数据。
这是我的代码:

<?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/

相关文章:

php - #1064 - 将 .ibd 或 .frm 文件导入数据库时​​出错

php - 允许的最大 cookie 数

php - MySQL 将用户插入到另一个用户拥有的组中

mysql - 关于获取重复行总和的 SQL 问题

php - 将 php sql 查询 while 循环转换为 for 循环

php - 合并两个表,按日期排序,有限制

php - 动态切换图像(使用 PHP)以及 CSS 媒体查询?

php - CakePHP 2.x 插件和插件文件夹之间有什么区别?

php - 比较两个sql表

mysql - 如何中止 MySql 触发器中的 INSERT 操作?