Jump to content

Inserting same id into two tables


Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/14673-inserting-same-id-into-two-tables/
Share on other sites

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();
}
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
-title

and

[b]Text[/b]
-id - INT
-text

I'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'); ?>
<?php
if (!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">&nbsp;</td>
      <td><input type="submit" value="Insert record"></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</form>
<p>&nbsp;</p>
</body>
</html>

Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.