CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   MySQL (http://www.codingforums.com/forumdisplay.php?f=7)
-   -   MYSQL error (confused) (http://www.codingforums.com/showthread.php?t=258646)

loopsnhoops 04-25-2012 07:46 PM

MYSQL error (confused)
 
Hi,

I am trying to create an upload file system to my mysql database and I keep getting the same error message. Can you take a quick look and help please?

Thanks, Eric

HTML FORM

Code:

<form action="show.php" method="post" enctype="multipart/form-data">
       
        IB2:
        <input type=radio name="grade" value="IB2" />
        <br />
        IB1:
        <input type=radio name="grade" value="IB1" />
        <br />
        FY:
        <input type=radio name="grade" value="FY" />
        <br />
        Y2:
        <input type=radio name="grade" value="Y2" />
        <br />
        Y1:
        <input type=radio name="grade" value="Y1" />
        <br />
        <input type=file method=post enctype=multipart/form-data />
        <input type=submit name="submitb" value="Upload File" />
       
</form>

PHP CODE (show.php)

PHP Code:

<?php

$TheGrade 
$_POST['grade'];
$name $_FILES['file']['name'];
$type $_FILES['file']['type'];
$size $_FILES['file']['size'];
            
$Server="xxxx";
$User="xxxx";
$Password="xxxx";
$Database="db1083186_Files";

if(
$TheGrade==IB2){

$gradetable="IB2FILES";
    
} elseif(
$TheGrade==IB1){

$gradetable="IB1FILES";
    
} elseif(
$TheGrade==FY){

$gradetable="FYFILES";
    
} elseif(
$TheGrade==Y2){

$gradetable="Y2FILES";
    
} elseif(
$TheGrade==Y1){

$gradetable="Y1FILES";
    
} else {
    
    echo 
"Connection error: No grade submission";
    
$gradetable=null;
    echo 
$gradetable;
    
}

$connection mysql_connect($Server$User$Password);
if(!
$connection){
    die(
"Couldn't Connect" mysql_error());
}
echo 
"Connection Working";
echo 
$TheGrade;    
echo 
$name;    
echo 
$type;    
echo 
$size;    

mysql_select_db($Database$connection);

$query "INSERT INTO " $gradetable " (name,type,size) VALUES (" $name "," $type "," $size ")";

$table mysql_query($query,$connection) or die(mysql_error());  



// echo "<table border='1'>";
// echo "<tr> <th>id</th> <th>name</th> </tr>";
// // keeps getting the next row until there are no more to get
// while($row = mysql_fetch_array( $query )) {
    // // Print out the contents of each row into a table
    // echo "<tr><td>"; 
    // echo $row['id'];
    // echo "</td><td>"; 
    // echo $row['name'];
    // echo "</td></tr>"; 
// }  

// echo "</table>";
            
?>

ERROR MESSAGE:

Connection WorkingY1 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

Fumigator 04-25-2012 08:04 PM

The problem is how you are building your INSERT query. Each value needs to be enclosed in single quotes (except columns that are integers, doubles, floats and decimals), and because you are not including these quotes in the string, the last value of " " is causing a syntax error with your query. Your query looks like this:

Code:

INSERT INTO tablename (name,type,size) VALUES (Y,1,)
It needs to look like this:

Code:

INSERT INTO tablename (name,type,size) VALUES ('Y','1',' ')
Whenever you run into a syntax error like this, echo out the _actual_ query (the string where you've put all the variables in) and the error will most likely jump right out at you.

PHP Code:

$query "INSERT INTO " $gradetable " (name,type,size) VALUES (" $name "," $type "," $size ")";

$table mysql_query($query,$connection);
if (!
$table)
{
    die(
"SQL Error! Query is $query<br />Error is ".mysql_error());



loopsnhoops 04-25-2012 09:59 PM

Hi,

Thanks for the reply and advice so far. I did what you said and I got the same error unfortunately.

Here it is:

Connection Working IB1 SQL Error! Query is INSERT INTO IB1FILES (name,type,size) VALUES (,,)
Error 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 ',)' at line 1



I think the problem is that the code isn't filling the file variables with information. Is this a reasonable thought?

Old Pedant 04-25-2012 11:24 PM

A reasonable thought. What did your debug code show?
Code:

echo "Connection Working";
echo $TheGrade;   
echo $name;   
echo $type;   
echo $size;

???

Incidentally, that would be really hard to figure out what is/isn't working, as written.

Better:
Code:

echo "Connection Working<br/>";
echo "grade: $TheGrade <br/>";   
echo "name: $name <br/>"
echo "type: $type <br/>"
echo "size: $size <hr/>"

And you *REALLY* should follow the advice Fumigator gave you.

To wit: If the name and/or type fields in your DB is a string type (e.g., VARCHAR or CHAR) then you *MUST* have apostrophes around their values.
Code:

$query = "INSERT INTO " . $gradetable . " (name,type,size) "
      . " VALUES ('" . $name . "','" . $type . "'," . $size . ")";


loopsnhoops 04-26-2012 12:54 AM

Yay, you guys helped me get the SQL error out of the way. The problem of the missing data in the variables $name, $type and $size remains though. When I use the echo function on them nothing appears leaving me to believe the transfer of information from my html form to the php script was void.

Sorry for continuing the thread but if you help me with just this one problem I will award thanks virtually and in reality.

loopsnhoops 04-26-2012 01:07 AM

MUAHAHAHHAHAHA THANKS FOR THE HELP BUT I FINALL FIXED IT!!! retard mistake :S I didn't write
Code:

name="file"
in my input file form



thanks for all the help guys!


All times are GMT +1. The time now is 08:52 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.