Aadil Afzal
Aadil Afzal

Reputation: 63

MY sql query not working fully

I am using to add data into DB. First i get the values from post and then insert it into table. The problem is that there are total 7 values but only 5 values added and 2 of them not inserted into the table. Here is my code

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {
        $degree_title = $_POST['degree_title'];
        $degree_year = $_POST['degree_year'];
        $uni_name = $_POST['uni_name'];
        $degree_level = $_POST['degree_level'];
        $major_sub = $_POST['major_sub'];



        $run = mysql_query("INSERT INTO `career_fourudb`.`tffeck_employee_edu` (`id`, `employee_id`, `degree`, `year`, `degree_level`, `major_degree`, `uni`) 
        VALUES (NULL, $eme_uid, $degree_title, $degree_year, $degree_level, $major_sub, $uni_name)");


    }

I echo the all values and all values are coming so why they all not inserted into table any idea. Thank

Upvotes: 2

Views: 104

Answers (3)

bestprogrammerintheworld
bestprogrammerintheworld

Reputation: 5520

My guess is that $degree_title and $uni_name doesn't get inserted because they are varchars. In that case you will have to put quotes around these values.

Mysql is kind of "forgiving" in the sence that it does not throw an error when using incorrect types in the sql-statement in relation to the actual type of the column.

Try:

$run = mysql_query("INSERT INTO `career_fourudb`.`tffeck_employee_edu` (`id`, `employee_id`, `degree`, `year`, `degree_level`, `major_degree`, `uni`)
      VALUES (NULL, $eme_uid, '$degree_title', $degree_year, $degree_level, $major_sub, '$uni_name')");

As mentioned before id doesn't have to be included (if id-column is autoincremental) in the insert-statement, and you should really learn mysqli or PDO.

Upvotes: 0

Rufinus
Rufinus

Reputation: 30753

try:

$run = mysql_query("INSERT INTO `career_fourudb`.`tffeck_employee_edu` (`id`, `employee_id`, `degree`, `year`, `degree_level`, `major_degree`, `uni`) 
    VALUES (NULL, '$eme_uid', '$degree_title', '$degree_year', '$degree_level', '$major_sub', '$uni_name')");

and i would highly recommend:

1) dont use mysql_ its deprecated, use mysqli_*

2) sanitze ALL values in _POST befor using in SQL statements.

Upvotes: 1

echo_Me
echo_Me

Reputation: 37253

if id is autoincrement then you dont need to insert it.

try this

     $run = mysql_query("INSERT INTO `career_fourudb`.`tffeck_employee_edu` (`employee_id`, `degree`, `year`, `degree_level`, `major_degree`, `uni`) 
    VALUES ($eme_uid, $degree_title, $degree_year, $degree_level, $major_sub, $uni_name)");

Upvotes: 0

Related Questions