0

//This is my php code. i am trying to store data in a table using php and mysql using post method in html form tag but it shows an error that query is empty. i have just started to learn php so might be a dumb one but plz help me with it

<?php

   define('DB_NAME', 'db1');
   define('DB_USER', 'root');
   define('DB_PASSWORD', '');
   define('DB_HOST', 'localhost');

     $link= mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
       if(!$link){
       die('Could not connect:'. mysql_error());
       }
     $db_selected=mysql_select_db(DB_NAME,$link);
       if(!$db_selected){
       die('Cant use' .DB_NAME. ';'. mysql_error());
       }

     $value1="";
     $value2="";
     $value3="";
     $value4="";
     $value5="";
     $value6="";
     $value7="";
     $value8="";
     $value9="";
     $value10="";
     $value11="";
     $value12="";
     $value13="";
     $value14="";
     $value15="";
     $value16="";
     $value17="";
     $value18="";
     $value19="";
     $value20="";
     $value21="";
     $value22="";

     if(isset($_POST['submit'])){
     $value1=$_POST['Full Name'];
     $value2=$_POST['Fathers Name'];
     $value3=$_POST['Mothers Name'];
     $value4=$_POST['DOB'];
     $value5=$_POST['Gender'];
     $value6=$_POST['Marital Status'];
     $value7=$_POST['Husbands Name'];
     $value8=$_POST['Category'];
     $value9=$_POST['Sub-Category'];
     $value10=$_POST['Nationality'];
     $value11=$_POST['Religion'];
     $value12=$_POST['Address'];
     $value13=$_POST['Mobile no'];
     $value14=$_POST['Email-ID'];
     $value15=$_POST['Exam passed'];
     $value16=$_POST['Board'];
     $value17=$_POST['Passing Year'];
     $value18=$_POST['Percentage12'];
     $value19=$_POST['Migcir'];
     $value20=$_POST['ProofDOB'];
     $value21=$_POST['Marksheet'];
     $value22=$_POST['GapCertificate'];
      }
    $sql= mysql_query ("INSERT INTO cslist(Full Name, Fathers Name, Mothers          Name,DOB,Gender,Marital Status,Husbands Name,Category,Sub-Category,Nationality,Religion,Address,Mobile no,Email-ID,Exam passed, Board,Passing Year,Percentage12,Migcir,ProofDOB,Marksheet,GapCertificate) VALUES (
    '$value1','$value2','$value3','$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12','$value13','$value14','$value15','$value16','$value17','$value18','$value19','$value20','$value21','$value22')");

    $res=mysql_query($sql) or die ("Error: ". mysql_error(). " with query ".     $sql);
    mysql_close();

    ?>
  • were you able to connect to db ? – Ravi Apr 02 '17 at 09:56
  • Do not use **mysql_*** as they are deprecated. – Mayank Pandeyz Apr 02 '17 at 09:57
  • Use backtics on the column names, especially those with spaces in them, or better yet, don't create column names with spaces. – Sloan Thrasher Apr 02 '17 at 09:57
  • btw, what is the error, you are getting ? pls post your error as well – Ravi Apr 02 '17 at 09:58
  • 1. Do `var_dump($_POST);` to see the complete structure of `$_POST` superglobal. 2. `$sql` is a resource, not a string. Why are you doing `$res=mysql_query($sql) ...` ? 3. Don't use `mysql_*` functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use [`mysqli`](http://php.net/manual/en/book.mysqli.php) or [`pdo`](http://php.net/manual/en/book.pdo.php) instead. [And this is why you shouldn't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – Rajdeep Paul Apr 02 '17 at 09:58

1 Answers1

0

The problem is here:

(Full Name, Fathers Name, Mothers          Name,DOB,Gender,Marital Status,Husbands Name,Category,Sub-Category,Nationality,Religion,Address,Mobile no,Email-ID,Exam passed, Board,Passing Year,Percentage12,Migcir,ProofDOB,Marksheet,GapCertificate)

your column names are having space in them, some are in small case and some are in capital case. This is a very bad DB structure.

Either change the column names by making all columns small case and use _ underscore instead of space or enclosed your column names with ' single quotes

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59