简体   繁体   English

如何将相同的值插入表sql,php

[英]How to insert same value into to tables sql, php

can someone help me with this, Im using a simple form to add user into a database, my goal is to add the user into one table, but at the same time add the (userid) into a different table, the user id is made automatically by the database, it is possible to do this ? 有人可以帮我吗,我使用一种简单的形式将用户添加到数据库中,我的目标是将用户添加到一个表中,但同时将(userid)添加到另一个表中,从而创建了用户ID由数据库自动执行,有可能做到这一点?

Thank you 谢谢

 <?php 
    require("coneccion.php"); 

    if(!empty($_POST))
    { 

      $query = "INSERT INTO students (fname, lname, studentcode) values (:fname, :lname, :studentcode)";
      $query_params = array(':fname' => $_POST['fname'], ':lname' => $_POST['lname'], ':studentcode' => $_POST['studentcode']);

      try
      {
        $stmt = $db->prepare($query);
        $result = $stmt->execute($query_params);
      }
      catch(PDOException $ex)
      {
        die("Error" . $ex->getMessage());
      }
      $name = $_SESSION['user']['username'];
      $id = $_SESSION['courseid'];
      $query = "INSERT INTO idtables(courseid, studentid) values ('$id', (SELECT id FROM users WHERE username = '$name'))";
      try
      {
        $stmt = $db->prepare($query);
        $stmt->execute();
      }
      catch(PDOExecute $ex)
      {
        die("Error" . $ex->getMessage());
      }
      $id = $_SESSION['courseid'];
      header("Location: index.php?id=$id");
      die("Rendirecting to index.php?id=$id");

    }
    ?>

    <?php
    $id = $_SESSION['courseid'];
    echo $id;
    ?>

    <form action="add.php" method="post">
      First Name: <input type="text" name="fname" value=""><br>
      Last Name: <input type="text" name="lname" value=""><br>
      Stude Code: <input type="text" name="studentcode" value=""><br>
      <input type="submit" value="Add Student">
    </form>

You can get the id of the row you added like this : 您可以像这样获得添加的行的ID:

$studentId = $db->lastInsertId();

See docs 查看文件

It's not safe just to find the student ID by checking names. 仅通过检查姓名来找到学生证并不安全。 Does your table structure allow two students with the same name? 您的表格结构允许两个同名学生吗? Above all you need to make sure that your students table is set with a studentid column that is the primary key and is AUTOINCREMENT. 最重要的是,您需要确保您的学生表设置有一个studentid列,该列是主键,并且是AUTOINCREMENT。 Then you use lastInsertId() and you don't need to do another SELECT in the second (insert) query at all. 然后,您使用lastInsertId(),而根本不需要在第二个(插入)查询中执行另一个SELECT。 Just this: 只是这个:

  $query = "INSERT INTO students (fname, lname, studentcode) values (:fname, :lname, :studentcode)";
  $query_params = array(':fname' => $_POST['fname'], ':lname' => $_POST['lname'], ':studentcode' => $_POST['studentcode']);

  try
  {
    $stmt = $db->prepare($query);
    $result = $stmt->execute($query_params);
    if($result) $newStudentID = $db->lastInsertId();
  }
  catch(PDOException $ex)
  {
    die("Error" . $ex->getMessage());
  }
  $name = $_SESSION['user']['username'];
  $id = $_SESSION['courseid'];
  $query = "INSERT INTO idtables(courseid, studentid) values ('$id', $newStudentID)";

Change the second query to: 将第二个查询更改为:

      $query = "INSERT INTO idtables(courseid, studentid) values ('$id', LAST_INSERT_ID())";

LAST_INSERT_ID is a built-in MySQL function that returns the last inserted ID. LAST_INSERT_ID是一个内置的MySQL函数,它返回最后插入的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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