-4

i have written the following code but it is giving parse error . here is my code.

<?php

$link = mysql_connect('localhost', 'root', '9829126849');

if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';

mysql_select_db("recruitmentdb", $link); 

$sql="
    INSERT INTO 
        recruitmentapp_candidate
            (
                id,
                name,
                contact1,
                contact2,
                contact3,
                e_mail,
                reference_type,
                resume_urce,
                date_of_first_contact,
                hr_contact,
                experience_level,
                current_employer,
                current_city,
                highest_degree,
                year_of_highest_degree,
                prominent_college,
                possible_projects,
                skill_set,
                track,
                status,
                offer_date,
                acceptance_date,
                joining_date,
                joining_date,
                comment,
                feedback_temp,
                upload_date,
                vacancy_id
            )
    VALUES 
            (
                null,
                '$out['Name']',
                null, 
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                '$out['ExpLevel']',
                '$out['CurrEmp']',
                '$out['CurrCity']',                     
                '$out['HighestDegree']',
                '$out['Year_Passing']',
                null,
                null,
                '$out['Skill_set']',
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null
            )
";

if (!mysql_query($sql,$link))
{
    die('Error: ' . mysql_error());
}
echo "1 record added";
?>

I have an array named $out and I am using it to place the filed values in the database, but parse error occurs as follows:

PHP Parse error:  syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING .

i have tried many combinations but same problem occurs.

afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
mannu singh
  • 879
  • 3
  • 13
  • 22
  • It's really hard to find a syntax error in a 600+ character line of code. I'm not suggesting you stick to traditional 80 columns but you must place a limit somewhere. – Álvaro González Sep 12 '11 at 12:08
  • @Álvaro G. Vicario with good IDE it's easy :) – OZ_ Sep 12 '11 at 12:09
  • I can't see any syntax errors in the above code - please can you post the FULL error message (with line number and file name). Also, your query won't work because you can't place associative array keys directly into a string like this unless you surround them with curly braces, so `...null,'$out['Name']',null...`, should be `...null,'{$out['Name']}',null...`. Also you should be using [`mysql_real_escape_string()`](http://uk.php.net/manual/en/function.mysql-real-escape-string.php) to sanitize your data - ``...null,'".mysql_real_escape_string($out['Name'])."',null...`` – DaveRandom Sep 12 '11 at 12:10
  • @OZ_ -1 ... Do a column mapping is the way to go, less error prune, and better code readability – ajreal Sep 12 '11 at 12:11
  • 2
    Also, NEVER use the `root` user to connect to your database from a script! – DaveRandom Sep 12 '11 at 12:11
  • @ajreal ok, but where did you found that I'm against it? – OZ_ Sep 12 '11 at 12:13
  • @Álvaro G. Vicario yes, read my answer. – OZ_ Sep 12 '11 at 12:13
  • @OZ_ Not lah, I just pointed out which is inaccurate IMO. (Because not every one like to use IDE) – ajreal Sep 12 '11 at 12:14
  • @ajreal Mmmm... tasty error prunes... :-P – DaveRandom Sep 12 '11 at 12:17

1 Answers1

5

In SQL-query replace all entries of '$out[]' by {$out[]}
And try to use IDE: NetBeans or PhpStorm.

Also, don't forget to sanitize your data in SQL, consider to use PDO and don't use closing ?> tag.

Your fixed code:

<?php

$link = mysql_connect('localhost', 'name', 'password');
if (!$link)
{
    die('Could not connect: '.mysql_error());
}
echo 'Connected successfully';
mysql_select_db("recruitmentdb", $link);

$sql = "INSERT INTO recruitmentapp_candidate(id,name,contact1,contact2,contact3,e_mail,reference_type,resume_urce,date_of_first_contact,hr_contact,experience_level,current_employer,current_city ,highest_degree,year_of_highest_degree,prominent_college ,possible_projects,skill_set,track ,status ,offer_date,acceptance_date,joining_date,joining_date,comment,feedback_temp,upload_date,vacancy_id)VALUES (null,{$out['Name']},null, null,null,null,null,null,null, null,{$out['ExpLevel']},{$out['CurrEmp']},{$out['CurrCity']}, {$out['HighestDegree']},{$out['Year_Passing']},null,null,{$out['Skill_set']},null,null,null,null,null,null,null,null,null,null)";
if (!mysql_query($sql, $link))
{
    die('Error: '.mysql_error());
}
echo "1 record added";
Community
  • 1
  • 1
OZ_
  • 12,492
  • 7
  • 50
  • 68