gpt4 book ai didi

Mysql 不插入定义的值

转载 作者:行者123 更新时间:2023-11-29 09:09:36 24 4
gpt4 key购买 nike

session_start(); 
if(!$_SESSION['user_id'])
{
$_SESSION['user_id'] = rand(1, 1000000);


include 'database_connect.php';

mysql_query('INSERT INTO product_views (user_session_id)
VALUES
('.$_SESSION['user_id'].')');

}

$productid = $_GET['name'];

$query = 'SELECT * FROM product_views WHERE user_session_id = '.$_SESSION['user_id'].'';
$result = mysql_query($query);

while ($row = mysql_fetch_array($result))
{

mysql_query('UPDATE product_views SET modelNumber="'.$productid.'" WHERE user_session_id="'.$_SESSION['user_id'].'"');


}

我的字段 modelNumber 设置为 null,并且我正在通过最后一个查询执行更新。您是否认为由于默认值为 null,因此不允许插入?

我的表结构:

CREATE TABLE `product_views` (
`id` int(10) DEFAULT NULL,
`user_session_id` int(11) DEFAULT NULL,
`product_id` varchar(100) DEFAULT NULL,
`view_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`modelNumber` varchar(...

最佳答案

我很困惑:

$query = 'SELECT * FROM product_views WHERE user_session_id = '.$_SESSION['user_id'].''; 
$result = mysql_query($query);

while ($row = mysql_fetch_array($result))
{
mysql_query('UPDATE product_views SET modelNumber="'.$productid.'" WHERE user_session_id="'.$_SESSION['user_id'].'"');
}

如果您甚至没有使用 $row,为什么还要循环这个结果集?

编辑:我认为这就是您真正想做的事情:

session_start(); 
if(!$_SESSION['user_id'])
{
// Get the user ID
$_SESSION['user_id'] = rand(1, 1000000);
require_once('database_connect.php');

// Get the model number and escape it to prevent SQL injection
$model_number = mysql_real_escape_string($_GET['name']);

// Insert a row that associates the user_id with the model number
mysql_query("INSERT INTO product_views (user_session_id,modelNumber) VALUES('{$_SESSION['user_id']}', '$model_number')");
}

关于Mysql 不插入定义的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6011849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com