SQL插入不起作用

时间:2012-02-26 10:48:28

标签: php mysql get

为什么这个简单的插入不起作用?唯一没有插入的值是$ 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 ;

3 个答案:

答案 0 :(得分:4)

你忘记用引号括起来。

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

但是为了避免sql注入,这种方法更可取: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 );

答案 1 :(得分:1)

mysql_query(“INSERT INTO录制视频(user_id,streamName,record_duration,recorder_id) VALUES($ userId,'$ streamName','$ streamDuration','$ recorderId')“);

在字符串类型值周围加上单引号。

答案 2 :(得分:1)

您必须enclose string fields with single quotes

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