0

I am unsucessfully trying to capture file and data and upload to a php file. I am not sure what i am doing wrong if someone can maybe see what i am doing wrong please. It will upload file/image no problem but when i add a text input to post it will not work, here is what i have been using as a template to trial

<iframe name="my_iframe" src="" id="my_iframe"></iframe>
<form action="http://www.********/upload11.php" method="post" enctype="multipart/form-data" target="my_iframe">
<input type="file"  input id="input" name="image" />
<input type="text" name="Description" value="Image of Time"/>
<div>
<input type="submit" value="Send">
</div>
</form>

upload11.php

<?php
$upload_image = $_FILES["image"][ "name" ];
$folder = "images/";
move_uploaded_file($_FILES["image"]["tmp_name"], "$folder".$_FILES["image"]["name"]);;
$file = 'images/'.$_FILES["image"]["name"];
$uploadimage = $folder.$_FILES["image"]["name"];
$newname = $_FILES["image"]["name"];

$resize_image = $folder.$newname; 
list( $width,$height ) = getimagesize( $uploadimage );
$newwidth = 550;
$newheight = 350;
$thumb = imagecreatetruecolor( $newwidth, $newheight );
$source = imagecreatefromjpeg( $resize_image );
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg( $thumb, $resize_image, 100 ); 
$out_image=addslashes(file_get_contents($resize_image));
$msg = '';
if($_SERVER['REQUEST_METHOD']=='POST'){
$a = ('" alt="" />');
$b = ("'<img src=\"\https://www.******/images/".$_FILES['image']['name']."$a'");
$Description = $_POST['Description'];
$image = $_FILES['image']['tmp_name'];
$img = file_get_contents($image);
$con = mysqli_connect('***','******','******','********');
$sql = "INSERT INTO links (hyper_links, link ) VALUES ($b, $Description)";

$stmt = mysqli_prepare($con,$sql);

mysqli_stmt_bind_param($stmt, "s",$img);
mysqli_stmt_execute($stmt);

$check = mysqli_stmt_affected_rows($stmt);
if($check==1){
    $msg = 'Successfullly UPloaded';
}else{
    $msg = 'Could not upload';
}
mysqli_close($con);
}
?>
<?php
echo $msg;
?>
INOH
  • 365
  • 2
  • 9
  • 27
  • please add **error_reporting(E_ALL);** to check error while you uploading image. – Sunny Sep 15 '16 at 23:49
  • I did add into my php file, but when i submit i just get Could not Upload – INOH Sep 15 '16 at 23:57
  • I suggest you take a look at my answer to this question over here: [Full Secure Image Upload Script](http://stackoverflow.com/questions/38509334/full-secure-image-upload-script/38712921#38712921) It will teach you a lot about upload scripts and security, as well as give you a fully working script at the end. You can easily add text fields to the form and process that with PHP. This shouldn't be a problem at all. – icecub Sep 15 '16 at 23:58

2 Answers2

1

See your code you forgot to add ' ' in $b and $Description

$sql = "INSERT INTO links (hyper_links, link ) VALUES ($b, $Description)";

It should be like this

$sql = "INSERT INTO links (hyper_links, link ) VALUES ('$b', '$Description')";

Hope this will work :)

Sunny
  • 402
  • 4
  • 15
  • 1
    It won't. He's preparing a query without using selectors at all. He's then even trying to bind parameters that make no sense at all. Aside from that, the entire script is full of errors. Like not using `basename()` when moving the file.. double `;;` at line 4 etc etc.. – icecub Sep 16 '16 at 00:11
  • 1
    Btw: Not using quotes when preparing a query is actually a good thing :P – icecub Sep 16 '16 at 00:14
  • Nope! I didn't agree by this @icecub. My php file is also not working when I forgot to add quotes while preparing a query. – Sunny Sep 16 '16 at 00:17
  • 2
    Although I normally don't like W3Schools, have a look here: http://www.w3schools.com/php/php_mysql_prepared_statements.asp for an example, and here http://php.net/manual/en/mysqli.prepare.php with I quote `Note that single-quotes around the parameter markers _will_ prevent your statement from being prepared correctly.` – icecub Sep 16 '16 at 00:20
  • everything works except as soon as i add the 2 value and column (link & $Description) this is when it fails when they are added – INOH Sep 16 '16 at 00:22
1

I've been rewriting half your script to get rid of all the errors and unnessesary variables. This should make it work. If not, it should at least give usefull information about what is going on:

<?php

if(!empty($_FILES["image"])){
    $imgName = $_FILES["image"]["name"];
    $imgTmpName = $_FILES['image']['tmp_name'];
    $upload_dir = "images/";

    if(move_uploaded_file($imgTmpName, $folder . basename($imgName))){
        $imgPath = $upload_dir . $imgName;

        list( $imgWidth, $imgHeight ) = getimagesize( $imgPath );

        $newImgWidth = 550;
        $newImgHeight = 350;

        $thumbnailImg = imagecreatetruecolor( $newImgWidth, $newImgHeight );
        $originalImg = imagecreatefromjpeg( $imgPath );
        imagecopyresized($thumbnailImg, $originalImg, 0, 0, 0, 0, $newImgWidth, $newImgHeight, $imgWidth, $imgHeight);

        imagejpeg( $thumbnailImg, $imgPath, 100 );

        $mysqli = new mysqli("localhost", "user", "password", "database");

        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }

        if($stmt = $mysqli->prepare("INSERT INTO links (hyper_links, link ) VALUES (?, ?)")){
            if($stmt->bind_param("ss", $html, $descryption)){

                $html = "<img src=\"https://www.******/". $imgPath ."\">";
                $descryption = $_POST['Description'];

                if($stmt->execute()){
                    if($stmt->affected_rows > 0){
                        echo "Successfully Uploaded";

                        $stmt->close();
                        $mysqli->close();
                    } else {
                        echo "Could not upload";
                    }
                } else {
                    die("Execute() failed: " . htmlspecialchars($stmt->error));
                }
            } else {
                die("Bind_param() failed: " . htmlspecialchars($stmt->error));
            }
        } else {
            die("Prepare() failed: " . htmlspecialchars($stmt->error));
        }
    } else {
        die("Unable to move uploaded file to upload folder.");
    }
} else {
    die("You did not select a file to upload.");
}

?>

Make a backup of what you have right now, and test this out. Let me know how it went.

icecub
  • 8,615
  • 6
  • 41
  • 70
  • i tried running your code and received an the error `Fatal error: Call to undefined method mysqli_stmt::affected_rows() in /data/9/3/62/77/3714077/user/4129685/htdocs/upload01.php on line 35` – INOH Sep 16 '16 at 01:30
  • not sure if this matters but i am using mysql not sqli – INOH Sep 16 '16 at 01:31
  • 1
    @INOH I made a small mistake. That error should be fixed now. On your question: No that doesn't matter. – icecub Sep 16 '16 at 01:33
  • 1
    @INOH Also just fixed another little error in `$html` – icecub Sep 16 '16 at 01:41
  • that was brilliant, i am very impressed, thanks for taking the time to help me, i have been stuck on this for awhile. You even fixed my img src, thanks so much – INOH Sep 16 '16 at 01:49
  • @INOH You're welcome :) Please learn to work with error handling. By implementing if statements and echoing out errors, it's A LOT easier to figure out what's going wrong. Instead of having to find the problem in your entire code, you just have to find the problem in a few lines only. – icecub Sep 16 '16 at 01:56
  • icecube, if you could revisit this question, i am 2 issues, 1 being that if there is not a image to upload i get the `unable to move uploaded file to upload folder` error message because not all times is there a file/image to upload. and 2nd issue is that some images i upload maybe portrait images turn black.please if you can please have another look.. thanks in advance – INOH Sep 19 '16 at 17:41
  • 1
    @INOH 1) Just change the error message in the code. It's normal this happens whenever someone tries to upload something else than an image. 2) I've never heard about images turning "black". This is most likely an issue with your systems graphics card and not the code. – icecub Sep 20 '16 at 01:46
  • Thanks was able to fix upload still working on images turning black, seems to be portrait or large images – INOH Sep 20 '16 at 01:53