-3

My form dose not submit the data inserted into my database but it just displays an empty page with no error message and the echo is not executed.

Here is the php code

<?php

 if(isset($_POST['submit']))

  {

    $username="root";
    $password="";
    $db_host="localhost";
    $database="logintrial";

    $con = mysql_connect($db_host,$username,$password);

    mysql_select_db($database,$con);

    $sql="INSERT INTO abonne (N_A,NUMBER,TYP,NO_FTX,ADDRESS,D_R,D_E,lengthone,
     P,lengthtwo,GARDY,A,B,C_F,POLES)
    VALUES
   ('$_POST[N_A]','$_POST[NUMBER]','$_POST[TYP]','$_POST[NO_FTX]','$_POST[ADDRESS]',"
    . "'$_POST[D_R]','$_POST[D_E]','$_POST[lengthone]','$_POST[P]''$_POST[lengthtwo]',"
    . "'$_POST[GARDY]','$_POST[A]''$_POST[B]',$_POST[C_F]','$_POST[POLES]')";
    $a=mysql_query($sql);

    if (!$a)
      {
       echo mysql_error();
      }
   else
      {
        echo "1 record added";
      }

 }
  • Have you set the right error reporting level? Take a look at http://php.net/manual/en/function.error-reporting.php – smiggle Aug 22 '14 at 10:50
  • 1
    As a reminder, the [*mysql* functions are deprecated](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), you should use *mysqli* or *PDO* instead. Also, be careful with this code, its wide open to [SQL injection attacks](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – mTorres Aug 22 '14 at 11:03
  • Yes but its not displaying any error and no value has been inserted either @smiggle – Stainopel' Aug 22 '14 at 11:15
  • Can you add ```or die(mysql_error())``` after your ```mysql_connect```? – mTorres Aug 22 '14 at 17:05
  • Yes i already did it – Stainopel' Aug 25 '14 at 07:31
  • And? Did it yell any connection error? – mTorres Aug 26 '14 at 11:56

2 Answers2

0

You just have 2 syntax error on your SQL query:

. "'$_POST[D_R]','$_POST[D_E]','$_POST[lengthone]','$_POST[P]''$_POST[lengthtwo]',"
//                                                          ^^^^
//                                                        error here!
."'$_POST[GARDY]','$_POST[A]''$_POST[B]',$_POST[C_F]','$_POST[POLES]')";
//                        ^^^^
//                   error here also!

Just put the comma , between the 2 fields:

. "'$_POST[D_R]','$_POST[D_E]','$_POST[lengthone]','$_POST[P]','$_POST[lengthtwo]',"
."'$_POST[GARDY]','$_POST[A]','$_POST[B]',$_POST[C_F]','$_POST[POLES]')";

As an advice, when developing, you should set the correct error reporting to spot syntax (and other) errors. To sum add, just add:

ini_set('display_errors', 1);
error_reporting(E_ALL);       // If running PHP < 5.4 then E_ALL | E_STRICT

in the begging of your script (or even better change the php.ini on the dev machine).

Community
  • 1
  • 1
mTorres
  • 3,590
  • 2
  • 25
  • 36
  • It still has not inserted the values into the database but thanks anyway for your advice – Stainopel' Aug 22 '14 at 11:25
  • Can you edit the question and add the output of ```var_dump($sql)```? – mTorres Aug 22 '14 at 17:08
  • Please can you explain what var_dump($sql) is suppose to archieve @mTorres – Stainopel' Aug 25 '14 at 08:22
  • ```var_dump``` is the way to ouput the value of the variable (in this case the $sql variable, my intention is to see the value and check if there are any other non-spotted errors (maybe the $_POST does not have all values?) – mTorres Aug 26 '14 at 11:57
-1

Try like this:

 $con=mysqli_connect("localhost","root","if you have password to db use it here otherwise leave it empty","logintrial");

 $qry="insert into abonne (N_A,NUMBER,TYP,NO_FTX,ADDRESS,D_R,D_E,lengthone, P,lengthtwo,GARDY,A,B,C_F,POLES)
  values('$_POST[N_A]','$_POST[NUMBER]','$_POST[TYP]','$_POST[NO_FTX]','$_POST[ADD‌​RESS]'," . "'$_POST[D_R]','$_POST[D_E]','$_POST[lengthone]','$_POST[P]''$_POST[lengthtwo]',‌​" . "'$_POST[GARDY]','$_POST[A]''$_POST[B]',$_POST[C_F]','$_POST[POLES]')";

 $res=mysqli_query($con,$qry);
DRK
  • 160
  • 1
  • 4
  • 14