我有一个难题。我有一个简单的表格。从数据库和文本区域中提取技术人员姓名的动态选择列表。该表单旨在让我粘贴邮政编码(采用以下格式:12345、12346、12347 等),并将它们与技术人员的 ID 一起一一插入到表中。这太棒了!然而,任何 zip 都行不通——因为我必须有许可证才能在其中工作。
为了解决这个恼人的问题,我创建了一个表,其中包含我提供服务的邮政编码列表。所以,我有这些邮政编码的记录集,我将它们插入到一个数组中,然后在插入之前检查该数组中是否存在来自 textarea 的值。除此之外,它不起作用。数组打印得很好 - 这样就可以了。插入的东西工作得很好 - 就像我添加 in_array 的东西之前一样。在一起 - 他们就像我和我的女朋友。它根本无法协同工作。
这是我的代码:
//This is my recordset and where I'm putting it into an array
mysql_select_db($database_localhost, $localhost);
$query_Recordset1 = "SELECT zip_code FROM zip_code";
$Recordset1 = mysql_query($query_Recordset1, $localhost) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
$results = array();
do {
$results[] = $row_Recordset1;
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
//DONE ARRAY
//Now I'm taking zipcodes pasted in textarea so they can be inserted one at a time
$zipcodes = explode(",", $_POST['textarea']);
$countZips = (count($zipcodes));
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
foreach($zipcodes as $key=>$value)
{
while ($countZips >= 1) {
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
//DONE textarea
//If statement to see if value exists in array
if (in_array($value, $results)){
$insertSQL = sprintf("INSERT INTO zip_zip (zip_code, tech_id) VALUES (%s, %s)",
GetSQLValueString($value, "int"),
GetSQLValueString($_POST['select'], "int"));
mysql_select_db($database_localhost, $localhost);
$Result1 = mysql_query($insertSQL, $localhost) or die(mysql_error());
$countZips--;
} else {
$countZips--;
}
//Done value exists
//Now moving to next page when done
}
$insertGoTo = "index.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
}
//END
有谁知道问题出在哪里吗?另外 - 如果有人编辑它并使代码格式变得漂亮 - 你会怎么做?
谢谢!
最佳答案
更改以下行
$results[] = $row_Recordset1;
到下面一行 $results[] = $row_Recordset1['zip_code'];
在您的代码中,在从数据库获取记录时,您将每一行分配到 $result 数组中,这就是为什么当您要检查 $result 数组中的 $value 时给出错误结果,因为您的 $result 数组有根据您上述代码的以下结构
数组 ( [0] => 数组 ( [邮政编码] => 12312331 ) )
并且您需要 in_array() 以下数组结构
数组 ( [0] => 12312331 )
关于php - 查看数组中是否存在文本区域值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19683211/