user974435
user974435

Reputation: 397

SQL Insert doesn't work

Why doesn't this simple insertion work? The only value not inserted is $streamName, if I remove that value from code then everything works, here's the code:

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

and MySQL export

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 ;

Upvotes: 2

Views: 3918

Answers (3)

dani herrera
dani herrera

Reputation: 51715

You forgot enclose string in quotes.

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

But to avoid sql injection is preferable this approach: 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 );

Upvotes: 4

John Woo
John Woo

Reputation: 263883

You have to enclose string fields with single quotes

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

Upvotes: 1

Peter Kiss
Peter Kiss

Reputation: 9329

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

Put single quotes around the string type values.

Upvotes: 1

Related Questions