gpt4 book ai didi

php - SQL 插入不起作用

转载 作者:行者123 更新时间:2023-11-29 01:23:41 25 4
gpt4 key购买 nike

为什么这个简单的插入不起作用?唯一未插入的值是 $streamName,如果我从代码中删除该值,那么一切正常,代码如下:

 $userId= $_SESSION['kt_login_id'];
$streamName= $_GET['streamName'];
$streamDuration= $_GET['streamDuration'];
$recorderId= $_GET['recorderId'];

$con = mysql_connect("localhost","wwwmeety_staff","xxxxxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("wwwmeety_ourstaff", $con);

mysql_query("INSERT INTO recordedvideos (user_id, streamName, record_duration, recorder_id)
VALUES ($userId, $streamName, $streamDuration, $recorderId)");


mysql_close($con);

和MySQL导出

CREATE TABLE IF NOT EXISTS `recordedvideos` (
`record_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`streamName` text,
`record_duration` text,
`recorder_id` text,
PRIMARY KEY (`record_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

最佳答案

您忘记用引号将字符串括起来。

  mysql_query(
"INSERT INTO recordedvideos
(user_id, streamName, record_duration, recorder_id) VALUES
($userId, '$streamName', '$streamDuration', '$recorderId')"
);

但为了避免 sql 注入(inject),这种方法更可取:http://php.net/manual/en/function.mysql-query.php

$query = sprintf("INSERT INTO recordedvideos 
(user_id, streamName, record_duration, recorder_id) VALUES
($userId, '%s', '%s', '%s')",
mysql_real_escape_string($streamName),
mysql_real_escape_string($streamDuration),
mysql_real_escape_string($recorderId)
);

mysql_query( $query );

关于php - SQL 插入不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9452470/

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