Inserting data into database

Nur
Nur used Ask the Experts™
on
Hi experts...

I have a problem in inserting data  which i have four attributes(no_kupon, tarikh,amaun,kasyer) to be insert into a table (resit)

The problem is only 3 attributes can be inserted into a table:
<?php
 if(isset($_POST['simpan'])){
  mysql_query("INSERT INTO  `teksi`.`resit` (`no_kupon` ,`tarikh` ,'amaun') VALUES ( '$_POST[no_kupon]',  '$_POST[tarikh]','$_POST[amaun]');");
  }
 ?>

when i use this sql to insert 4 attributes the data failed to be sent into database
<?php
 if(isset($_POST['simpan'])){
  mysql_query("INSERT INTO  `teksi`.`resit` (`no_kupon` ,`tarikh` ,'amaun',`kasyer`) VALUES ( '$_POST[no_kupon]',  '$_POST[tarikh]','$_POST[amaun]',$_POST[kasyer]');");
  }
 ?>

here is my full coding: https://jsfiddle.net/Lp67q7p0/

Please someone here help me...I only have a day to settle this problem before submission.Thank you
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can you "+ dot(.) symbol to combine the variables into string.  Can you post what is the error exactly you are getting?

Please check if below code works.

  if(isset($_POST['simpan'])){
	  
	  print_r($_POST);
	  
  mysql_query("INSERT INTO  `teksi`.`resit` (`no_kupon` ,`tarikh` ,'amaun',`kasyer`) VALUES ( '".$_POST[no_kupon]."', '".$_POST[tarikh]."','".$_POST[amaun]."','".$_POST[kasyer]."');"); 
  }
 ?>

Open in new window

Ryan ChongSoftware Team Lead

Commented:
 mysql_query("INSERT INTO  `teksi`.`resit` (`no_kupon` ,`tarikh` ,'amaun',`kasyer`) VALUES ( '$_POST[no_kupon]',  '$_POST[tarikh]','$_POST[amaun]',$_POST[kasyer]');");
what error you encountered here?
Ryan ChongSoftware Team Lead

Commented:
ok, you may try this:

mysql_query("INSERT INTO  `teksi`.`resit` (`no_kupon` ,`tarikh` ,`amaun`,`kasyer`) VALUES ( '$_POST[no_kupon]',  '$_POST[tarikh]','$_POST[amaun]',$_POST[kasyer]');"); 

Open in new window


the single quote for 'amaun' is invalid. ' should be placed as `
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Nur

Author

Commented:
actually there is no error display for my coding but when i listed 3 attribute in my sql (no_kupon, tarikh,amaun).It was successfully added into database.

But, when I am trying to listed 4 attribute in my sql(no_kupon, tarikh,amaun,kasyer). It was unsuccessfully added into database.

I don't know what i am supposed to do since I had to listed only 3 attribute in sql to ensure the data inserted into database.
Ryan ChongSoftware Team Lead

Commented:
But, when I am trying to listed 4 attribute in my sql(no_kupon, tarikh,amaun,kasyer). It was unsuccessfully added into database.
as already explained and provided a sample in comment: ID: 42038757. Have you tried that?

if it's not working, tell us what error you getting here?
Nur

Author

Commented:
I have tried a sample provided in comment: ID: 42038757 but the the data still failed to save into database
$query  = "INSERT INTO  `teksi`.`resit` (`no_kupon` ,`tarikh` ,'amaun',`kasyer`) VALUES ( '$_POST[no_kupon]',  '$_POST[tarikh]','$_POST[amaun]',$_POST[kasyer]');"

echo $query; // THIS WILL PRINT THE QUERY

Open in new window



Once you see the query on browser, you can try running directly in mysql(if you are using phpmyadmin, you can run the query manually by pasting the sql statement into run).  

Also check if table has the  relevant column name.
Software Team Lead
Commented:
ooops, try this instead:

mysql_query("INSERT INTO  `teksi`.`resit` (`no_kupon` ,`tarikh` ,`amaun`,`kasyer`) VALUES ( '$_POST[no_kupon]',  '$_POST[tarikh]','$_POST[amaun]', '$_POST[kasyer]');"); 

Open in new window


your statement is without the opening single quote for its value.
Nur

Author

Commented:
ohh yess!! finally it can be save. I used a sample provided in comment ID: 42038867. thank you a lot to all of you for helping me
Most Valuable Expert 2011
Top Expert 2016

Commented:
First things first...  NEVER put external variables ($_POST) into a query string.  You must escape the variables or your script will get hacked and your database will get destroyed.

Next, there are examples in these comments that misuse quotation marks in ways that might cause the scripts to fail at run time.  Here is what you need to know about quotes.
https://www.experts-exchange.com/articles/12241/Quotation-Marks-in-PHP.html

Now on to the longer-term issues that call for refactoring...  You can't use MySQL any more.  PHP has removed support for the MySQL extension, and you will have to change your scripts to stop using MySQL.  There are two other extensions that will let you continue to use your database.  Here is the explanation with examples of how to keep your scripts running in the future.
https://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial