Jump to content

Adding multiple records to a MySQL table


h2oskierc

Recommended Posts

I have a form that is used to insert a record into a database, but would like to be able to enter multuple records into the same table with the one form.

 

There is a session variable that I have assigned to $numProofs that will store the number of records to insert.  I would like the number of records inserted to change depending on the value of this variable, ex. if the variable has a value of 6, the form would have space for 6 records, but if the value is 1, it would only have space for 1 record.

 

The code works, as it will insert one record, so I just need t determine how to make it repeat the proper umber of times, and make individual records.

 

I am using a combination of Dreamweaver CS3 and hand coding (sorry, there is 197 lines of code, the form starts at line 162):

 

<?php
session_start();
//define a constant for maximum upload size
define ('MAX_FILE_SIZE', 512000);
//store session variables to regular variables for ease of use
$userID = '1'; //$_SESSION['userID'];
$invoice = '12345'; //$_SESSION['invoice'];
$numProofs = '5'; //$_SESSION['numProofs'];

if (array_key_exists('create', $_POST)) {
//define constant for upload folder
define('UPLOAD_DIR', '/dellsigns/proofs/images/large/');
//replace any spaces in the original filename with underscores
//at the same time, assign to a simpler variable
$file = str_replace(' ', '_', $_FILES['proof']['name']);
//get the date and time
ini_set('date.timezone', 'America/Chicago');
$now = date('m-d-Y-His');
//set fileName variable for ease of coding
$fileName = $now.$file;
// convert maximum size to KB
$max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
//create an array of permitted MIME types
$permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
///begin by assuming the file is unnacceptable
$sizeOK = false;
$typeOK = false;
//check that the file is within the permitted size
if ($_FILES['proof']['size'] > 0 && $_FILES['proof']['size'] <= MAX_FILE_SIZE) {
	$sizeOK = true;
	}
//check that the file is of a permitted type
foreach ($permitted as $type) {
	if ($type == $_FILES['proof']['type']) {
		$typeOK = true;
		break;
		}
	}
if ($sizeOK && $typeOK) {
switch($_FILES['proof']['error']){
case 0:		
//move the file to the upload folder and rename it
$success = move_uploaded_file($_FILES['proof']['tmp_name'],UPLOAD_DIR.$fileName);
if ($success) {
	$result = "$file uploaded successfully";
	}
else {
	$result = "Error uploading $file.  Please try again.";
	}
break;
case 3:
	$result = "Error uploading $file.  Please try again.";
default:
	$result = "System error uploading $file.  Contact webmaster.";
	}
}
elseif ($_FILES['proof']['error'] == 4) {
	$result = 'No file selected.';
	}
else {
	$result = "$file cannot be uploaded.  Maximum size: $max.  Acceptable file types: gif, jpeg, png.";
	}
}
?>

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

$user_getProofDetails = "-1";
if (isset($userID)) {
  $user_getProofDetails = $userID;
}
$inv_getProofDetails = "-1";
if (isset($invoice)) {
  $inv_getProofDetails = $invoice;
}
$numbProofs_getProofDetails = "-1";
if (isset($numProofs)) {
  $numbProofs_getProofDetails = $numProofs;
}
mysql_select_db($database_proofs, $proofs);
$query_getProofDetails = sprintf("SELECT proofID FROM proofs WHERE proofs.userID = %s AND proofs.invoice = %s  AND proofs.numProofs = %s", GetSQLValueString($user_getProofDetails, "int"),GetSQLValueString($inv_getProofDetails, "int"),GetSQLValueString($numbProofs_getProofDetails, "int"));
$getProofDetails = mysql_query($query_getProofDetails, $proofs) or die(mysql_error());
$row_getProofDetails = mysql_fetch_assoc($getProofDetails);
$totalRows_getProofDetails = mysql_num_rows($getProofDetails);
?>
<?php
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
?>
<?php
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "proofDetails")) {
  $insertSQL = sprintf("INSERT INTO signs (proofID, image, `size`, material, cost) VALUES (%s, '$fileName', %s, %s, %s)",
                       GetSQLValueString($_POST['proofID'], "int"),
                       GetSQLValueString($_POST['size'], "text"),
                       GetSQLValueString($_POST['material'], "text"),
                       GetSQLValueString($_POST['cost'], "text"));

  mysql_select_db($database_proofs, $proofs);
  $Result1 = mysql_query($insertSQL, $proofs) 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=utf-8" />
<title>Dell Signs & Graphics Template</title>
<link href="../assets/styles/oneColFixCtrHdr.css" rel="stylesheet" type="text/css" />
<script src="../assets/spry/SpryMenuBar.js" type="text/javascript"></script>
<link href="../assets/spry/SpryMenuBarHorizontal_nav.css" rel="stylesheet" type="text/css" />
</head>

<body class="oneColFixCtrHdr">

<div id="container">
  <div id="header">
    <h1>Dell Signs & Graphics</h1>
    <img src="../assets/images/header.jpg" alt="Dell Signs & Graphics" width="780" height="179" />
    <!-- end #header -->
  </div>
  <?php include('../assets/includes/nav.inc.php'); ?>
  <div id="mainContent">
    <h1> Add Signs to Proof</h1>
    <?php
//if the form has been submitted, display result
if (isset($result)) {
	echo "<p class=warning>$result</p>";
	}
	?>
    <form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="proofDetails" id="proofDetails">
      <p>
        <input type="hidden" name="proofID" id="proofID" value="<?php echo $row_getProofDetails['proofID']; ?>"/>
        <label for="proof">Proof Image:</label>
        <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
        <input type="file" name="proof" id="proof" />
      </p>
      <p>
        <label for="size">Sign Size:</label>
        <input type="text" name="size" id="size" />
      </p>
      <p>
        <label for="material">Sign Materials:</label>
        <input type="text" name="material" id="material" />
      </p>
      <p>
        <label for="cost">Sign Cost:</label>
        <input type="text" name="cost" id="cost" />
      </p>
      <p>
        <label for="create"></label>
        <input type="submit" name="create" id="create" value="Create Sign(s)" />
</p>
      <input type="hidden" name="MM_insert" value="proofDetails" />
    </form>
    <p><!-- end #mainContent -->
    </p>
  </div>
  <?php include('../assets/includes/footer.inc.php'); ?>
<!-- end #container --></div>

</body>
</html>
<?php
mysql_free_result($getProofDetails);
?>

The user will not determine how many different records are needed, a table in the proofs database will (for instance, if there are 6 proofs needed).  This value is obtained through a session variable that is stored on the previous page, and is assigned to $numProofs.  Right now I have commented out the code for getting the value from the session, and hard coded in a value of 5.

 

The data entered for each entry will be different, so I need to make the form build istself dynamically, and each record into the MySQL table.

 

The other way I have thought about doing this is by having the form only have the space for one record, and then having a counter that would keep track of the number of times the form has been used to insert the records, and redirect to the next page once the counter hits the right number.  Perhaps that would be a better (or easier) way of doing this?

 

 

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.