1

I am getting an error message when I try to insert values in a sql table via php coding. Even I also create a new database again but it's not working. So I need your help to solve it. My error message is given below:

Insertion failed! You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near'Usage,Validity,Price)values('indranil','som','WWBIPL_NE_4GB','512kbps','4GB','3' at line 1

My html code is given below :

<html>
<head>
<body>
<form action="insert.php" method="post">
<select name="package">
<option value="WWBIPL_NE_4GB">WWBIPL_NE_4GB</option>
<option value="WWBIPL_NE_6GB">WWBIPL_NE_6GB</option>
</select>
Firstname:<input type="text" name="firstname">
Lastname:<input type="text" name="secondname">
Email:<input type="text" name="email">
Address:<input type="text" name="address">
<input type="submit">
</form>
</body>
</head>
</html>

Here is my PHP code

<?php
$fstname=$_POST["firstname"];
$sndname=$_POST["secondname"];
$price=$_POST["package"];
if($price=="WWBIPL_NE_4GB")
{
$bandwidth="512kbps";
$usage="4GB";
$validity="30days";
$price1="499";
}
else if($price=="WWBIPL_NE_6GB")
{
$bandwidth="512kbps";
$usage="6GB";
$validity="90days";
$price1="1280";
}
$servername="localhost";
$username="root";
$password="";
$dbname="stock";
$conn=mysqli_connect($servername,$username,$password,$dbname);
if(!$conn)
{
die("Connection Failed".mysqli_error($conn));
}   $sql="insertintoisuue(Firstname,Lastname,Package,Bandwidth,Usage,Validity,Price)values('$fstname','$sndname','$price','$bandwidth','$usage','$validity','$price1')";
if(mysqli_query($conn,$sql))
{
echo"Data inserted successfully";
}
else
{
echo"Insertion failed".mysqli_error($conn);
}
mysqli_close($conn);
?>
2
  • 1
    AFAIK, insertintoisuue would work better written this way: insert into isuue. Apart from that, your SQL is wide open to injections. Commented Mar 17, 2015 at 10:07
  • I already did.. It is a printing mistake Commented Mar 17, 2015 at 11:08

2 Answers 2

3

Usage is a reserved word in mysql . You havr to escape it with backticks or better rename your column

$sql="insert into isuue(Firstname,Lastname,Package,Bandwidth,`Usage`,Validity,Price)values('$fstname','$sndname','$price','$bandwidth','$usage','$validity','$price1')";

Alo you should use prepared statements and of cause as X.L.Ant mentioned you should write insert into ... with blanks.

Sign up to request clarification or add additional context in comments.

1 Comment

sory it was printing mistake..I already wrote insert into isuue(Firstname,Lastname,Package,Bandwidth,Usage,Validity,Price)values('$fstname','$sndname','$price','$bandwidth','$usage','$validity','$price1')"; with spaces
0

Replace:

$sql="insertintoisuue(Firstname,Lastname,Package,Bandwidth,Usage,Validity,Price)values('$fstname','$sndname','$price','$bandwidth','$usage','$validity','$price1')";

with:

$sql="INSERT INTO isuue (Firstname, Lastname, Package, Bandwidth, Usage,
          Validity, Price ) 
      VALUES ('$fstname', '$sndname',' $price','$bandwidth',  
          $usage','$validity','$price1')";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.