0

I am trying to do a couple of php insert queries into a relational database, but I am running into a bit of an issue. In order for this relation to work I need to grab the autoincremented value from the first query and then insert it into the second query so the relation between the two exists.

I have this:

$query2 = "INSERT into words values ('' ,'$name') ";

-- The first value listed as '' is the auto-incremented primary key --

$query3 = "INSERT into synonyms values ('' , '', $alias') ";

-- The first value listed is the auto incremented pk, the second value needs to be the fk or the pk from the first query, but I don't know how to place it there. --

Is there a way to do this? Any help would be appreciated.

Here an SQL Fiddle to help y'all out:

http://sqlfiddle.com/#!2/47d42

4

3 回答 3

1
<?php
    $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db('mydb');

    mysql_query("INSERT INTO words(word) values ('word1')");

    $last_id = mysql_insert_id();

    mysql_query("INSERT INTO words(synonym_id,synonym) values ($last_id, "synonym1)");

?>

Reference: http://php.net/manual/en/function.mysql-insert-id.php

于 2013-04-20T04:10:40.023 回答
1

. . You should consider using PDO in most recent PHP versions for its modern features, such as prepared statements, so that you don't need to worry about SQL Injection or broken escaping functions.

. . Using transactions is also advisable if the follow up queries are mandatory for the record to be useful. Using transactions keeps your database clear of the garbage of any failed second or third queries.

. . Also, you can omit the Auto-Increment field when running the Insert Query if you list the other fields after the table name. I think it's a much more common pattern, like INSERT INTO table (field1, field2) VALUES ("value1", "value2"). I used it in the example below:

$pdo = new PDO('mysql:host=localhost;dbname=database', 'user', 'pass');

$pdo->beginTransaction();
try {
  $prepared = $dbh->prepare('INSERT INTO words (fieldName) values (?)');
  $prepared->execute(array($name));
  $fID = $pdo->lastInsertId();

  $prepared = $dbo->prepare('INSERT INTO synonyms (fieldName) Values (?, ?)';
  $prepared->execute(array($fID, $alias));

  $dbo->commit(); 
} catch(PDOExecption $e) {
  $dbo->rollback();
  print 'Error: '. $e->getMessage(); 
}

. . Note that this will not work with MSSQL as it doesn't support "lastInsertId".

. . Amplexos.

于 2013-04-20T04:24:27.200 回答
0

not sure if you're using MySQL native functions or not. If so the answer is to use mysql_last_id(). These functions are deprecated and are not adivsable to use.

EXAMPLE:

//escape your indata
$brand= mysql_real_escape_string($_POST['brand']);
$sql = "INSERT INTO cars(brand) VALUES('{$brand}')";
mysql_query($sql);

//find last id from query above
$id = mysql_last_id();

Try PDO instead: PDO::lastInsertId

EXAMPLE:

$brand= $_POST['brand'];
$sql = "INSERT INTO cars(brand) VALUES (:brand)";
$query = $conn->prepare($sql);
$query ->execute(array(':brand'=>$brand));
$id = $conn->lastInsertId();

http://www.php.net/manual/en/book.pdo.php

于 2013-04-20T04:12:41.203 回答