Jump to content

[SOLVED] It's not inserting filename in database.


bobahad

Recommended Posts

Does anyone know why ? It prints the query fine and it looks fine.

 

$query2="INSERT INTO $Table (Filename) VALUES ('$uploadfile')";

mysql_query($query2);

print($query2);

 

It will say Insert into cars (Filename) VALUES ('picture.jpg')

But when I go to the table column "Filename", all the fields are empty...

 

Thanks  :)

Try this on your page, and tell me he error you are getting.

 

<HTML>
<HEAD>
  <TITLE> Add a car </TITLE>
</HEAD>

<BODY>
<h1>Add a Car</h1>
<FORM NAME="addcarform" enctype="multipart/form-data" METHOD="Post" ACTION="">

<TABLE>
<TR>
        <TD><B>Type:</B></TD>
        <TD><INPUT SIZE="6" TYPE="text" NAME="type" VALUE=""></TD>
</TR>
<TR>
        <TD><B>Make:</B></TD>
        <TD><INPUT TYPE="text" NAME="make" VALUE=""><br></TD>
</TR>
<TR>
        <TD><B>Model:</B></TD>
        <TD><INPUT TYPE="text" NAME="model" VALUE=""></TD>
</TR>
<TR>
        <TD><B>Year:</B></TD>
        <TD><INPUT SIZE="4" TYPE="text" NAME="year" VALUE=""></TD>
</TR>
<TR>
        <TD><B>V.I.N #:</B></TD>
        <TD><INPUT TYPE="text" NAME="vin" VALUE=""></TD>
</TR>
<TR>
        <TD><B>Color:</B></TD>
        <TD><INPUT SIZE="10" TYPE="text" NAME="color" VALUE=""></TD>
</TR>
<TR>
        <TD><B>Cost:</B></TD>
        <TD><INPUT SIZE="5" TYPE="text" NAME="cost" VALUE=""></TD>
</TR>
<TR>
        <TD><B>Selling Price:</B></TD>
        <TD><INPUT SIZE="5" TYPE="text" NAME="sellingprice" VALUE=""></TD>
</TR>
<TR>
        <TD><B>Date Bought:</B></TD>
        <TD><INPUT SIZE="20" TYPE="text" NAME="datebought" VALUE=""></TD>
</TR>
<TR>
        <TD><B>Link to Pictures:</B></TD>
        <TD><INPUT SIZE="33" TYPE="text" NAME="pictureslink" VALUE=""></TD>
</TR>
<TR>
        
        <TD><input type="hidden" name="MAX_FILE_SIZE" value="500000" /><b>Upload Main Picture:</b></TD>
        <TD><input  name="filename" type="file" /></TD>
<TR/>
  <TR>
        <TD><INPUT TYPE="submit" name="add" Value="Add Car"></TD>
</TR>
<TR>
<TD><INPUT TYPE="reset"></TD>
</TR>
</TABLE>
<br>


</FORM>
<a href="ViewCars.php"> View Cars </a><br>

<?php
//Database Variables
$Host = "****";
$Database = "******"; 
$Table = "Cars" ;
$Username = "*****"; 
$Password = "*****";


//Connect to Database
$Link = mysql_connect($Host,$Username,$Password);
@mysql_select_db($Database) or die("Unable to select database");

//File Variables
$uploaddir = "images/";
$uploadfile = $_FILES['filename']['name'];
$uploadplace = $uploaddir . basename($uploadfile);



//Query to insert in table
if (isset($_POST['add'])){
         $query="INSERT INTO $Table (Type,Make,Model,Year,VIN,Color,Cost,Price,Bought,Link) 
    VALUES ('$type','$make','$model','$year','$vin','$color','$cost','$sellingprice','$datebought','$pictureslink')";
    
   
   if(mysql_db_query($Database,$query,$Link)){
                         print("<FONT SIZE=\"3\" COLOR=\"Green\">");
                         print("The car was added successfully!\n");
                         print("</FONT>");
                         print("<br>");

                         
        if(file_exists($uploadplace))
                {
                        print("<FONT SIZE=\"3\" COLOR=\"Red\">");
                        echo "File already exists";
                         print("</FONT>");
                        

                        /*echo 'Here is some more debugging info:';
                        print_r($_FILES);
                        */
                }
                else
                        {
                                if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploadplace)) {
                                        echo "File is valid, and was successfully uploaded.\n";
                                        createThumbnail("images", $uploadfile, "images/thumbs", 120, 80); 
                                        
                                        
                                } else {
                                echo "Possible file upload attack!\n";
                                }
                
                        }
                                $query2="INSERT INTO $Table (Filename) VALUES ('$uploadfile')";
                                mysql_query($query2) or die(mysql_error());
                                print($query2);
                        
      
    } else{
    
                        print("<FONT SIZE=\"3\" COLOR=\"Red\">");
                        print("The car was not added!\n");
                        print("</FONT>");
       }





}

//thumbnail function
function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth, $quality){ 
    $details = getimagesize("$imageDirectory/$imageName") or die('Please only upload images.'); 
    $type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']); 
    eval('$srcImg = imagecreatefrom'.$type.'("$imageDirectory/$imageName");'); 
    $thumbHeight = $details[1] * ($thumbWidth / $details[0]); 
    $thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight); 
    imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight,  
    $details[0], $details[1]); 
    eval('image'.$type.'($thumbImg, "$thumbDirectory/$imageName"'. 
    (($type=='jpeg')?', $quality':'').');'); 
    imagedestroy($srcImg); 
    imagedestroy($thumbImg); 
}


//Close connection
mysql_close($Link);

?>
  
</BODY>
</HTML>

put your table, field, and values in the correct syntax so you don't have a problem with reserved words and such.

 

$query2="INSERT INTO `$Table` (`Filename`) VALUES ('$uploadfile')";
mysql_query($query2);
print($query2);

 

table names and field names should go in ticks and values should be in single quotes

 

I could be wrong but filename may be a reserved word so you would have to put it in ticks

 

Ray

 

 

This usually means you set the field to have a unique value. (I am pretty sure) So if you try to enter a row with the FileName the same you will get the error. If you want to keep the FileName unique you may want to do a check first before you insert the data.

 

Ray

So you are inserting a Row, with only 1 column value, which means it will enter NULL or 0 as your VIN, which probably would be a duplicate.  If the record exists, then you need to Update it.  If you insert a row, insert all the data, not just a filename.

phpSensei, I ran that query, it didn't do anything.

Revraz, I am putting all the information, with a new vin everytime and a new picture.

All the date in the table gets inserted successfully except the filename even though when I print the query, it actually shows the correct syntax. INSERT INTO Cars (Filename) VALUES ('whateverpic.jpg')

  id int(11)  UNSIGNED No  auto_increment             

  Type varchar(50) utf8_general_ci  No               

  Make varchar(50) utf8_general_ci  No               

  Model varchar(50) utf8_general_ci  No               

  Year year(4)  No 0000               

  VIN varchar(50) utf8_general_ci  No               

  Color varchar(50) utf8_general_ci  No               

  Cost decimal(10,0)  No 0               

  Price decimal(10,0)  No 0               

  Bought varchar(50) utf8_general_ci  No               

  Link varchar(100) utf8_general_ci  No               

  Filename varchar(250) utf8_general_ci  No               

 

this is copied from php admin.

My full script is on page 1, but here is the insert portion again.

 

$query2="INSERT INTO $Table (Filename) VALUES ('$uploadfile')";

mysql_query($query2);

print($query2);

 

and I even tried.

 

$query2="INSERT INTO `$Table` (`Filename`) VALUES ('$uploadfile')";

mysql_query($query2);

print($query2);

So you only want to Insert 1 column of data into a new Row, but you also have other columns with requirements.  So do you really want to insert a new row or do you just want to update an existing row with a filename?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.