Author Topic: [SOLVED] MySQL insert problem  (Read 425 times)

0 Members and 1 Guest are viewing this topic.

Offline gsTopic starter

  • Irregular
  • Posts: 11
    • View Profile
[SOLVED] MySQL insert problem
« on: August 16, 2008, 11:13:02 PM »
Hi, I really need some help with this, Ive been trying to figure it out for such a long time!

Ok, I want this to connect to the database, and then insert info into the table, which I already have created. Here is the exact script:

<?php

$username = $_COOKIE['ID_my_site'];
$msg = $_POST['message'];
$to = $_POST['to'];


mysql_connect("freshsql.com","username","password")or die("SQL Error".mysql_error());;
mysql_select_db("data1")or die("SQL Error".mysql_error());;

$query="INSERT INTO messages(username,from,message) VALUES('$to',$username','$msg')";
mysql_query($query)or die("SQL Error".mysql_error());


?>

It keeps returning an error:

"SQL ErrorYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''messages'(username,from,message) VALUES('gs',gs','gsaafd')' at line 1"

Please help! I cannot figure this out, Thanks
-GS


EDIT:
Use meaningful topic subject names and post in the correct forum.
« Last Edit: August 16, 2008, 11:24:56 PM by toplay »

Offline toplay

  • Staff Alumni
  • Freak!
  • *
  • Posts: 6,272
    • View Profile
Re: MySQL insert problem
« Reply #1 on: August 16, 2008, 11:26:09 PM »
Change this:

$query="INSERT INTO messages(username,from,message) VALUES('$to',$username','$msg')";


To this:

$query="INSERT INTO messages(`username`,`from`,`message`) VALUES('$to','$username','$msg')";

Offline gsTopic starter

  • Irregular
  • Posts: 11
    • View Profile
Re: MySQL insert problem
« Reply #2 on: August 16, 2008, 11:31:19 PM »
Yes!!! It worked! Thank you VERY much! I have been trying to figure this out for SOOO LONG! Thanks very much!