bobahad Posted December 19, 2007 Share Posted December 19, 2007 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 Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 19, 2007 Share Posted December 19, 2007 Can you post the full script so I can examine the problem please? Quote Link to comment Share on other sites More sharing options...
bobahad Posted December 19, 2007 Author Share Posted December 19, 2007 Hey, not sure if It will all fit here so I posted it in a bin. http://pastebin.com/d264ebc33 Thanks Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 19, 2007 Share Posted December 19, 2007 add error_reporting(E_ALL); into your script to see what errors you get. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 19, 2007 Share Posted December 19, 2007 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> Quote Link to comment Share on other sites More sharing options...
craygo Posted December 19, 2007 Share Posted December 19, 2007 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 Quote Link to comment Share on other sites More sharing options...
bobahad Posted December 19, 2007 Author Share Posted December 19, 2007 Hi guys, I got this The car was added successfully! File is valid, and was successfully uploaded. Duplicate entry '' for key 2 Quote Link to comment Share on other sites More sharing options...
revraz Posted December 19, 2007 Share Posted December 19, 2007 Is your column set to Unique? Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 19, 2007 Share Posted December 19, 2007 may auto increment key as well Quote Link to comment Share on other sites More sharing options...
craygo Posted December 19, 2007 Share Posted December 19, 2007 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 Quote Link to comment Share on other sites More sharing options...
bobahad Posted December 19, 2007 Author Share Posted December 19, 2007 hey guys, The field is not set to unique, the only fields that are modified is id which is auto increment primary, and VIN which is unique. The Filename field is set to Varchar(250). Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 19, 2007 Share Posted December 19, 2007 try mysql_query("ALTER TABLE table_name MODIFY id int(11) AUTO_INCREMENT"); Quote Link to comment Share on other sites More sharing options...
revraz Posted December 19, 2007 Share Posted December 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
bobahad Posted December 19, 2007 Author Share Posted December 19, 2007 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') Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 19, 2007 Share Posted December 19, 2007 have you tried echoing the file name? Quote Link to comment Share on other sites More sharing options...
craygo Posted December 19, 2007 Share Posted December 19, 2007 What is the structure of your table?? Quote Link to comment Share on other sites More sharing options...
bobahad Posted December 19, 2007 Author Share Posted December 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
bobahad Posted December 19, 2007 Author Share Posted December 19, 2007 adam: yeah I did echo the $uploadfile and it shows the correct file name picture.jpg Quote Link to comment Share on other sites More sharing options...
revraz Posted December 19, 2007 Share Posted December 19, 2007 Give us your whole insert statement and not just the filename part. Quote Link to comment Share on other sites More sharing options...
bobahad Posted December 19, 2007 Author Share Posted December 19, 2007 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); Quote Link to comment Share on other sites More sharing options...
revraz Posted December 19, 2007 Share Posted December 19, 2007 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? Quote Link to comment Share on other sites More sharing options...
bobahad Posted December 19, 2007 Author Share Posted December 19, 2007 omg I am an idiot...lmao... I read the code over and over again and couldn't find what's wrong. THANK YOU Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.