会话ID更改保存到DB

时间:2014-03-18 23:10:51

标签: php mysql session

我在Session上有这个小问题。我会给你看下面的代码。

//checking session orderid if not created  
$setSession = $_SESSION['neworderid'];
if (empty($setSession)) {
    $neworderid = mt_rand(1000000000, 9999999999);  
    $_SESSION['neworderid'] = $neworderid;
}

//check if order_id exists in the database  
$db->setQuery("SELECT * FROM mn_orderitems WHERE order_id =" . $_SESSION['neworderid']);  
$query = $db->loadResult();  
if (!empty($query)) {  
    //if exists, do nothing  
    echo 'Not Empty';  
} else {
    //if order id doesn't exist, save the new order item  
    $qry = "INSERT INTO mn_orderitems (order_id, product_id, orderitem_name, orderitem_quantity, orderitem_price, orderitem_final_price) VALUES
        ('" . $_SESSION['neworderid'] . "', '" . $item->product_id . "', '" . $item->orderitem_name . "', '" . $item->orderitem_quantity . "', '" . $item->orderitem_price . "', '" . $item->orderitem_final_price . "')";  
    $result = mysql_query($qry);  
    if (!$result) {  
        echo "Error";  
    } else {  
        echo "Saved";  
    }  
    echo 'Empty';  
}  

问题:
“当我试图回应$_SESSION['neworderid'];它outputs = 8152269414时 但是当它保存到数据库时,order_id更改为2147483647“。

这只发生在实时服务器中。我的localhost Apache没问题。

2 个答案:

答案 0 :(得分:2)

您遇到的问题不在于PHP脚本本身,而在于您的数据库结构。在MySQL中,您可以存储的最大数字(声明为INT)是2147483647.如果您想存储更大的数字,您必须更改脚本以生成较小的数字或更改数据库以使用BIGINT。

答案 1 :(得分:0)

将phpMyAdmin中的第一个order_id结构从 INT 更改为 BIGINT

/* checking session orderid if not created  */

session_start();

if (empty($_SESSION['neworderid'])) {
    $neworderid = mt_rand(1000000000, 9999999999);  
    $_SESSION['neworderid'] = $neworderid;
    $setSession = $_SESSION['neworderid'];
}

else {

$setSession = $_SESSION['neworderid'];

}

/* check if order_id exists in the database and convert the code to mysqli */
/* have your connection store into $db variable */
$result=mysqli_query($db, "SELECT * FROM mn_orderitems WHERE order_id ='$setSession'");  

if (mysqli_num_rows($result)!=0) {  
    /* If a record has been found, do nothing */
    echo "Not Empty";  
} else {

    /* if order id doesn't exist, save the new order item */

    $insert=mysqli_query($db,"INSERT INTO mn_orderitems (order_id, product_id, orderitem_name, orderitem_quantity, orderitem_price, orderitem_final_price)
    VALUES ('$setSession', '$product_id', '$orderitem_name', '$orderitem_quantity', '$orderitem_price', '$orderitem_final_price')");  

    if($insert){
    echo "Successfully Added item no. ".$setSession;
    }

}