将值插入数据库,其中一个表单值是一个数组
Insert value into a db where one form value is an array
我有一个表单,里面有很多字段需要填写,如下:
形式
<form action="<?php echo $editFormAction; ?>" id="form1" name="form1" method="POST">
<input type="checkbox" name="Category[]" value="Casio Digital Pianos" id="Category_0" />
Casio piano</td>
<td><input type="checkbox" name="Category[]" value="Casio Keyboards" id="Category_2" />
Casio Keyboard</td>
<td><input type="checkbox" name="Category[]" value="Recording" id="Category_3" />
Recording</td>
<td><input type="checkbox" name="Category[]" value="Modules & Add-on s" id="Category_4" />
Modules & Add-on's</td>
</tr>
<tr>
<td> <input type="checkbox" name="Category[]" value="Kawai Digital Pianos" id="Category_5" />
</label>
</p>
<p>
<label>Manufacturer
<select name="Manufacturer" id="Manufacturer">
<option value="Casio">Casio</option>
<option value="Kawai">Kawai</option>
<option value="Korg">Korg</option>
<option value="Roland">Roland</option>
<option value="Yamaha">Yamaha</option>
<option value="Ketron">Ketron</option>
<option value="Boss">Boss</option>
<option value="Samson">Samson</option>
<option value="Orla">Orla</option>
<option value="Technics">Technics</option>
<option value="Ultimax">Ultimax</option>
</select>
</label>
</p>
<p>
<label>Model
<input type="text" name="Model" id="Model" />
</label>
</p>
<p>
<label>Color
<input type="text" name="Color" id="Color" />
</label>
</p>
<p>
<label>
<p><input name="submit" type="submit" value="submit" /></p>
<input type="hidden" name="MM_insert" value="form1" />
</form>
我想为类别的每个值在数据库中创建一个新行
我正在使用 DW 开始,如下所示:
插入代码:
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 products (Category, Manufacturer, Model, Color) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST['Category'], "text"),
GetSQLValueString($_POST['Manufacturer'], "text"),
GetSQLValueString($_POST['Model'], "text"),
GetSQLValueString($_POST['Color'], "text");
mysql_select_db($database_dconn, $dconn);
$Result1 = mysql_query($insertSQL, $dconn) or die(mysql_error());
}
这不起作用,因为它以某种方式找不到类别我认为这是因为它是一个数组。有没有一种方法可以使它工作,以便在勾选到新字段时确实为每个类别插入表单。
我尝试了以下方法:
if (isset($_POST['Category'])) {
foreach($_POST['Category'] as $value);
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
etc...
但这似乎根本不起作用。
欢迎任何帮助
首先,read up on PDO并停止使用mysql()
功能。接下来,您与 foreach 接近,但停止使用 isset - 即使字段为空,这对于发布的表单也总是 return true(因为变量由于发布而初始化,即使它为 NULL - 但是不要不要尝试检查 NULL,因为它仍然会在值“”时作为 true 传递,而是使用 if(!empty($_POST['field']))
。关于针对类别数组的 foreach,请尝试以下操作。
现在,我注意到了一些问题。您是否希望 运行 多个 SQL 插入取决于检查类别的数量?这些颜色、制造商等值是否相同?如果是这样,你就走在正确的轨道上。如果没有,我建议您重做整个表格(例如,如果您需要为每个选中的类别提供唯一值)。接下来,为什么要传递带有表单名称值的隐藏字段?这是为了验证数组是否已发布?
最后但同样重要的是,get_magic_quotes_gpc()
- 不,就是不。首先,使用 PDO 不需要在进入 SQL 查询的过程中对字符串进行清理(虽然理解危险的荣誉,如果不是赢得执行点的话)。此外,在显示上,您真正需要的只是 htmlspecialchars()
来清理任何显示的输出。这将达到目的并消除对您的功能的需求。
编辑:试试下面的(没有任何 HTML 格式)
你能不能简单地试试:
if(!empty($_POST['submit'])) {
foreach ($_POST['category'] AS $val) {
# $val will hold the value of your checkbox
# Run SQL insert
}
} else {
# Display HTML form
}
我有一个表单,里面有很多字段需要填写,如下: 形式
<form action="<?php echo $editFormAction; ?>" id="form1" name="form1" method="POST">
<input type="checkbox" name="Category[]" value="Casio Digital Pianos" id="Category_0" />
Casio piano</td>
<td><input type="checkbox" name="Category[]" value="Casio Keyboards" id="Category_2" />
Casio Keyboard</td>
<td><input type="checkbox" name="Category[]" value="Recording" id="Category_3" />
Recording</td>
<td><input type="checkbox" name="Category[]" value="Modules & Add-on s" id="Category_4" />
Modules & Add-on's</td>
</tr>
<tr>
<td> <input type="checkbox" name="Category[]" value="Kawai Digital Pianos" id="Category_5" />
</label>
</p>
<p>
<label>Manufacturer
<select name="Manufacturer" id="Manufacturer">
<option value="Casio">Casio</option>
<option value="Kawai">Kawai</option>
<option value="Korg">Korg</option>
<option value="Roland">Roland</option>
<option value="Yamaha">Yamaha</option>
<option value="Ketron">Ketron</option>
<option value="Boss">Boss</option>
<option value="Samson">Samson</option>
<option value="Orla">Orla</option>
<option value="Technics">Technics</option>
<option value="Ultimax">Ultimax</option>
</select>
</label>
</p>
<p>
<label>Model
<input type="text" name="Model" id="Model" />
</label>
</p>
<p>
<label>Color
<input type="text" name="Color" id="Color" />
</label>
</p>
<p>
<label>
<p><input name="submit" type="submit" value="submit" /></p>
<input type="hidden" name="MM_insert" value="form1" />
</form>
我想为类别的每个值在数据库中创建一个新行
我正在使用 DW 开始,如下所示: 插入代码:
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 products (Category, Manufacturer, Model, Color) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST['Category'], "text"),
GetSQLValueString($_POST['Manufacturer'], "text"),
GetSQLValueString($_POST['Model'], "text"),
GetSQLValueString($_POST['Color'], "text");
mysql_select_db($database_dconn, $dconn);
$Result1 = mysql_query($insertSQL, $dconn) or die(mysql_error());
}
这不起作用,因为它以某种方式找不到类别我认为这是因为它是一个数组。有没有一种方法可以使它工作,以便在勾选到新字段时确实为每个类别插入表单。 我尝试了以下方法:
if (isset($_POST['Category'])) {
foreach($_POST['Category'] as $value);
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
etc...
但这似乎根本不起作用。 欢迎任何帮助
首先,read up on PDO并停止使用mysql()
功能。接下来,您与 foreach 接近,但停止使用 isset - 即使字段为空,这对于发布的表单也总是 return true(因为变量由于发布而初始化,即使它为 NULL - 但是不要不要尝试检查 NULL,因为它仍然会在值“”时作为 true 传递,而是使用 if(!empty($_POST['field']))
。关于针对类别数组的 foreach,请尝试以下操作。
现在,我注意到了一些问题。您是否希望 运行 多个 SQL 插入取决于检查类别的数量?这些颜色、制造商等值是否相同?如果是这样,你就走在正确的轨道上。如果没有,我建议您重做整个表格(例如,如果您需要为每个选中的类别提供唯一值)。接下来,为什么要传递带有表单名称值的隐藏字段?这是为了验证数组是否已发布?
最后但同样重要的是,get_magic_quotes_gpc()
- 不,就是不。首先,使用 PDO 不需要在进入 SQL 查询的过程中对字符串进行清理(虽然理解危险的荣誉,如果不是赢得执行点的话)。此外,在显示上,您真正需要的只是 htmlspecialchars()
来清理任何显示的输出。这将达到目的并消除对您的功能的需求。
编辑:试试下面的(没有任何 HTML 格式)
你能不能简单地试试:
if(!empty($_POST['submit'])) {
foreach ($_POST['category'] AS $val) {
# $val will hold the value of your checkbox
# Run SQL insert
}
} else {
# Display HTML form
}