Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Exclamation 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

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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());

    Last edited by Fumigator; 04-25-2012 at 08:06 PM.

  • Users who have thanked Fumigator for this post:

    loopsnhoops (04-26-2012)

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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?
    Last edited by loopsnhoops; 04-25-2012 at 10:01 PM. Reason: Thoughts

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,211
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    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 . ")";
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    loopsnhoops (04-26-2012)

  • #5
    New Coder
    Join Date
    Aug 2011
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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.

  • #6
    New Coder
    Join Date
    Aug 2011
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •