1

I have tried a bunch of soultions on here an other forums but always get errors. Here is current PHP code using hostgator and PHP 5.2

$insert = "INSERT INTO events (datecreated, fullname, email, title, url, picurl, desc, psw, skypeid, event) SELECT jvs.datecreated, jvs.fullname, jvs.email, jvs.title, jvs.url, jvs.picurl, jvs.desc, jvs.psw, jvs.skypeid, jvs.event FROM jvs";
if (! mysqli_query($dbh, $insert)  ||  mysqli_affected_rows($dbh) == 0)
            {
die ("<BR><BR><BR>Failed to insert data- " . mysqli_error($dbh));
            }

Error I get is:

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 'desc, psw, skypeid, event) SELECT jvs.datecreated, jvs.fullname, jvs.email, jvs.' at line 1

I have checked every field in both tables and the spelling is correct and the fileds are there. The fileds are not in the same order in events table and that table has many more fields. Thanks, Ed

1

2 Answers 2

3

DESC is a MySQL Reserved keyword. You should escape with backtick, eg

INSERT INTO tableName(......, `desc`, .....)
SELECT ...
Sign up to request clarification or add additional context in comments.

3 Comments

Thought to myself who is JW, with 46k that I've never seen before? But you're Kuya John/John Woo. :)
@MichaelBerkowski it's been a habit to change my name and pass every 30 days. I don't know why. Maybe I'm sick. haha!
Thank you. That worked perfectly. Glad I asked here or I would have spent many more hours with this.
0

1. See at desc, desc is a reserved word and readed as statement and you must add backtick to it.

2. You must add seperator between two query (INSERT INTO and SELECT)

3. And where is your VALUES, INSERT INTO must include a VALUES

And the fix code is:

$insert = "INSERT INTO events (`datecreated`, `fullname`, `email`, `title`, `url`, `picurl`, `desc`, `psw`, `skypeid`, `event`) VALUES('datecreated', 'fullname', 'email', 'title', 'url', 'picurl', 'desc', 'psw', 'skypeid', 'event');
SELECT jvs.datecreated, jvs.fullname, jvs.email, jvs.title, jvs.url, jvs.picurl, jvs.desc, jvs.psw, jvs.skypeid, jvs.event FROM jvs";
if (! mysqli_query($dbh, $insert)  ||  mysqli_affected_rows($dbh) == 0){
   die ("<BR><BR><BR>Failed to insert data- " . mysqli_error($dbh));
}

3 Comments

This is inserting from one table to another so values are not used. Looks like you backticked everything. I guess that would be a good idea to be safe. Just putting the backticks around desc solved everything. Thanks.
I tried to mark yours as accepted but it wouldn't without unchecking the other answer. He answered first so I gave the accept to him.
i'm sorry ..., i don't know :)

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.