-3

I am using mysql_query to execute a insert query. It fails to save a particular user's data due to DB is not able to insert the Educations column's value, returns false result. This is working fine for remaining users.

$result = mysql_query("INSERT INTO user (FirstName,LastName,Pic,Birthday,Industry,Languages,Summary,Email,Positions,Skills,Publications,Volunteer,Location,Awards,Certifications,Interests,Educations)
values ('$first_name','$last_name','$pic','$birthday','$industry','$languages','$summary','$email','$positions','$skills','$publications','$volunteer','$location','$awards','$certifications','$interests','$education')",$mysql);

All fields are string values.

tshepang
  • 12,111
  • 21
  • 91
  • 136

2 Answers2

1

The variable may not be escaped. I suggest you switch to using the PDO library because as of PHP 5.5, the MySQL native extension will be deprecated. PDO has many advantages over MySQL native such as prepared statements and named parameters. Prepare method will escape and also auto quote variables that you pass in. You won't have to worry about it anymore. Take a look at my rewrite of your current code:

$pdo = new PDO($dsn, $user, $password);

$values_array = array(
    ':first_name' => $first_name,
    ':last_name' => $last_name,
    ':pic' => $pic,
    ':birthday' => $birthday,
    ':industry' => $industry,
    ':languages' => $languages,
    ':summary' => $summary,
    ':email' => $email,
    ':positions' => $positions,
    ':skills' => $skills,
    ':publications' => $publications,
    ':volunteer' => $volunteer,
    ':location' => $location,
    ':awards' => $awards,
    ':certifications' => $certifications,
    ':interests' => $interests,
    ':education' => $education
);

$result = $pdo->prepare('INSERT INTO user (FirstName,LastName,Pic,Birthday,Industry,Languages,Summary,Email,Positions,Skills,Publications,Volunteer,Location,Awards,Certifications,Interests,Educations) values (:first_name,:last_name,:pic,:birthday,:industry,:languages,:summary,:email,:positions,:skills,:publications,:volunteer,:location,:awards,:certifications,:interests,:education)');

$result->execute($values_array);

You might want to consider switching to PDO for future proof.

Kirill Fuchs
  • 13,446
  • 4
  • 42
  • 72
tungpham42
  • 26
  • 5
0

I notice most of the value variables are plural, except $education, though the key states its plural.

Aside from that, look at escaping and your quotes.

Cody
  • 1,389
  • 1
  • 9
  • 14
  • 3
    [`As said 3 minutes prior to this`](https://stackoverflow.com/questions/24073231/insert-query-is-failing#comment37123284_24073231) - this is a comment and not an answer. – Funk Forty Niner Jun 06 '14 at 02:41