问题描述
我正在编写一个Php(或者更方便地使用Dreamweaver来实现)以将多个数据插入数据库中.例如,如果我有10个值,而不是分别提交每个值,则可以复制并粘贴用空格分隔的10个值,以便它们进入不同的行.出于学习目的,我做了一个简单的页面,连接到名为cmsd1的Phpmyadmin数据库.到目前为止的代码是
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> </p>
<p> </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> </p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>
它会转到一个简单的页面,仅用于存储数据.
It goes to a simple webpage to just deposit data.
请不要使用详细的代码,因为我不太擅长PhP编码..感谢您提供的任何帮助.
Please don't go into detailed code since I am not very good at PhP coding.. Thanks for any help offered..
推荐答案
要插入多行,请执行以下操作:
To insert multiple rows you do the following:
INSERT INTO tbl_name (a, b, c) VALUES (1, 2, 3), (4, 5, 6);
这将插入两行,其中第一行的 a
列为 1
, 2
和 3
, b
, c
和第二行具有 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
.
请参见 INSERT
上的文档a>声明.
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) . "')");
尽管我建议您使用准备的语句,但请避免使用自 PHP 5.5.0
起已弃用的 mysql
扩展名.尝试 mysqli
或 PDO
.
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
.
这篇关于网页将多个数据插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!