1

我将首先向您展示代码,这样更容易讨论。

 $message = $_GET['r'];
    $message = str_replace("%20", " ", $message);
    echo $message;

    mysql_query("INSERT INTO `uniform` (`note`,`cadetID) 
    VALUES ('$message','$cadet')");

    $query = mysql_query("SELECT `uniformID` FROM `cadets` WHERE id = '$cadet'");
    while ($row = mysql_fetch_array($query)) {
      $uniformA = $row['uniformID'];
    }

    if($uniformA == "0"){
      mysql_query("UPDATE `cadets` SET `uniformID` = '" .  ."'
      WHERE id = '$cadet'");
    }

暂时不用担心消毒。

我到了这条线:

mysql_query("UPDATE `cadets` SET `uniformID` = '" .  ."' WHERE id = '$cadet'");

并意识到我需要 sql 插入的 ID

mysql_query("INSERT INTO `uniform` (`note`,`cadetID) VALUES ('$message','$cadet')");

有没有一种从数据库中获取新增加的 ID 的有效方法?表结构如下:

cadet(id[primary key, auto increment],...,uniformID)

uniform(id[prim key, auto inc], note, cadetID)

数据:

Cadet: 1,...,[1,2,3]<- [] 中的所有内容都在 cadetID 中,数组由 ',' 分隔

Uniform: 1, need a new blahh blahh, 1

Uniform: 2, new shoes needed, 1

等等。

所以,在急诊室里,一个学员可以有很多统一的身份证。

TL;DR我需要将新 ID 存储在数据库中(由 SQL db 自动递增)insert into

4

3 回答 3

2

我建议您将其更改为 PDO 并像这样简单地访问它:

$query = "INSERT INTO `uniform` ( `note`, `cadetID` ) VALUES ( ?, ? )";
$params = array( $message, $cadet );

$stmt = $db->prepare( $query );
$stmt->execute( $params );

$last_id = $db->lastInsertId();

这是很好的教程以及如何做到这一点:http ://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

但是在您的情况下,您可以使用这样的查询(尽管它不安全):

SELECT LAST_INSERT_ID();
于 2012-11-08T11:45:16.347 回答
2

MySQL 存储每个会话/连接的最后一个 auto_increment 值。您可以在客户端(即您的 php 脚本)端获取此值,也可以通过 LAST_INSERT_ID() 函数在服务器端(即 MySQL)简单地使用它

自包含示例(使用 PDO - 无论如何你都想研究它;-))

<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
setup($pdo);

$stmtUniform = $pdo->prepare('INSERT INTO so_uniforms (labelid) VALUES (?)');
$stmtAssignToCadet = $pdo->prepare('
  UPDATE
    so_cadets
  SET
    uniform_id=LAST_INSERT_ID()
  WHERE
    id=?
');

// add uniform and assign to cadet #1
$stmtUniform->execute( array('uniform1') );
$stmtAssignToCadet->execute( array(1) );

// add uniform and assign to cadet #3
$stmtUniform->execute( array('uniform2') );
$stmtAssignToCadet->execute( array(3) );

// add uniform and assign to cadet #4
$stmtUniform->execute( array('uniform3') );
$stmtAssignToCadet->execute( array(4) );

// show cadets<->uniforms
$query = '
    SELECT
        c.id, u.labelid
    FROM
        so_cadets as c
    LEFT JOIN
        so_uniforms as u
    ON
        c.uniform_id = u.id
';

foreach( $pdo->query($query, PDO::FETCH_ASSOC) as $row ) {
    echo join(', ', $row), "\n";
}

// boilerplate
function setup($pdo) {
    $pdo->exec('
        CREATE TEMPORARY TABLE so_cadets (
            id int auto_increment,
            name varchar(32),
            uniform_id int default 0,
            primary key(id)
        )
    ');

    $pdo->exec('
        CREATE TEMPORARY TABLE so_uniforms (
            id int auto_increment,
            labelid varchar(32),
            primary key(id),
            unique key(labelid)
        )
    ');

    $stmt = $pdo->prepare('INSERT INTO so_cadets (name) VALUES (?)');
    foreach( range('A','F') as $c ) {
        $stmt->execute( array('cadet'.$c));
    }
}

印刷

1, uniform1
2, 
3, uniform2
4, uniform3
5, 
6, 
于 2012-11-08T11:46:00.677 回答
1

您还可以使用mysqli函数

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "INSERT INTO `uniform` (`note`,`cadetID) VALUES ('$message','$cadet')";
$mysqli->query($query);

echo $mysqli->insert_id; //echoes the newly incremented id
于 2012-11-08T11:50:16.107 回答