Jump to content

[SOLVED] INSERT INTO help please


Gem

Recommended Posts

Hi all, I'm new to this forum so be nice lol :)

 

I'm also very new to the whole MySQL and PHP thing, and its only taken me 3 days to connect to my webserver, so things are looking good.

 

Anyway, I'm finally in ... and I'm experimenting with INSERT. I got this code from a tutorial, added my details and kind of hoped it would work, but it doesn't.

 

The connection is fine, it's not that ...

 

The DB is called "bssql", the table is called "name" and has 2 columns, "firstname" and "lastname" CHAR(20).

 

I'm using the following form:

 

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

Firstname: <input type="text" name="firstname" />

Lastname: <input type="text" name="lastname" />

<input type="submit" />

 

and the following for "insert.php"

 

<?php

$con = mysql_connect("host","user","password");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }mysql_select_db("bssql", $con);$sql="INSERT INTO name (firstname, lastname,)

VALUES

('$_POST[firstname]','$_POST[lastname]',)";if (!mysql_query($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

echo "1 record added";mysql_close($con)

?>

 

and I get this responce ...

 

"Error: 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 ') VALUES ('Roger','Wilson',)' at line 1"  

 

Roger Wilson is the name I was trying to insert into the table.

 

I hope this is enough information for some to help me ...

 

I'd really appreciate it if someone could point out where i'm going wrong please ...

 

Many Thanks, and Kind Regards

 

Gem

Link to comment
Share on other sites

Hi all, I'm new to this forum so be nice lol :)

 

I'm also very new to the whole MySQL and PHP thing, and its only taken me 3 days to connect to my webserver, so things are looking good.

 

Anyway, I'm finally in ... and I'm experimenting with INSERT. I got this code from a tutorial, added my details and kind of hoped it would work, but it doesn't.

 

The connection is fine, it's not that ...

 

The DB is called "bssql", the table is called "name" and has 2 columns, "firstname" and "lastname" CHAR(20).

 

I'm using the following form:

 

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

Firstname: <input type="text" name="firstname" />

Lastname: <input type="text" name="lastname" />

<input type="submit" />

 

and the following for "insert.php"

 

<?php

$con = mysql_connect("host","user","password");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }mysql_select_db("bssql", $con);$sql="INSERT INTO name (firstname, lastname,)

VALUES

('$_POST[firstname]','$_POST[lastname]',)";if (!mysql_query($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

echo "1 record added";mysql_close($con)

?>

 

and I get this responce ...

 

"Error: 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 ') VALUES ('Roger','Wilson',)' at line 1"  

 

Roger Wilson is the name I was trying to insert into the table.

 

I hope this is enough information for some to help me ...

 

I'd really appreciate it if someone could point out where i'm going wrong please ...

 

Many Thanks, and Kind Regards

 

Gem

put a space in between your values

values ("value1", "value2", "value3",);

not ("value1","value2","value3");

Link to comment
Share on other sites

change

$sql="INSERT INTO name (firstname, lastname,) VALUES ('$_POST[firstname]','$_POST[lastname]',)";

to

$sql="INSERT INTO name (firstname, lastname) VALUES ('$_POST[firstname]','$_POST[lastname]')";

you have extra commas

Link to comment
Share on other sites

OK - They both made sense, but unfortunatly that didn't do the trick ... I got all excited and everything then LOL

 

hmmm... any other idea's?

 

Thanks for your help, most appreciated x

Link to comment
Share on other sites

Error: 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 ') VALUES ('roger', 'wilson')' at line 1

 

Same error but with a space between the 2 values ...

 

Is it possible there is a setting in the table on the server or the database that isnt right or something??

x

 

Link to comment
Share on other sites

Hi

 

Change the line:-

 

die('Error: ' . mysql_error());

 

to:-

 

die('Error: ' . " $sql " . mysql_error());

 

and that should display the full sql. Hopefully we can then see the problem.

 

Sql generally should not care about using a space after the comma.

 

All the best

 

Keith

Link to comment
Share on other sites

Error: INSERT INTO name (firstname, lastname,) VALUES ('roger', 'wilson') 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 ') VALUES ('roger', 'wilson')' at line 1

 

New error message ...

 

Still doesnt mean anything to me though  :-[

Link to comment
Share on other sites

  <?php
$con = mysql_connect("host","user","pw");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("bssql", $con);$sql="INSERT INTO name (firstname, lastname,)
VALUES
('$_POST[firstname]', '$_POST[lastname])'";if (!mysql_query($sql,$con))
  {
  die('Error: ' . " $sql " . mysql_error());
  }
echo "1 record added";mysql_close($con)
?>

 

Hey Keith - thanks for your help mate ...

 

Which comma is it that shouldnt be there? Please...

 

XXX

Link to comment
Share on other sites

Thanks mate - thats done it!

 

Now to try making information go into 2 tables ... fun!

 

Who'd of thought one little comma would cause 2 days worth of headaches!LOL

 

Thanks again xxx

Link to comment
Share on other sites

Hi

 

No problem. Can I suggest that you format your code a bit more. Doesn't really make any difference to how it runs, but does make it easier for you to debug it. For example, put each statement on a new line and indent inside if statements, loops, etc.

 

Eg:-

 

<?php
$con = mysql_connect("host","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("bssql", $con);
$sql="INSERT INTO name (firstname, lastname) VALUES ('$_POST[firstname]','$_POST[lastname]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
?>

 

(although everyone has their own style of formatting, keeping it fairly consistant for yourself makes life easier).

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.