mysql_insert_id()中的未定义变量

时间:2015-10-26 05:53:09

标签: php mysql

虽然我正确地声明了这个变量,但我在这里呆了差不多4小时却无法找到变量中的错误。

错误提示如下:

  

注意:未定义的变量:last_id in   第87行的C:\ xampp \ htdocs \ CIS \ addSubscriber.php

以下是代码:

 if(mysql_query("INSERT INTO subscribers(fName, mName, lName, suffix, globeNumber, emailAddress, contactNumber, 
    address, state, country, virtualNumber, agentCode, ipAddress, dateRegistration, dateStarted, dateExpired, time, 
    transaction_id, accountStatus, subscriptionStatus, status, remarks)
  VALUES ('$fName' , '$mName', '$lName', '$sName', '$globe', '$emailAdd', '$contactNo', '$address', '$state', '$country', 
  '$virtualNum', '$cAgentCode', '$ipAddress', '$dateRegistration', '$dateStarted', '$dateExpired', '$time', 
  '$transaction', '$accStatus', '$subsStatus', '$status', '$remarks' )"))
{ 

    $last_id = mysql_insert_id(); 


}

**//this is line 87** 

$query =  "INSERT INTO `transactions` (`transactionNumber`, `subscriptionStart`, `subscriptionEnd`, `subsStatus`, `cus_id`)
VALUES ('".$transaction."', , '".$dateStarted."', '".$dateExpired."', '".$subsStatus."', '".$last_id."');"; 

  if($query_run = mysql_query($query)){


 ?>
  <script> alert('<?php echo $last_id; ?>'); 
 </script>

    <?php
} else {
    ?>
    <script>alert('Agent Code is not available!');</script>
    <?php
}

}

 echo mysql_error();

我在其他文件中尝试使用它,但它正常工作我无法找到错误的触发器。

与基本未定义变量错误不同:

2 个答案:

答案 0 :(得分:1)

这个怎么样?

$query = mysql_query("INSERT INTO subscribers(fName, mName, lName, suffix, globeNumber, emailAddress, contactNumber, 
    address, state, country, virtualNumber, agentCode, ipAddress, dateRegistration, dateStarted, dateExpired, time, 
    transaction_id, accountStatus, subscriptionStatus, status, remarks)
  VALUES ('$fName' , '$mName', '$lName', '$sName', '$globe', '$emailAdd', '$contactNo', '$address', '$state', '$country', 
  '$virtualNum', '$cAgentCode', '$ipAddress', '$dateRegistration', '$dateStarted', '$dateExpired', '$time', 
  '$transaction', '$accStatus', '$subsStatus', '$status', '$remarks' )");

if(!$query)
{ 
    echo mysql_error();
}
else
{
    $last_id = mysql_insert_id(); 
    $query2 =  mysql_query("INSERT INTO `transactions` (`transactionNumber`, `subscriptionStart`, `subscriptionEnd`, `subsStatus`, `cus_id`)
        VALUES ('$transaction', '$dateStarted', '$dateExpired', '$subsStatus', '$last_id')"); 
    if(!$query2)
    { ?>
        <script> 
            alert('<?php echo $last_id; ?>'); 
        </script>
    <?php
    } 
    else {
    ?>
        <script>
            alert('Agent Code is not available!');
        </script>
    <?php
    }
}
  

MySQL扩展名在PHP 5.5.0中已弃用,并已在PHP 7.0.0中删除。相反,应该使用MySQLi或PDO_MySQL扩展。小号

答案 1 :(得分:1)

尝试放

else { die(mysql_error()); }

找到错误。