Re: [PHP] File uploading and saving info on mysql
Marcelo Wolfgang wrote: Hi all, I'm developing for my first time an upload file form which will populate a sql insert query, I think I got everything working fine, but the data isn't been saved on the database. Can someone help me with what I'm doing wrong here ? the code follow: 0) { echo "Return Code: " . $_FILES["file"]["error"] . ""; } else { if (file_exists("../downloads/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "../downloads/" . $_FILES["file"]["name"]); } } } else { echo "Invalid file"; } $title = $_POST["title"]; $filePath = "../downloads/" . $_FILES["file"]["name"]; if($_FILES["file"]["type"] == "application/pdf"){ $fileType = "pdf"; } else if ($_FILES["file"]["type"] == "application/msword"){ $fileType = "doc"; } echo($title) . ""; //outputs 'yada' ( correctly as I've typed on the form; echo($filePath) . ""; //outputs '../downloads/66321-Estrutura.doc' and I can check that the file is there; echo($fileType) . ""; //outputs 'doc' this is correct; mysql_connect("localhost",$db_user,$db_pass) or die (mysql_error());; mysql_select_db ($db_table); $user_Query = mysql_query("INSERT INTO tb_downloads (var_title, var_filepath, var_filetype, dt_data, bol_active) VALUES ('$title', '$filePath','$fileType','NOW(),1)"); You have an error in your SQL statement. You have a single quote opening just before your NOW() call. I usually setup my query in a string before I place it in the mysql_query() command. This way I can echo it out and see it before it get executed. $SQL = "INSERT INTO tb_downloads ( var_title, var_filepath, var_filetype, dt_data, bol_active ) VALUES ( '$title', '$filePath', '$fileType', NOW(), 1 )"; echo $SQL; plus, just for debugging, you should add this to your query line $user_Query = mysql_query($SQL) or die("SQL Error #[".mysql_errno()."]:\n" . mysql_error()); mysql_close(); echo($user_Query) . ""; //outputs nothing (? I suck at debugin queries) this will only output the Result pointer from mysql. You need to use something like this: print_r(mysql_fetch_assoc($user_Query)); you might want to look at mysql_num_rows() also. This will tell you the number of results you got back. Take a look at this example, it will give you all the needed parts to make this work http://us.php.net/mysql_fetch_assoc#id5291494 header("Location: http://www.w3ol.com.br/50congresso/adm/downloads.php";); // I know that this won't work while I echo something on the page, but the echo is there for debug only ?> TIA Marcelo Wolfgang -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Unknown -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] File uploading and saving info on mysql
On Thu, May 3, 2007 3:23 pm, Marcelo Wolfgang wrote: > I'm developing for my first time an upload file form which will > populate > a sql insert query, I think I got everything working fine, but the > data > isn't been saved on the database. Can someone help me with what I'm > doing wrong here ? > > the code follow: > > if (($_FILES["file"]["type"] == "application/msword") > || ($_FILES["file"]["type"] == "application/pdf") Browsers will probably NOT populate these reliably... Mac Safari, might, for example, choose "application/x-pdf" for the PDF file. You really can't rely on 'type' to be useful in any way, shape, or form. See various recent thread regarding "mime_magic" and other options. > && ($_FILES["file"]["size"] < 200)) >{ >if ($_FILES["file"]["error"] > 0) > { > echo "Return Code: " . $_FILES["file"]["error"] . ""; > } >else > { > if (file_exists("../downloads/" . $_FILES["file"]["name"])) >{ >echo $_FILES["file"]["name"] . " already exists. "; >} > else >{ >move_uploaded_file($_FILES["file"]["tmp_name"], >"../downloads/" . $_FILES["file"]["name"]); >} > } >} > else >{ >echo "Invalid file"; >} > $title = $_POST["title"]; > $filePath = "../downloads/" . $_FILES["file"]["name"]; > if($_FILES["file"]["type"] == "application/pdf"){ > $fileType = "pdf"; > } else if ($_FILES["file"]["type"] == "application/msword"){ > $fileType = "doc"; > } > echo($title) . ""; //outputs 'yada' ( correctly as I've typed on > the form; > echo($filePath) . ""; //outputs > '../downloads/66321-Estrutura.doc' > and I can check that the file is there; > echo($fileType) . ""; //outputs 'doc' this is correct; > > mysql_connect("localhost",$db_user,$db_pass) or die (mysql_error());; > mysql_select_db ($db_table); > $user_Query = mysql_query("INSERT INTO tb_downloads (var_title, > var_filepath, var_filetype, dt_data, bol_active) VALUES ('$title', > '$filePath','$fileType','NOW(),1)"); That's not a query, that's a result... And you aren't checking it to see if it's an error, and you're not using http://php.net/mysql_error to find out what the error is. > mysql_close(); > > echo($user_Query) . ""; //outputs nothing (? I suck at debugin > queries) And once you have closed the connection mysql_close() the result is GONE, so if you do want to see it, you'd need it to be before the mysql_close() probably. It won't be very interesting, really, as it will be FALSE if your SQL is messed up, and just a number from 1 to N (where N is the number of queries you have sent) if the query succeeded. -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] File uploading and saving info on mysql
Richard Lynch wrote: Browsers will probably NOT populate these reliably... Mac Safari, might, for example, choose "application/x-pdf" for the PDF file. You really can't rely on 'type' to be useful in any way, shape, or form. Thanks for the tips, I've deleted this check, and changed how I check for the file type in the code. and I've redone the code to match Jim Lucas suggestion also, it's working very nicely right now. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php