gpt4 book ai didi

php - 将复选框 PHP 表单项作为数组插入到 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-28 23:51:07 26 4
gpt4 key购买 nike

我有一个带有复选框的 PHP 表单;我想为每个复选框项目插入一个新的行项目。这是我的表格:

<form id="form1" name="form1" method="post" action="/form/formsubmit.php?">
<p>&nbsp;</p>
<table align="center">
<tbody><tr valign="baseline">
<td><p> <input type="checkbox" value="2" name="Cat_Ref_Array[]">
<label for="Category">Food</label>
<input type="checkbox" value="3" name="Cat_Ref_Array[]">
<label for="Category">Camping</label>
<input type="checkbox" value="5" name="Cat_Ref_Array[]">
<label for="Category">Shopping</label>
<input type="checkbox" value="7" name="Cat_Ref_Array[]">
<label for="Category">Cinema</label>
</p></td>
</tr>
<tr valign="baseline">
<td><select name="dist">
<option value="5">5 Miles</option>
<option value="10">10 Miles</option>
<option value="25">25 Miles</option>
<option value="50">50 Miles</option>
<option value="100">100 Miles</option>
</select></td>
</tr>
<tr valign="baseline">
<td><input type="submit" value="Insert record"></td>
</tr>
</tbody></table>
<input type="hidden" value="-1" name="Ref">
<input type="hidden" value="form1" name="MM_insert">
</form>

...这是我的 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")) {

$chkbox = $_POST['Cat_Ref_Array'];
$i = 0;
While($i<sizeof($chkbox))
$insertSQL = sprintf("INSERT INTO locations (`Ref`, Cat_Ref, dist) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['Ref'], "text"),
GetSQLValueString($chkbox[$i], "int"),
GetSQLValueString($_POST['dist'], "int"));

mysql_select_db($database_DB, $DB);
$Result1 = mysql_query($insertSQL, $DB) or die(mysql_error());
$i++;

我不断遇到各种超时和失败 - 我做错了什么?

最佳答案

尝试像下面这样添加括号 for while

$i = 0;
While($i<sizeof($chkbox)){
$insertSQL = sprintf("INSERT INTO locations (`Ref`, Cat_Ref, dist) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['Ref'], "text"),
GetSQLValueString($chkbox[$i], "int"),
GetSQLValueString($_POST['dist'], "int"));

mysql_select_db($database_DB, $DB);
$Result1 = mysql_query($insertSQL, $DB) or die(mysql_error());
$i++;
}

关于php - 将复选框 PHP 表单项作为数组插入到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32569590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com