[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem with Multiple "Insert Record"

Posted on 2006-05-18
3
Medium Priority
?
193 Views
Last Modified: 2010-04-25
Hi All,
Have a page where I want to submit the form contents to two different tables in the DB. I've done this before and had no problems but for some reason in this case, when I enter the second add record server behaviour (to log_table), the page always returns blank in the browser. Here is the code if someone could spot any errors in it. As I said, without the second add record, everything is working fine.
Thanks,
D

<?php require_once('../Connections/GSM_mobile.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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;
}

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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 ($_POST['cab_yes_no'] == "No") {
     $_POST['cab_select'] = "";
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "add_model_frm")) {
  $insertSQL = sprintf("INSERT INTO model_table (mod_model_name, mod_type_id, mod_cable_id) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['txt_name_model'], "text"),
                       GetSQLValueString($_POST['Brand'], "int"),
                       GetSQLValueString($_POST['cab_select'], "int"));

  mysql_select_db($database_GSM_mobile, $GSM_mobile);
  $Result1 = mysql_query($insertSQL, $GSM_mobile) or die(mysql_error());
}

$model_table_last_id = mysql_insert_id();

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "add_model_frm")) {
  $insertSQL = sprintf("INSERT INTO log_table (log_model_id, log_1, log_2, log_5, log_10) VALUES (%s, %s, %s, %s, %s)",
                       GetSQLValueString($model_table_last_id, "int"),
                       GetSQLValueString($_POST['1_log'], "int"),
                       GetSQLValueString($_POST['2_log'], "int"),
                       GetSQLValueString($_POST['5_log'], "int"),
                       GetSQLValueString($_POST['10_log'], "int"));

  mysql_select_db($database_GSM_mobile, $GSM_mobile);
  $Result1 = mysql_query($insertSQL, $GSM_mobile) or die(mysql_error());

  $insertGoTo = "adminhome.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_GSM_mobile, $GSM_mobile);
$query_rsCatMod = "SELECT category_table.cat_category_id, category_table.cat_category_name, type_table.type_type_id, type_table.type_type_name, type_table.type_category_id FROM category_table, type_table WHERE category_table.cat_category_id = type_table.type_category_id";
$rsCatMod = mysql_query($query_rsCatMod, $GSM_mobile) or die(mysql_error());
$row_rsCatMod = mysql_fetch_assoc($rsCatMod);
$totalRows_rsCatMod = mysql_num_rows($rsCatMod);

mysql_select_db($database_GSM_mobile, $GSM_mobile);
$query_rsCables = "SELECT cable_table.cab_cable_id, cable_table.cab_cable_name FROM cable_table";
$rsCables = mysql_query($query_rsCables, $GSM_mobile) or die(mysql_error());
$row_rsCables = mysql_fetch_assoc($rsCables);
$totalRows_rsCables = mysql_num_rows($rsCables);
?><!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 id="add_model_frm" name="add_model_frm" method="POST" action="<?php echo $editFormAction; ?>">
  <p>Welcome to The Add Phone Model Category:</p>
  <p>
    <label>Enter the new model name:
    <input name="txt_name_model" type="text" id="txt_name_model" />
    </label>
</p>
  <p>Enter model prices( For Remote Just Enter in 1 Log):</p>
  <p>
    <label>1 Log:
    <input name="1_log" type="text" id="1_log" />
    </label>
</p>
  <p>
    <label>2 Logs:
    <input name="2_log" type="text" id="2_log" />
    </label>
  </p>
  <p>
    <label>5 Logs:
    <input name="5_log" type="text" id="5_log" />
    </label>
  </p>
  <p>
    <label>10 Logs:
    <input name="10_log" type="text" id="10_log" />
    </label>
  </p>
  <p>Please Select The Category:
    <select name="Category" id="Category" onchange="TCN_reload(this)">
      <option selected="selected">Category</option>
    </select>
  </p>
  <p>Please Select The Brand:
    <select name="Brand" id="Brand" onchange="TCN_reload(this)">
      <option selected="selected">Brand</option>
    </select>
    <script language="JavaScript" type="text/javascript">
TCN_contents=new Array();
TCN_tempArray=new Array();
TCN_counter=0;
function TCN_addContent(str){
     TCN_contents[TCN_counter]=str;
     TCN_counter++;
}
function TCN_split(){
     TCN_arrayValues = new Array();
     for(i=0;i<TCN_contents.length;i++){
          TCN_arrayValues[i]=TCN_contents[i].split(separator);
          TCN_tempArray[0]=TCN_arrayValues;
     }
}
function TCN_makeSelValueGroup(){
     TCN_selValueGroup=new Array();
     var args=TCN_makeSelValueGroup.arguments;
     for(i=0;i<args.length;i++){
          TCN_selValueGroup[i]=args[i];
          TCN_tempArray[i]=new Array();
     }
}
function TCN_makeComboGroup(){
     TCN_comboGroup=new Array();
     var args=TCN_makeComboGroup.arguments;
     for(i=0;i<args.length;i++) TCN_comboGroup[i]=findObj(args[i]);
}
function TCN_setDefault(){
     for (i=TCN_selValueGroup.length-1;i>=0;i--){
          if(TCN_selValueGroup[i]!=""){
               for(j=0;j<TCN_contents.length;j++){
                    if(TCN_arrayValues[j][(i*2)+1]==TCN_selValueGroup[i]){
                         for(k=i;k>=0;k--){
                              if(TCN_selValueGroup[k]=="") TCN_selValueGroup[k]=TCN_arrayValues[j][(k*2)+1];
                         }
                    }
               }
          }
     }
}
function TCN_loadMenu(daIndex){
     var selectionMade=false;
     daArray=TCN_tempArray[daIndex];
     TCN_comboGroup[daIndex].options.length=0;
     for(i=0;i<daArray.length;i++){
          existe=false;
          for(j=0;j<TCN_comboGroup[daIndex].options.length;j++){
               if(daArray[i][(daIndex*2)+1]==TCN_comboGroup[daIndex].options[j].value) existe=true;
          }
          if(existe==false){
               lastValue=TCN_comboGroup[daIndex].options.length;
               TCN_comboGroup[daIndex].options[TCN_comboGroup[daIndex].options.length]=new Option(daArray[i][daIndex*2],daArray[i][(daIndex*2)+1]);
               if(TCN_selValueGroup[daIndex]==TCN_comboGroup[daIndex].options[lastValue].value){
                    TCN_comboGroup[daIndex].options[lastValue].selected=true;
                    selectionMade=true;
               }
          }
     }
     if(selectionMade==false) TCN_comboGroup[daIndex].options[0].selected=true;
}    
function TCN_reload(from){
     if(!from){
          TCN_split();
          TCN_setDefault();
          TCN_loadMenu(0);
          TCN_reload(TCN_comboGroup[0]);
     }else{
          for(j=0; j<TCN_comboGroup.length; j++){
               if(TCN_comboGroup[j]==from) index=j+1;
          }
          if(index<TCN_comboGroup.length){
               TCN_tempArray[index].length=0;
               for(i=0;i<TCN_comboGroup[index-1].options.length;i++){
                    if(TCN_comboGroup[index-1].options[i].selected==true){
                         for(j=0;j<TCN_tempArray[index-1].length;j++){
                              if(TCN_comboGroup[index-1].options[i].value==TCN_tempArray[index-1][j][(index*2)-1]) TCN_tempArray[index][TCN_tempArray[index].length]=TCN_tempArray[index-1][j];
                         }
                    }
               }
          TCN_loadMenu(index);
          TCN_reload(TCN_comboGroup[index]);
          }
     }
}
function findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
TCN_makeSelValueGroup("","");
TCN_makeComboGroup("Category","Brand");
 var separator="+#+";
<?php do{?>
TCN_addContent("<?php echo $row_rsCatMod['cat_category_name']; ?>+#+<?php echo $row_rsCatMod['cat_category_id']; ?>+#+<?php echo $row_rsCatMod['type_type_name']; ?>+#+<?php echo $row_rsCatMod['type_type_id']; ?>");
<?php } while ($row_rsCatMod = mysql_fetch_assoc($rsCatMod)); ?>
TCN_reload();

    </script>
  </p>
 
  <p>
    <label>Does this phone has a cable?
    <select name="cab_yes_no" id="cab_yes_no">
      <option>No</option>
      <option>Yes</option>
    </select>
    </label>
  </p>
  <p>
    <label>Select the Cable:
    <select name="cab_select" id="cab_select">
      <?php
do {  
?>
      <option value="<?php echo $row_rsCables['cab_cable_id']?>"><?php echo $row_rsCables['cab_cable_name']?></option>
      <?php
} while ($row_rsCables = mysql_fetch_assoc($rsCables));
  $rows = mysql_num_rows($rsCables);
  if($rows > 0) {
      mysql_data_seek($rsCables, 0);
       $row_rsCables = mysql_fetch_assoc($rsCables);
  }
?>
    </select>
    </label>
  </p>
  <p>
    <label></label>
    <label>
    <input type="text" name="textfield" />
    </label>
  </p>
  <input type="hidden" name="MM_insert" value="add_model_frm">
  <label>
  <input name="submit_btn" type="submit" id="submit_btn" value="Add Model Now" />
  </label>
</form>
</body>
</html>
<?php
mysql_free_result($rsCatMod);

mysql_free_result($rsCables);
?>
0
Comment
Question by:dereksheahan
  • 2
3 Comments
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 1800 total points
ID: 16713097
Derek,

Try removing one of the GetSQLValueString functions.  It certainly doesn't need to be there twice.  Other than that, code looks okay, so hopefully the error is due to double-defining the function.
0
 

Author Comment

by:dereksheahan
ID: 16717405
Great stuff! That was the problem alright.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 16719736
Sweet...
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those who don't know, Adobe Dreamweaver is a popular commercial web editor that enables you to design, build and manage complex websites. The editor is a WYSIWYG (What You See Is What You Get) web editor, which means that you can create your web…
Adobe Dreamweaver CS5 is a WYSIWYG web page editor that has advanced HTML, CSS, and Javascript rendering functionality and is probably the most well-known HTML editor available. Much of Dreamweaver's appeal centers around the Design View interfac…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question