unable to insert PDF in mysql

Alles, was PHP betrifft, kann hier besprochen werden.

unable to insert PDF in mysql

Postby thunder.kunal » 21. April 2015 21:51

<!doctype html>
<html lang=en>
<head>
<title>upload pdf</title>
<meta charset=utf-8>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
<input type="file" name="pdf"><input type="submit" name="submit" value="upload">
</form>
<?php
if(isset($_POST['submit']))
{

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pdf_upload";

$con = mysql_connect($servername,$username,$password);
if (!$con)
{
die("Connect Fail:" . mysql_error());
}
$db_select = mysql_select_db($dbname, $con);
if (!$db_select)
{
die ("DB Select fail:" . mysql_error());
}
$pdfName= mysql_real_escape_string($_FILES["pdf"]["name"]);
$pdfData= mysql_real_escape_string(file_get_contents($_FILES["pdf"]["tmp_name"]));
$pdfType= mysql_real_escape_string($_FILES["pdf"]["type"]);
//echo $pdfData;
if(substr($pdfType,0,15) == "application/pdf")
{
echo "working code";
$sqlquery="INSERT INTO blob1 VALUES ('',$pdfName,$pdfData)";
if ($results = mysql_query($sqlquery))
{
$numrows = mysql_affected_rows();
echo 'affected rows: ' .$numrows;
$event = stripslashes($event);
} else
{
echo 'MySQL Error (' .mysql_errno(). '): ' .mysql_error();
}
echo "<hr>$sqlquery<hr>";
}
else
{
echo "only pdf allowed!!";
}
mysql_query($sqlquery) or die('<p>Error, query failed');
}
?>
</body>
</html>

the connection worksfine, but the table blob1 doesnt update
please help
thunder.kunal
 
Posts: 1
Joined: 21. April 2015 21:46
Operating System: win 7

Re: unable to insert PDF in mysql

Postby Nobbie » 22. April 2015 11:30

Your code is faulty, you are doing a mysql_query() twice!! And please format your code as i did, otherwise its unreadable spaghetti code.


Code: Select all
<?php
        if(isset($_POST['submit']))
        {

                $servername = "localhost";
                $username = "root";
                $password = "";
                $dbname = "pdf_upload";

                $con = mysql_connect($servername,$username,$password);
                if (!$con)
                {
                        die("Connect Fail:" . mysql_error());
                }

                $db_select = mysql_select_db($dbname, $con);
                if (!$db_select)
                {
                        die ("DB Select fail:" . mysql_error());
                }

                $pdfName= mysql_real_escape_string($_FILES["pdf"]["name"]);
                $pdfData= mysql_real_escape_string(file_get_contents($_FILES["pdf"]["tmp_name"]));
                $pdfType= mysql_real_escape_string($_FILES["pdf"]["type"]);
                //echo $pdfData;
                if(substr($pdfType,0,15) == "application/pdf")
                {
                        echo "working code";
                        $sqlquery="INSERT INTO blob1 VALUES ('',$pdfName,$pdfData)";
                        if ($results = mysql_query($sqlquery))   // THIS IS THE FIRST CALL TO mysql_query()!! NOW PROCEED TO THE BOTTOM...
                        {
                                $numrows = mysql_affected_rows();
                                echo 'affected rows: ' .$numrows;
                                $event = stripslashes($event);
                        } else
                        {
                                echo 'MySQL Error (' .mysql_errno(). '): ' .mysql_error();
                        }
                        echo "<hr>$sqlquery<hr>";
                }
                else
                {
                        echo "only pdf allowed!!";
                }

                mysql_query($sqlquery) or die('<p>Error, query failed');   // WHAT THE HECK IS THIS?? IT IS A SECOND QUERY!!! THIS QUERY POSSIBLY DESTROY EVERYTHING
        }
?>


You also have NONE validation, whether the upload was running successfully, you neither check the contents of $pdfData. Finally i have no clue about your table blob1, what is the create table statement, what are the columns? And how do you evaluate if the row has been created or not?
Nobbie
 
Posts: 8780
Joined: 09. March 2008 13:04


Return to PHP

Who is online

Users browsing this forum: No registered users and 4 guests