【问题标题】:SQL Insert doesn't workSQL 插入不起作用
【发布时间】:2012-02-26 10:48:27
【问题描述】:

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

【问题讨论】:

  • 另一件事值传递给该文件,因为我在 firebug 中检查过,以下是值:
  • 您收到什么错误信息? (mysql_errno()mysql_error() 返回什么?)
  • 已排序。我忘了用引号将字符串括起来。

标签: php mysql get


【解决方案1】:

你忘了用引号把字符串括起来。

  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 );

【讨论】:

    【解决方案2】:

    mysql_query("插入录制的视频 (user_id, streamName, record_duration, recorder_id) 值 ($userId, '$streamName', '$streamDuration', '$recorderId')");

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

    【讨论】:

      【解决方案3】:

      你必须enclose string fields with single quotes

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-10
        • 2016-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多