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

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Peos John

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 Chong

 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 Chong

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 `
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
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.
Ryan Chong

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

ASKER
I have tried a sample provided in comment: ID: 42038757 but the the data still failed to save into database
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Peos John

$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
Ryan Chong

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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
Ray Paseur

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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes