插入查询后设置会话值

时间:2011-10-25 19:53:54

标签: php mysql

我试图在第一次成功插入查询后设置会话变量。我有一个javascript确认以检查用户是否想要添加更多数据。如果用户想要添加更多产品,那么我不必插入第一个表中,因此请保留会话值。如果没有,则清除会话数据。我不确定这是否正确。如果有任何建议,请你告诉我。

<?php                  
if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{               
    session_start(); 

   if (empty($_SESSION['new_id'])) 
   {
       $first = $_POST["first"];
       $first = mysql_real_escape_string($first);
       $last = $_POST["last"];
       $last = mysql_real_escape_string($last);
       $insertsql = "INSERT INTO Table1(FirstName,LastName) VALUES ('".$first."', '" .$last. "')";              
       $result1 = mysql_query($insertsql) or die(mysql_error());
       $new_id = MySql_Insert_Id();
       $_SESSION['new_id'] = $new_id;
    }
    $prodName = $_POST["prodName"];
    $prodName = mysql_real_escape_string($prodName); 
    $prod_details = $_POST["prod_details"];
    $prod_details = mysql_real_escape_string($prod_details);
    $insertSQl2 = "INSERT INTO Table2($_SESSION['new_id'], Product_Name, Product_Details) VALUES ('".$new_id."', '" .$prodName. "', '" .$prod_details. "')";               
$Result2 = mysql_query($insertSQl2) or die(mysql_error());      
}  
?> 
<form>
  //controls here
</form>
<?php
    if($Result2 == true)
    {
    echo "Data saved Successfully";
    echo '<script type="text/javascript">'    , 'if confirm("Would like to add more Products?") '    , '</script>';
    }
?>      

0 个答案:

没有答案