dansta Posted July 15, 2006 Share Posted July 15, 2006 Hi, hope someone can help.I have an insert form that I can get information into my database no problem but what I want to do is insert the same id into a seperate table at the same time. I understand that I need to use **mysql_insert_id();** but due to my lack of PHP knowledge I don't know how to use it.Can anyone help with this please.Thanks Quote Link to comment https://forums.phpfreaks.com/topic/14673-inserting-same-id-into-two-tables/ Share on other sites More sharing options...
pixy Posted July 15, 2006 Share Posted July 15, 2006 Whatever your primary key is that you have set on auto_increment is the id you would enter with the mysql_insert_id(); function.Try this format, but fill in your values:$query = "INSERT INTO table1 (value1, value2) VALUES ('$variable1, '$variable2)";$result = mysql_query($query);if ($result) { $id = mysql_insert_id(); $new_query = "INSERT INTO table2 (id_colum) VALUES ('$id')"; $new_result = mysql_query($new_query); if ($new_result) { echo 'Everything is sucessfull!'; } else { echo mysql_error();}else { echo myslq_error();} Quote Link to comment https://forums.phpfreaks.com/topic/14673-inserting-same-id-into-two-tables/#findComment-58551 Share on other sites More sharing options...
dansta Posted July 15, 2006 Author Share Posted July 15, 2006 Thanks for the reply.I've tries to put that into my page and amend the details but still can't get it to work. My tables are[b]Title[/b]-id - INT, auto_increment-titleand [b]Text[/b]-id - INT-textI'm using dreamweaver and this is the code it's produced to insert the id and title, I just need to know where to put your code to carry the id across to the Text table.<?php require_once('Connections/dbconnect.php'); ?><?phpif (!function_exists("GetSQLValueString")) {function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue;}}$editFormAction = $_SERVER['PHP_SELF'];if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);}if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { $insertSQL = sprintf("INSERT INTO title (title) VALUES (%s)", GetSQLValueString($_POST['title'], "text")); mysql_select_db($database_dbconnect, $dbconnect); $Result1 = mysql_query($insertSQL, $dbconnect) or die(mysql_error());}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title></head><body><form method="post" name="form1" action="<?php echo $editFormAction; ?>"> <table align="center"> <tr valign="baseline"> <td nowrap align="right">Title:</td> <td><input type="text" name="title" value="" size="32"></td> </tr> <tr valign="baseline"> <td nowrap align="right"> </td> <td><input type="submit" value="Insert record"></td> </tr> </table> <input type="hidden" name="MM_insert" value="form1"></form><p> </p></body></html>Thanks Quote Link to comment https://forums.phpfreaks.com/topic/14673-inserting-same-id-into-two-tables/#findComment-58630 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.