473,513 Members | 2,399 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

syntax error, unexpected T_STRING on INSERT command

20 New Member
I have uploaded a file and am now trying to store it in a DB. I am getting a "Parse error: syntax error, unexpected T_STRING" error on this line below:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO gallery (user_id, image_name) 
  2.             VALUES ($RecordsetUserID, UPLOAD_DIR.$_SESSION['MM_Username'].'/'.$now.$_SESSION['MM_Username'].'-'.$file);
Can anyone help?
Mar 4 '08 #1
21 8511
hsriat
1,654 Recognized Expert Top Contributor
I have uploaded a file and am now trying to store it in a DB. I am getting a "Parse error: syntax error, unexpected T_STRING" error on this line below:

INSERT INTO gallery (user_id, image_name)
VALUES ($RecordsetUserID, UPLOAD_DIR.$_SESSION['MM_Username'].'/'.$now.$_SESSION['MM_Username'].'-'.$file);

Can anyone help?
Is it $UPLOAD_DIR ........?
Mar 4 '08 #2
Jordan79
20 New Member
Is it $UPLOAD_DIR ........?
This is my whole code:

[PHP]<?php require_once('Connections/PhotoABC.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$colname_Recordset1 = "-1";
if (isset($_SESSION['username'])) {
$colname_Recordset1 = $_SESSION['username'];
}
mysql_select_db($database_PhotoABC, $PhotoABC);
$query_Recordset1 = sprintf("SELECT user_id FROM `user` WHERE username = %s", GetSQLValueString($colname_Recordset1, "text"));
$Recordset1 = mysql_query($query_Recordset1, $PhotoABC) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

//define constant for the maximum upload
define ('MAX_FILE_SIZE', 100000);

if (array_key_exists('upload', $_POST)) {

//define constant for upload folder
define('UPLOAD_DIR', 'C:\htdocs\PhotoABC\upload_test\\');

//replace any spaces with underscore
//also assign to simple variable
$file = str_replace(' ', '_', $_FILES['image']['name']);

//convert the max size to KB
$max = number_format(MAX_FILE_SIZE/1024,1).'KB';

//create variable of correct file typr
$correct = array('image/jpeg','image/pjpeg');

// begin by saying file is unacceptable
$sizeOK = false;
$typeOK = false;

//check file is correct size
if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <=MAX_FILE_SIZE){
$sizeOK = true;
}

//check file is correct type
foreach ($correct as $type){
if($type == $_FILES['image']['type']){
$typeOK = true;
break;
}
}

if ($sizeOK && $typeOK){
switch($_FILES['image']['error']){
case 0:



//.$_SESSION['MM_Username']
if (!is_dir(UPLOAD_DIR.$_SESSION['MM_Username'])){
mkdir(UPLOAD_DIR.$_SESSION['MM_Username']);
}


//move file to the upload folder and rename it
//get date and time
ini_set('date.timezone', 'Europe/Dublin');
$now = date('Y-m-is-');

$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$_SESSION[ 'MM_Username'].'/'.$now.$_SESSION['MM_Username'].'-'.$file);

//copy image to database ????
INSERT INTO gallery (user_id, image_name)
VALUES ($RecordsetUserID,
UPLOAD_DIR.$_SESSION['MM_Username'].'/'.$now.$_SESSION['MM_Username'].'-'.$file);




if ($success){
$result= "$file uploaded successfully";
}

break;
}
}
elseif ($_FILES['image']['error'] == 4){
$result = 'No file selected';
}
else {
$result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: jpg";
}
}

mysql_free_result($Recordset1);
?>
[/PHP]

Maybe i am not using the SQL correctly for the insert query?
Mar 4 '08 #3
ronverdonk
4,258 Recognized Expert Specialist
You certainly are not using it correcly. A MySQL INSERT statement is ususally issued (in PHP) using the mysql_query() command.

So in your case it would be something like[php]$result=mysql_query("INSERT INTO gallery (user_id, image_name) VALUES ( .... etc )"
or die("Error in INSERT: ".mysql_error());[/php]Ronald
Mar 4 '08 #4
Jordan79
20 New Member
You certainly are not using it correcly. A MySQL INSERT statement is ususally issued (in PHP) using the mysql_query() command.

So in your case it would be something like[php]$result=mysql_query("INSERT INTO gallery (user_id, image_name) VALUES ( .... etc )"
or die("Error in INSERT: ".mysql_error());[/php]Ronald
Thanks for your reply.
I have tried this and still not having any luck.
I am just trying to save the name of the uploaded image into a database.
Any suggestions on how I might go about this?
Mar 4 '08 #5
ronverdonk
4,258 Recognized Expert Specialist
Show the INSERT statement as you have it now and that does not work. (I almost bet that you did not include the value of the second field within quotes.)

Ronald
Mar 4 '08 #6
Jordan79
20 New Member
[php]$sql = "SELECT 'user_id' FROM 'user' ";
$result = mysql_query($sql) or die (mysql_error());

$result=mysql_query("INSERT INTO gallery (user_id, image_name) VALUES ($result,$success)"
or die("Error in INSERT: ".mysql_error());[/php]

If u look at my code I am trying to create a recordset so that I can add the user_id to the database as well...and the name of my file is in the varible $success. u can see this around line 94 of my code.

Thanks for any help
Mar 4 '08 #7
Jordan79
20 New Member
Code as it is now

[PHP]<?php
//define constant for the maximum upload
define ('MAX_FILE_SIZE', 100000);

if (array_key_exists('upload', $_POST)) {

//define constant for upload folder
define('UPLOAD_DIR', 'C:\htdocs\PhotoABC\upload_test\\');

//replace any spaces with underscore
//also assign to simple variable
$file = str_replace(' ', '_', $_FILES['image']['name']);

//convert the max size to KB
$max = number_format(MAX_FILE_SIZE/1024,1).'KB';

//create variable of correct file typr
$correct = array('image/jpeg','image/pjpeg');

// begin by saying file is unacceptable
$sizeOK = false;
$typeOK = false;

//check file is correct size
if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <=MAX_FILE_SIZE){
$sizeOK = true;
}

//check file is correct type
foreach ($correct as $type){
if($type == $_FILES['image']['type']){
$typeOK = true;
break;
}
}

if ($sizeOK && $typeOK){
switch($_FILES['image']['error']){
case 0:



//.$_SESSION['MM_Username']
if (!is_dir(UPLOAD_DIR.$_SESSION['MM_Username'])){
mkdir(UPLOAD_DIR.$_SESSION['MM_Username']);
}


//move file to the upload folder and rename it
//get date and time
ini_set('date.timezone', 'Europe/Dublin');
$now = date('Y-m-is-');

$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$_SESSION[ 'MM_Username'].'/'.$now.$_SESSION['MM_Username'].'-'.$file);

//copy image to database ????
//$sql = "SELECT 'user_id' FROM 'user' ";
//$result = mysql_query($sql) or die (mysql_error());

//$result=mysql_query("INSERT INTO gallery (user_id, image_name) VALUES ($result,$success)"
//or die("Error in INSERT: ".mysql_error());


//$query = "INSERT INTO gallery (user_id,image_name) VALUES ($result,$success)";
//$queryresult = mysql_query($query) or die (mysql_error());




if ($success){
$result= "$file uploaded successfully";
}

break;
}
}
elseif ($_FILES['image']['error'] == 4){
$result = 'No file selected';
}
else {
$result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: jpg";
}
}
?>[/PHP]
Mar 4 '08 #8
ronverdonk
4,258 Recognized Expert Specialist
$result=mysql_query("INSERT INTO gallery (user_id, image_name) VALUES ($result,$success)"
This statement is nonsense. You take the result of the move_uploaded...() command (which is an error or an okay) and want to insert that as the name of the image file into your database.

Ronald
Mar 4 '08 #9
Jordan79
20 New Member
This statement is nonsense. You take the result of the move_uploaded...() command (which is an error or an okay) and want to insert that as the name of the image file into your database.

Ronald
yeah I am getting myself confused.
How then do I get the name of the file that was just uploaded and renamed. I am at a loss really...
Mar 4 '08 #10
ronverdonk
4,258 Recognized Expert Specialist
Save it in a variable like[php]$v=UPLOAD_DIR.$_SESSION['MM_Username'].'/'.$now.$_SESSION['MM_Username'].'-'.$file;[/php]
Then use that variable in your INSERT statement, like[php]$resins=mysql_query("INSERT INTO gallery (user_id, image_name) VALUES ($result,'$v')"
//or die("Error in INSERT: ".mysql_error());[/php]
Ronald
Mar 4 '08 #11
Jordan79
20 New Member
Save it in a variable like[php]$v=UPLOAD_DIR.$_SESSION['MM_Username'].'/'.$now.$_SESSION['MM_Username'].'-'.$file;[/php]
Then use that variable in your INSERT statement, like[php]$resins=mysql_query("INSERT INTO gallery (user_id, image_name) VALUES ($result,'$v')"
//or die("Error in INSERT: ".mysql_error());[/php]
Ronald
Thank u again for your help....
This is my code now.

[PHP]$v=UPLOAD_DIR.$_SESSION['MM_Username'].'/'.$now.$_SESSION['MM_Username'].'-'.$file;

//copy image to database
$resins=mysql_query("INSERT INTO gallery (user_id, image_name) VALUES ($Recordset1,'$v'")
or die("Error in INSERT: ".mysql_error());[/PHP]


What i have done is created a recordset with dreamweaver call Recordset1...this holds user_id from the user DB.
Can I use this $Recordset1 as my varible in my INSERT query or do I need to extract the information some how?
And I am getting an error :
"Error in INSERT: 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 'id #6,'C:\htdocs\PhotoABC\upload_test\Jordan/2008-03-2022-Jordan-CompeteHome_03.' at line 1"
Mar 4 '08 #12
ronverdonk
4,258 Recognized Expert Specialist
Looking at the INSERT part of the code (NOT the other part) it looks like you need something like the following to

(a) get the correct userid, repace the ??? in that statement with the correct WHERE user_id=xxxxx, (probably $_SESSION['MM_username']??

(b) Insert the name of the image into a row
[php]//copy image to database
$sql = "SELECT user_id FROM user ???????????????? ";
$result = mysql_query($sql)
or die (mysql_error());
if (mysql_num_rows($result) > 0) {
$row=mysql_fetch_assoc($result);
$userid=$row['user_id'];
$v=UPLOAD_DIR.$_SESSION['MM_Username'].'/'.$now.$_SESSION['MM_Username'].'-'.$file;
$resins=mysql_query("INSERT INTO gallery (user_id, image_name) VALUES($userid,'$v'")
or die("Error in INSERT: ".mysql_error());
}[/php]Ronald
Mar 4 '08 #13
Jordan79
20 New Member
[PHP]$v=UPLOAD_DIR.$_SESSION['MM_Username'].'/'.$now.$_SESSION['MM_Username'].'-'.$file;

//copy image to database

$sql = "SELECT user_id FROM user WHERE username = $_SESSION['MM_username']";
$queryresult = mysql_query($sql)
or die (mysql_error());
if (mysql_num_rows($queryresult) > 0) {
$row=mysql_fetch_assoc($queryresult);
$userid=$row['user_id'];

$resins=mysql_query("INSERT INTO gallery (user_id, image_name) VALUES ($userid,'$v'")
or die("Error in INSERT: ".mysql_error());
[/PHP]

Getting an error on line 5.
I think i am just missing some ' '.
Does this code look more like what I need now?
Mar 4 '08 #14
ronverdonk
4,258 Recognized Expert Specialist
User_id is (most probably) a character type field, so you must enclose any value to be used with that field, between quotation marks, like[[php]$sql = "SELECT user_id FROM user WHERE user_id = '".$_SESSION['MM_username']."'";[/php]If it is a char field, then the same goes for line 12 statement.

Ronald
Mar 4 '08 #15
Jordan79
20 New Member
User_id is (most probably) a character type field, so you must enclose any value to be used with that field, between quotation marks, like[[php]$sql = "SELECT user_id FROM user WHERE user_id = '".$_SESSION['MM_username']."'";[/php]If it is a char field, then the same goes for line 12 statement.

Ronald
Yeah that was it.

[PHP]$v=UPLOAD_DIR.$_SESSION['MM_Username'].'/'.$now.$_SESSION['MM_Username'].'-'.$file;

//copy image to database

$sql ="SELECT user_id FROM user WHERE username = '".$_SESSION['MM_Username']."'";
$queryresult = mysql_query($sql)
or die (mysql_error());
if (mysql_num_rows($queryresult) > 0) {
$row=mysql_fetch_assoc($queryresult);
$userid=$row['user_id'];
}

$resins=mysql_query("INSERT INTO gallery (user_id, image_name) VALUES ($userid,'$v'")
or die("Error in INSERT: ".mysql_error());[/PHP]

When I upload a file and it tries to send it to the database I get this error message:

"Error in INSERT: 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 '' at line 1"
Mar 4 '08 #16
ronverdonk
4,258 Recognized Expert Specialist
Please read my posts entirely
If it is a char field, then the same goes for line 12 statement.
So line 13 (was line 12 previously) must have the user_id within quotes just like the other user_id fields:[php]VALUES ('$userid','$v'")[/php]Ronald
Mar 4 '08 #17
Jordan79
20 New Member
Please read my posts entirely
So line 13 (was line 12 previously) must have the user_id within quotes just like the other user_id fields:[php]VALUES ('$userid','$v'")[/php]Ronald
Sorry.
I have been using this line:
[PHP]$sql ="SELECT user_id FROM user WHERE username = '".$_SESSION['MM_Username']."'";[/PHP]

where I get user_id from the user DB WHERE username is equal to the the session username.
I understood that I was using this query to return user_id for the username logged in?

Sorry if I picked u up wrong.
That is why I didnt put the quotes around line 12 because user_id is an INT.
Mar 4 '08 #18
ronverdonk
4,258 Recognized Expert Specialist
Statement should then be
Expand|Select|Wrap|Line Numbers
  1. $sql ="SELECT user_id FROM user WHERE user_id = ".$_SESSION['MM_Username'];
Ronald
Mar 4 '08 #19
Jordan79
20 New Member
I have been working on my code.
New Code

[PHP]$v=UPLOAD_DIR.$_SESSION['MM_Username'].'/'.$now.$_SESSION['MM_Username'].'-'.$file;

//copy image to database

$sql = "SELECT `user_id` FROM `user` WHERE `username` = '".$_SESSION['MM_Username']."'";
$queryresult = mysql_query($sql)
or die (mysql_error());
if (mysql_num_rows($queryresult) > 0) {
$row=mysql_fetch_assoc($queryresult);
$userid=$row['user_id'];
}

$resins=mysql_query("INSERT INTO gallery ('user_id', 'image_name') VALUES ($userid,'$v')")
or die("Error in INSERT: ".mysql_error());[/PHP]

I get this error
"Error in INSERT: 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 ''user_id', 'image_name') VALUES (5,'C:\htdocs\PhotoABC\upload_test\Jordan/2008-0' at line 1"

As u can see the VALUES entered are correct.
Just have an SQL syntax error and I do not know where.
Mar 4 '08 #20
Jordan79
20 New Member
This is fixed and working now
Thanks for all your help

Here is the working code

[PHP]$sql = "SELECT `user_id` FROM `user` WHERE `username` = '".$_SESSION['MM_Username']."'";
$queryresult = mysql_query($sql)
or die (mysql_error());
if (mysql_num_rows($queryresult) > 0) {
$row=mysql_fetch_assoc($queryresult);
$userid=$row['user_id'];
}

$resins=mysql_query("INSERT INTO gallery (`user_id`, `image_name`) VALUES ('$userid','$v')")
or die("Error in INSERT: ".mysql_error());
[/PHP]

Thanks again..it wouldnt be working with out u :)
Mar 4 '08 #21
ronverdonk
4,258 Recognized Expert Specialist
Good that it works now.

Remember, you cannot use quotation marks ina MySQL statement for enclosing table, column or db names. You have to use 'back ticks' for that. And you found that out.

See you again some time.

Ronald
Mar 4 '08 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2873
by: Salim | last post by:
Hi people, keep getting this errorParse error: parse error, unexpected T_STRING in order_fns.php line 91. the code is below for the file and I've indicated line 91 <?php function...
5
13134
by: Anna MZ | last post by:
I am new to php and have written the following mysql code to enter the details of a new user in the admin subdomain of my website: $sql = "INSERT INTO 'users' ('userid', 'username', 'upassword')...
4
22129
kestrel
by: kestrel | last post by:
I have some html code that is supposed to be displayed by php echo. But for some reason i keep getting a syntax error, and i cant figure out what is going on. Heres what i have <?php...
36
7947
by: rhys | last post by:
My Gurus and Angels -- Please pardon this old-school programmer, only recently enlightened to open-source, having been trapped in the convenience of proprietary lingos for way too long. My...
6
2606
by: goodguyjam | last post by:
Hi all, I'm having trouble with mysql. I've just finished my php coding for HTTP authentication and with some help am now getting a login window pop up whenever I click on a link on my website...
3
2385
by: sclarkstone | last post by:
Im getting this error; Parse error: syntax error, unexpected T_STRING, expecting ':' or ';' with this line; header ('postcodesearch.php?e=nw&pcode=',); I cant find whats wrong, can anyone...
3
5584
paulrajj
by: paulrajj | last post by:
hi to all, i am getting syntax error on my code.. Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in D:\xampp\htdocs\Dummy\paulraj\matrim\exam.php on line 62 ...
10
5625
by: benicio | last post by:
Parse error: syntax error, unexpected T_STRING, expecting '(' in C:\wamp\www\study_group\includes\functions.php on line 19 I got this error and this syntax is from 8 to 19th line. <?php ...
14
5485
riverdale1567
by: riverdale1567 | last post by:
Hi I am a newbie trying to get some of my first code working, yada yada yada. I have a drop down box which chooses a state then takes the post data to 'processform2.php' to use that to pull up...
0
7265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7388
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7539
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5692
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4751
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3228
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
461
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.