0

I am writing a Php (or rather using dreamweaver to easily so it) to insert multiple data into a database. For example if I have 10 values, instead of submitting each one individually, is it just possible to copy and paste the 10 values separated by a space so that they go into different rows. For learning purposes, I have done a simple page, connected to a Phpmyadmin database named cmsd1. Code as of now is

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

$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 cmsd1 (Population_Name) VALUES (%s)",
                       GetSQLValueString($_POST['Form1'], "text"));

  mysql_select_db($database_cmsdtest, $cmsdtest);
  $Result1 = mysql_query($insertSQL, $cmsdtest) or die(mysql_error());

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

mysql_select_db($database_cmsdtest, $cmsdtest);
$query_Recordset1 = "SELECT Population_Name FROM cmsd1";
$Recordset1 = mysql_query($query_Recordset1, $cmsdtest) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!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>Untitled Document</title>
</head>

<body>
<p>&nbsp;</p>
<p>&nbsp;</p>
<form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
Population
  <label for="Form1"></label>
  <input type="text" name="Form1" id="Form1" />
  <input type="submit" name="Submit" id="Submit" value="Submit" />
  <input type="hidden" name="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

It goes to a simple webpage to just deposit data.

Simple website to insert values

Please don't go into detailed code since I am not very good at PhP coding.. Thanks for any help offered..

1 Answer 1

1

To insert multiple rows you do the following:

INSERT INTO tbl_name (a, b, c) VALUES (1, 2, 3), (4, 5, 6);

This will insert two rows where row one has 1, 2, and 3 for columns a, b, and c and row two has 4, 5, and 6.

See the documentation on INSERT statements.

To have this work with posted values where you have a single field with space-separated values you would do something like the following:

$values = explode(' ', $_POST['values']);

foreach ($values as &$value)
{
  $value = mysql_real_escape_string($value);
}

mysql_query("INSERT INTO tbl_name (a) VALUES ('" . implode("'), ('", $values) . "')");

Although I would advise that you look into using prepared statements and get away from the mysql extension which is deprecated as of PHP 5.5.0. Try mysqli or PDO.

Sign up to request clarification or add additional context in comments.

1 Comment

I am sorry but I do know that it is possible in SQL. I am trying to submit multiple data through a textfield on a webpage here. So not what I am looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.