php - 插入表并提取最后一个 ID

标签 php mysql

各位天才下午好,希望对您有所帮助。 在我向其中添加上传图像之前,这是插入到表 Twitch & Birds 中的,现在唯一有效的部分是图像上传。这是非常简单的代码,但无法弄清楚为什么它没有添加。

此外,一旦 TWITCH 表工作,我如何才能从 TWITCH 表中获取自动增量 ID。 提前致谢。

<?php
session_start();
include ('dbconnect.php');

//Set Variables from Form
$species =($_POST['Species']);
$age =($_POST['Age']);
$sex =($_POST['Sex']);
$location =($_POST['Location']);
$date_seen = date("Y-m-d");
$time_seen =($_POST['Time']);
$twitch =($_POST['Comments']);
//$twitch_id=(". mysql_insert_id()");

 //Insert Into Birds table
 $query1 = "INSERT INTO Birds (Species, Age, Sex, Location, Date_Seen, Time_Seen,     Comments) VALUES
  ( '$species', '$age', '$sex', '$location', '$date_seen', '$time_seen', '$twitch')";
 result1 = mysqli_query($conn, $query1);

 //Insert into Twitch table
  query2 = "INSERT INTO Twitch (Twitch_ID, Twitch) VALUES( 'NULL', '$twitch')";
 $result2 = mysqli_query($conn, $query2);

 // Validate and upload image file
 if ( !preg_match( '/gif|png|x-png|jpeg/', $_FILES['userFile']['type']) ) {
  die('<p>Only browser compatible images allowed</p></body></html>');
 } else if ( strlen($_POST['altText']) < 9 ) {
  die('<p>Please provide meaningful alternate text</p></body></html>');
 } else if ( $_FILES['userFile']['size'] > 1116384 ) {
 die('<p>Sorry file too large</p></body></html>');

 // Connect to database
 } else if ( !($conn=mysqli_connect($host, $username, $password)) ) {
  die('<p>Error connecting to database</p></body></html>');
 } else if ( !(mysqli_select_db($conn, $db_name)) ) {
  die('<p>Error selecting database</p></body></html>');

 // Copy image file into a variable
 } else if ( !($handle = fopen ($_FILES['userFile']['tmp_name'], "r")) ) {
   die('<p>Error opening temp file</p></body></html>');
 } else if ( !($image = fread ($handle, filesize($_FILES['userFile']['tmp_name']))) ) {
  die('<p>Error reading temp file</p></body></html>');
} else {
  fclose ($handle);

 // Commit image to the database
   $image = mysqli_real_escape_string($conn, $image);
   $alt = htmlentities($_POST['altText']);
   $query3 = 'INSERT INTO Image (Image_Name, Image_Type, Description, Image) VALUES ("' . $_FILES['userFile']['type'] . '","' . $_FILES['userFile']['name']  . '","' . $alt  .    '","' . $image . '")';
  if ( !(mysqli_query($conn, $query3)) ) {
     die('<p>Error writing image to database</p></body></html>');
    } else {
    header ("Location: TwitchWall.php");
  }
}
?>

最佳答案

使用mysqli_insert_id .

The mysqli_insert_id() function returns the ID generated by a query on a table with a column having the AUTO_INCREMENT attribute. If the last query wasn't an INSERT or UPDATE statement or if the modified table does not have a column with the AUTO_INCREMENT attribute, this function will return zero.

面向对象风格

$query = "INSERT INTO ...";
$con->query($query);
echo $con->insert_id;

程序风格

$query = "INSERT INTO ...";
mysqli_query($con, $query);
echo mysqli_insert_id($con);

关于php - 插入表并提取最后一个 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19953893/

相关文章:

php - 如何只显示特定页数而不是所有页数?

php - HTTPS PHP 大型 XML 负载无法通过回显到达客户端

php - 将 WooCommerce 订单总量保存为 Shipstation 的自定义字段

php - mysql 和 php 函数未正确显示 % 百分号

带游标参数的 MySQL 过程

php - 如何使用/不使用循环php随机获取和显示结果

php - 2 表MYSQL PHP查询并应用PHP填充HTML网格显示

mysql - 如何使用 REPLACE() 替换 mysql SELECT 查询中 1 列中的多个值?

php - 表值未被回显