Jump to content


Photo

Inserting same id into two tables


  • Please log in to reply
2 replies to this topic

#1 dansta

dansta
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 15 July 2006 - 01:47 PM

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

#2 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 15 July 2006 - 04:11 PM

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

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#3 dansta

dansta
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 15 July 2006 - 07:56 PM

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

Title
-id - INT, auto_increment
-title

and

Text
-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...nsitional.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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users