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:

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

<?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

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -