0

New to coding . tried to create a database that stores details for an advertisement . the php works fine till the end . but the output always shows not inserted . could someone please help me out ?? the form and php are given here .

<html>
<body>

<form action="basic.php" method="post">

Name: <input type="text" name="adtitle"></input><br>
Name: <input type="text" name="sellersname"></input><br>
Name: <input type="text" name="email"></input><br>
Name: <input type="text" name="description"></input><br>
Name: <input type="text" name="noo"></input><br>
Name: <input type="text" name="base"></input><br>
Name: <input type="text" name="cutoff"></input><br>

<input type="submit"></input>
</form>

</body>
</html>
<?php
$serverName = "127.0.0.1";
$userName = "root";
$password = "";
$database = "demo";

$con = mysqli_connect($serverName, $userName, $password, $database);
if(!$con)
{echo 'not connected';}
if(!mysqli_select_db($con,'demo'))
{echo'db not selected';
}


$Title = $_POST['adtitle'];
$Sname= $_POST['sellersname'];
$email = $_POST['email'];
$Description = $_POST['description'];
$Mobileno = $_POST['noo'];
$Base = $_POST['base'];
$Cutoff = $_POST['cutoff'];

$sql = "INSERT INTO advert (Title,Seller name,email,Description,Number,Base,Cutoff)VALUES('$Title','$Sname','$email','$Description','$Mobileno','$Base','$Cutoff')";
if(!mysqli_query($con,$sql))
{echo'not inserted';}
else {echo'innserted';}

?>

1
  • Instead of echo'not inserted'; check for errors, php.net/manual/en/mysqli.error.php. You also are open to SQL injections with this code which code be the cause of your current issue. Commented May 24, 2016 at 19:47

1 Answer 1

2

You have a field, seller name, which has a whitespace in it.

You must quote it in backticks:

Change it from:

"INSERT INTO advert (Title,Seller name,email,Description,Number,Base,Cutoff) VALUES ('$Title','$Sname','$email','$Description','$Mobileno','$Base','$Cutoff')"

to:

"INSERT INTO advert (Title,`Seller name`,email,Description,Number,Base,Cutoff)VALUES('$Title','$Sname','$email','$Description','$Mobileno','$Base','$Cutoff')"
Sign up to request clarification or add additional context in comments.

Comments

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.