Re: File uploading and saving info on mysql

From: Date: Thu, 03 May 2007 20:45:44 +0000
Subject: Re: File uploading and saving info on mysql
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-254373@lists.php.net to get a copy of this message
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: <?php if (($_FILES["file"]["type"] == "application/msword") || ($_FILES["file"]["type"] == "application/pdf") && ($_FILES["file"]["size"] < 2000000)) { if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
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) . "<br />"; //outputs 'yada' ( correctly as I've typed on the form; echo($filePath) . "<br />"; //outputs '../downloads/66321-Estrutura.doc' and I can check that the file is there; echo($fileType) . "<br />"; //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()."]:<br />\n" . mysql_error());
mysql_close(); echo($user_Query) . "<br />"; //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

« previous php.general (#254373) next »