网页将多个数据插入数据库

时间:2014-01-27 09:20:19

标签: php mysql wamp dreamweaver

我正在编写一个Php(或者更确切地说,使用Dreamweaver),以便将多个数据插入到数据库中。例如,如果我有10个值,而不是单独提交每个值,则可以复制并粘贴由空格分隔的10个值,以便它们进入不同的行。出于学习目的,我做了一个简单的页面,连接到名为cmsd1的Phpmyadmin数据库。截至目前的代码是

<?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);
?>

它只是一个简单的网页来存放数据。

Simple website to insert values

请不要进入详细的代码,因为我不擅长PhP编码..感谢您提供任何帮助..

1 个答案:

答案 0 :(得分:1)

要插入多行,请执行以下操作:

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

这将插入两行,其中第一行包含123,用于列abc第二行有456

请参阅INSERT声明的文档。

要使用包含空格分隔值的单个字段的已发布值,您可以执行以下操作:

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

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

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

虽然我建议您考虑使用prepared statements并远离mysql已弃用的PHP 5.5.0扩展程序。试试mysqliPDO

相关问题