Forum Moderators: coopster

Message Too Old, No Replies

Grabbing data from one table and inserting it into another

Sample code of how I'm trying to grab data from a table and insert it

         

abovemediocrity

8:29 pm on Dec 30, 2003 (gmt 0)

10+ Year Member



Hey guys, I'm new to PHP, but have devoloping Web sites for years now (I know I'm a late bloomer with PHP) but I'm trying to take some information from a table and insert it into another table, the problem is the variable names are different and this has been throwing me off, if anybody could throw some help my way, I'd be most grateful.

<?php

if( is_numeric( $_GET['from_message_id'] ) ){ //we're inserting from phone messages

//connect to db
mysql_select_db($database_message, $message);

$results = mysql_query( "SELECT (message_from) FROM messages WHERE id={$_GET['from_message_id']}" ); // get the message info we want to insert from $id=$_GET['id']; id={$_GET['from_message_id']}

$row = mysql_fetch_assoc($results);

mysql_query( "INSERT INTO clients (contact_name) VALUES (".GetSQLValueString($row['message_from'],"text").")" ); //run the insert with the fields you want from the result of the last query, just like is done everywhere else in these files

$_GET['id'] = mysql_query( "SELECT LAST_INSERT_ID()" ); //get the new row id and set it so client_edit knows what to edit
}
?>

Thanks a bunch in advance.

jatar_k

8:42 pm on Dec 30, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld abovemediocrity,

what exactly is going wrong with the insert? any error messages?

abovemediocrity

9:34 pm on Dec 30, 2003 (gmt 0)

10+ Year Member



The error I'm getting is:

You have an error in your SQL syntax near 'id #9' at line 1

The way this works is I have data from this one table displayed with a link under it to take it and insert it into another table and then displayed on the page to be readily updated.

jatar_k

9:55 pm on Dec 30, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try this to see what your query actually looks like

$q = "SELECT (message_from) FROM messages WHERE id=" . $_GET['from_message_id'];
echo $q;
$results = mysql_query($q);

take a look and see if there is a problem with the format of $_GET['from_message_id']

abovemediocrity

10:31 pm on Dec 30, 2003 (gmt 0)

10+ Year Member



The new message I get from it is:

SELECT (message_from) FROM messages WHERE id=478You have an error in your SQL syntax near 'id #9' at line 1

Could this have something to do with the connections I have it making above it, would that be line 1 or am I totally off here?

coopster

11:09 pm on Dec 30, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Is your id field of column type character, possibly CHAR or VARCHAR? Any columns that are of type character or date should be enclosed in quotation marks. Try this and see if you still get the error message:

$q = "SELECT (message_from) FROM messages WHERE id='" . $_GET['from_message_id'] . "'";
echo $q;
$results = mysql_query($q);

abovemediocrity

11:39 pm on Dec 30, 2003 (gmt 0)

10+ Year Member



It's an int cause it's the unique id for that table but still no go, getting that same error about:

You have an error in your SQL syntax near 'id #9' at line 1

This is starting to be irritating.

bluedevil

11:43 pm on Dec 30, 2003 (gmt 0)

10+ Year Member



Hi

try this before INSERT

$row['message_from'] = mysql_escape_string($row['message_from']);

coopster

11:46 pm on Dec 30, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



OK. What is the extra line after your query statement?

$results = mysql_query( "SELECT (message_from)
FROM messages WHERE id={$_GET['from_message_id']}" );
// get the message info we want to insert from $id=$_GET['id'];
id={$_GET['from_message_id']}

It seems to be hanging out there...

coopster

11:49 pm on Dec 30, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Also, get rid of the semicolon at the end of your comment right above that statement.

abovemediocrity

2:45 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



Sadly, I still receive the same error message, could be from something outside of this script? All that is above this script is the connection files.

coopster

5:53 pm on Dec 31, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yeah, I think you are right, I think we have been focusing on the wrong area. I didn't catch it before, but your error is stating an issue at line 1...

>>You have an error in your SQL syntax near 'id #9' at line 1

...and line 1 of this particular snippet of code looks just fine to me. I'm willing to bet it is your user-defined function that is causing the issue,

GetSQLValueString
. Have a look at that function.