Skip to main content
Known Participant
January 10, 2011
Question

PHP MySQL SELECT INSERT

  • January 10, 2011
  • 1 reply
  • 1907 views

I have a registration page with a html form. I have created an insert record server behavior that insert all the data into a MySQL database table fine. What I like to do additionally is to insert the data from a single row, actually the last row, into another table in the same mysql database. I have heard of MySQL insert-select but I can't seem to make it work on my own.  Is insert-select possible with Dreamweaver? If not, what should I use?

I appreciate any help

This topic has been closed for replies.

1 reply

January 10, 2011

Inserting all data into another table is redundant. You only need to pass the primary key to a new table when necessary and use the JOIN argument in your query to JOIN data from seperate tables for one user.

irajiAuthor
Known Participant
January 10, 2011

Sorry if I was misleading. I like to pass the data from 3 columns of the last row of the table to another table.

irajiAuthor
Known Participant
January 10, 2011

Show us the select insert statement that you tried and is not working. As TS stated, inserting redundant data is usually not necessary, but there are definitely exceptions.


Here's the entire php code but the last part in bold is not working:

<?php require_once('Connections/myConn.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

  if (PHP_VERSION < 6) {

    $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;

}

}

// *** Redirect if username exists

$MM_flag="MM_insert";

if (isset($_POST[$MM_flag])) {

  $MM_dupKeyRedirect="already-exits.html";

  $loginUsername = $_POST['userName'];

  $LoginRS__query = sprintf("SELECT userName FROM new_members WHERE userName=%s", GetSQLValueString($loginUsername, "text"));

  mysql_select_db($database_myConn, $myConn);

  $LoginRS=mysql_query($LoginRS__query, $myConn) or die(mysql_error());

  $loginFoundUser = mysql_num_rows($LoginRS);

  //if there is a row in the database, the username was found - can not add the requested username

  if($loginFoundUser){

    $MM_qsChar = "?";

    //append the username to the redirect page

    if (substr_count($MM_dupKeyRedirect,"?") >=1) $MM_qsChar = "&";

    $MM_dupKeyRedirect = $MM_dupKeyRedirect . $MM_qsChar ."requsername=".$loginUsername;

    header ("Location: $MM_dupKeyRedirect");

    exit;

  }

}

$editFormAction = $_SERVER['PHP_SELF'];

if (isset($_SERVER['QUERY_STRING'])) {

  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);

}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "register")) {

  $insertSQL = sprintf("INSERT INTO new_members (userName, password, confirmPassword, email, confirmEmail, datingSite, notifyMe, agreeToTerms) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",

                       GetSQLValueString($_POST['userName'], "text"),

                       GetSQLValueString($_POST['password'], "text"),

                       GetSQLValueString($_POST['confirmPassword'], "text"),

                       GetSQLValueString($_POST['email'], "text"),

                       GetSQLValueString($_POST['confirmEmail'], "text"),

                       GetSQLValueString($_POST['datingSite'], "text"),

                       GetSQLValueString(isset($_POST['notifyMe']) ? "true" : "", "defined","'Y'","'N'"),

                       GetSQLValueString(isset($_POST['agreeToTerms']) ? "true" : "", "defined","'Y'","'N'"));

  mysql_select_db($database_myConn, $myConn);

  $Result1 = mysql_query($insertSQL, $myConn) or die(mysql_error());

  $insertGoTo = "selectplan.php";

  if (isset($_SERVER['QUERY_STRING'])) {

    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";

    $insertGoTo .= $_SERVER['QUERY_STRING'];

  }

  header(sprintf("Location: %s", $insertGoTo));

}

mysql_select_db($database_myConn, $myConn);

$query_rsNotify = "INSERT INTO be_notified (new_members.email, new_members.datingSite, new_members.userName) SELECT new_members.userName, new_members.email, new_members.datingSite FROM new_members WHERE new_members.notifyMe = 'y' ORDER BY newMemberID DESC LIMIT 1 ";

$rsNotify = mysql_query($query_rsNotify, $myConn) or die(mysql_error());

$row_rsNotify = mysql_fetch_assoc($rsNotify);

$totalRows_rsNotify = mysql_num_rows($rsNotify);

?>