0

I am struggling with this piece of code. Why doesn't it work? It does receive the variables, but the sql command doesn't work.

 if ($action=='create')
  {

  $Surname=$_REQUEST['Surname'];
  $Name=$_REQUEST['Name'];
  $Fathername=$_REQUEST['Fathername'];
  $Dateofbirth=$_REQUEST['Dateofbirth'];
  $Afm=$_REQUEST['Afm'];
  $Landline=$_REQUEST['landline'];
  $Mobile=$_REQUEST['Mobile'];
  $Address=$_REQUEST['Adrs'];
  $Addressnum=$_REQUEST['Adrsnm'];
  $Location=$_REQUEST['Location'];
  $ZIP=$_REQIEST['Zip'];
  $Bankaccount=$_REQUEST['Bankaccount'];

$createuser=mysql_query("INSERT INTO `Customers` (`Surname`,`CName`,`Fathername`,`Birthdate`,`AFM`,`Landline`,`Mobile`,`Address`,`Adressnum`,`Location`,`ZIP`,`Bankaccount`) VALUES ('$Surname','$Name','$Fathername','$Dateofbirth','$Afm','$Landline','$Mobile','$Address','$Addressnum','$Location','$ZIP','$Bankaccount')");
}
3
  • Try printing the result of mysql_error() to see what the problem is. Commented Feb 15, 2013 at 23:49
  • or exit(mysql_error()); Commented Feb 15, 2013 at 23:49
  • More information is needed. Does it output an error? If so what is the error? Have you created a mysql connector? Commented Feb 15, 2013 at 23:49

1 Answer 1

1
mysql_query("INSERT INTO `Customers` (`Surname`,`CName`,`Fathername`,`Birthdate`,`AFM`,`Landline`,`Mobile`,`Address`,`Adressnum`,`Location`,`ZIP`,`Bankaccount`) VALUES ('$Surname','$Name','$Fathername','$Dateofbirth','$Afm','$Landline','$Mobile','$Address','$Addressnum','$Location',
'$ZIP','$Bankaccount')")or die(mysql_error());

Will give you the answer.

And make sure to escape your data:

 $Surname=mysql_real_escape_string($_REQUEST['Surname']);
  $Name=mysql_real_escape_string($_REQUEST['Name']);
  $Fathername=mysql_real_escape_string($_REQUEST['Fathername']);
 [...]

Better, use a prepared statement:

$q =  $sql->prepare("INSERT INTO `Customers` SET `Surname` = ? [...]");
$q->execute( array( $_REQUEST['Surname'], [...] ) );
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, actually the problem was connecting to the database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.