Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用foreach循环检索记录并在未找到记录时插入到表中_Php_Mysql_Foreach_Insert - Fatal编程技术网

Php 使用foreach循环检索记录并在未找到记录时插入到表中

Php 使用foreach循环检索记录并在未找到记录时插入到表中,php,mysql,foreach,insert,Php,Mysql,Foreach,Insert,我有一个存储区号的会话。我想做的是遍历每个区号,看看MySql数据库中是否有任何记录。如果没有记录,那么我想将该区号插入另一个表中 我已经创建了一个循环来执行此操作,但它没有插入新记录。也许是我用的!伊塞特错了吗?如果我说不通,下面是我的代码: //Start //Set Variables. $today = date("Y-m-d"); $getAreaCodes = $_SESSION['area_code']; // Start Foreach Loop. For ever

我有一个存储区号的会话。我想做的是遍历每个区号,看看MySql数据库中是否有任何记录。如果没有记录,那么我想将该区号插入另一个表中

我已经创建了一个循环来执行此操作,但它没有插入新记录。也许是我用的!伊塞特错了吗?如果我说不通,下面是我的代码:

//Start
//Set Variables.

$today = date("Y-m-d");
$getAreaCodes = $_SESSION['area_code'];

    // Start Foreach Loop.  For every area code in session, get it's area code from the zip_zip table

    foreach($getAreaCodes as $value) {

        mysql_select_db($database_localhost, $localhost);
        $query_getAC = sprintf("SELECT area_code FROM zip_zip WHERE area_code = $value", GetSQLValueString($colname_getAC, "text"));
        $getAC = mysql_query($query_getAC, $localhost) or die(mysql_error());
        $row_getAC = mysql_fetch_assoc($getAC);
        $totalRows_getAC = mysql_num_rows($getAC);

            // If it does NOT exist, insert it into zip_areacodes.
            if (!isset($row_getAC)){
                $insertSQL = sprintf("INSERT INTO zip_areacodes (area_code, date, type) VALUES (%s, %s, %s)",
                    GetSQLValueString($value, "text"),
                    GetSQLValueString($today, "text"),
                    GetSQLValueString("bad", "text"));                          
                    $Result1 = mysql_query($insertSQL, $localhost) or die(mysql_error());
            }
    }

mysql_free_result($getAC);

//Moving to another page

header("Location: confirm.php");
exit;
@舍伍德专业


您应该能够通过两种方式完成此操作:
empty($row\u getAC)
$totalRows\u getAC==0
。在select语句之后应具有相同的效果。(两者都是,而不是您的
!isset($row\u getAC)

如果得到空值,php会将其解释为空。检查下表作为参考


问候语

你应该可以用两种方式来做:空($row_getAC)或$totalRows_getAC==0。在select语句之后应具有相同的效果。(都是,而不是你的!isset($row_getAC))是的,这很有效。我用空的。我感谢你的帮助!如果它将返回NULL,那么empty仍然有效吗?(用于其他用途)同样,由于这是用评论来回答的,我如何结束这个问题并使你的答案正确呢?我的错,写得太快了:)。检查下面我的答案。谢谢你,这很有效。我很欣赏这个链接,它有助于把事情弄清楚,并将被证明是有用的。