【发布时间】:2014-01-27 09:20:18
【问题描述】:
我正在编写一个 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> </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);
?>
它转到一个简单的网页来存储数据。
由于我不擅长 PhP 编码,请不要详细介绍代码。感谢您提供的任何帮助。
【问题讨论】:
标签: php mysql wamp dreamweaver