Hi all I'm trying to learn php by myself I got as far as creating my first data base and table using the following code:

$con = mysql_connect("localhost","root","");
if (!$con)
die('Could not conect: '.mysql_error());
//Create database
if (mysql_query("CREATE DATABASE FATBYTES",$con))
echo "Database created";
echo "Error creating database ;".mysql_error();
$sql = "CREATE TABLE Customers
MobileNumber varchar(10),
Sex varchar(8),
FirstName varchar(15),
LastName varchar(15),
Age int,
Email varchar(30)
//Execute Query

The above works as I can access the database and table through phpMyadmin however the following insert does not work.I get this error"You have a mySQL syntax error...and so on"


$con = mysql_connect("localhost","root","");
if (!$con)
die('Could not conect: '.mysql_error());


//Execute Query

$query = "INSERT INTO Customers (MobileNumber,Sex,FirstName,LastName,Age,Email) VALUES ('$cellno','$Title'','$FirstName','$SurName',8,'$Email')";

{print("Couldn't make it happen<br/>");


Any help highly appreciated.

Recommended Answers

All 5 Replies

in sql query quotes missing in age insertion

this is should work

$query = "INSERT INTO Customers (MobileNumber,Sex,FirstName,LastName,Age,Email) VALUES ('$cellno','$Title'','$FirstName','$SurName','8','$Email')";

Member Avatar

An alternative syntax would be to use SET. I'm forever losing myself in VALUES lists - so you could do this:

$query = "INSERT INTO Customers SET MobileNumber = '$cellno' ,Sex = '$Title',FirstName = '$FirstName',LastName = '$SurName',Age = 8,Email ='$Email'";

is sex = '$Title' right?

@ardav thanks it worked so where was I going wrong?

You were going wrong here:


You accidentally wrote two single quotes side-by-side (right after $Title), which triggered a syntax error in your SQL statement. So there very well may have never been anything wrong with your use at all; it was just a small honest oversight - happens to all of us!

Keidi, welcome to PHP.

At the start of your script, you can simplify it a bit by changing

$con = mysql_connect("localhost","root","");
if (!$con)
die('Could not conect: '.mysql_error());


$con = mysql_connect("localhost","root","") or die("Could not conect: ".mysql_error());

Here's a further modification that I use to make my error messages display the line where the error occurred. Knowing the line has saved me a lot of grief.

$con = mysql_connect("localhost","root","") or die(__LINE__.": Could not conect: ".mysql_error());

Now here's a little bit mor unsolicited advice, useful if most or all of your work is done in one database.

I always create an include file, in a folder such as /includes/, that has my connect string in it, i.e.:


  $dbh=mysql_connect ("localhost", "username", "password") 
      or die ('I cannot connect to the database because: ' . mysql_error());
  mysql_select_db ("database") or die('Could not select database: '.mysql_error()); 

Then in the header of each page I simply put an include statement to include that in each page.


This way, my code is shorter and if my database connect string ever changes, i.e. I change my password, I only have one place to correct it.

By the way, if you haven't discovered includes yet, you'll want to early on. Includes are a way of saying "Take the contents of this file and stick it right here.". It's a bit like an automated cut and paste.

I always put the structure of the page (the <body> tag and everything before it as well as the menu structure, and then the </body> tag and everything after it) into include files called page-head.inc.php and page-foot.inc.php. The database connection include I put inside the page head include. This greatly simplifies the work.

Hope this is useful!


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.