php - How to grab an auto incremented variable and insert it into an insert query -
i trying couple of php insert queries relational database, running bit of issue. in order relation work need grab autoincremented value first query , insert second query relation between 2 exists.
i have this:
$query2 = "insert words
values ('' ,'$name') ";
-- first value listed '' auto-incremented primary key --
$query3 = "insert synonyms
values ('' , '', $alias') ";
-- first value listed auto incremented pk, second value needs fk or pk first query, don't know how place there. --
is there way this? appreciated.
here sql fiddle y'all out:
<?php $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('could not connect: ' . mysql_error()); } mysql_select_db('mydb'); mysql_query("insert words(word) values ('word1')"); $last_id = mysql_insert_id(); mysql_query("insert words(synonym_id,synonym) values ($last_id, "synonym1)"); ?>
reference: http://php.net/manual/en/function.mysql-insert-id.php
Comments
Post a Comment