简体   繁体   中英

Insert data on mysql table

I'm not able to insert the data with this.
Here is my code:

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

My database connection is correct. There is no error in config.php file.

Your form's method was missing and the value in your update button was missing

also, you must use ` instead of ' for database table names

try the code below

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

Code seems pretty much ok for me.

Are you certain that $_POST['update'] is set to something?

The lack of error could indicate that it isn't even going through your PHP block.

use form method post

<form method="post" id="insert-data">
</form>

value in insert query put $sampleid,$aba11,$aba12

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM