Link to home
Start Free TrialLog in
Avatar of Nur
Nur

asked on

Inserting data into database

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
Avatar of Peos John
Peos John
Flag of Malaysia image

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

 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?
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 `
Avatar of Nur
Nur

ASKER

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.
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?
Avatar of Nur

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nur

ASKER

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
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