0 Replies Latest reply on Nov 13, 2008 3:56 AM by patty12345678_8

    insert record without press submit button

    Level 1
      I am using both dreamweaver and php. My problem is how do I insert a record to Mysql table without using a submit button. I want to write something to a hidden fields and the hidden fields will insert to a record.

      The reason I want to do this is I have 2 programs. Program 1 will call program 2 and program 2 will insert a record to a table and then return back to program 1.

      Hope someone can help.
      Thanks in advance.
      Patty

      <?php require_once('Connections/abb.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;
      }

      $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 test2 (r1, r2) VALUES (%s, %s)",
      GetSQLValueString($_POST['h1'], "text"),
      GetSQLValueString($_POST['h2'], "text"));

      mysql_select_db($database_abb, $abb);
      $Result1 = mysql_query($insertSQL, $abb) or die(mysql_error());

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

      mysql_select_db($database_abb, $abb);
      $query_test2 = "SELECT * FROM test2";
      $test2 = mysql_query($query_test2, $abb) or die(mysql_error());
      $row_test2 = mysql_fetch_assoc($test2);
      $totalRows_test2 = mysql_num_rows($test2);
      ?><!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=" />
      <title>Untitled Document</title>
      </head>

      <body>
      <form name="form1" id="form1" action="" method="post">
      <input name="h1" type="hidden" value="3331" />
      <input name="h2" type="hidden" value="test3" />
      <input type="submit" name="Submit" value="Submit" />
      </form>
      </body>
      </html>
      <?php
      mysql_free_result($test2);
      ?>