Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am trying to update a record in my database which is a blob type, I can insert using this method really easily but when using UPDATE it doesn't seem to work, only for text. Would someone be able to point me in the right direction?

Here is my code:

<?php
$conn=mysqli_connect("localhost","root","root","plantidentify");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$ufamid = mysqli_real_escape_string($conn, $_POST['ufamilyid']);
$ucommonname = mysqli_real_escape_string($conn, $_POST['ucomname']);
$usynname = mysqli_real_escape_string($conn, $_POST['usynonyms']);
$uspecies = mysqli_real_escape_string($conn, $_POST['uspecies']);
$uprop = mysqli_real_escape_string($conn, $_POST['upropagation']);
$uimageData = mysqli_real_escape_string($conn, (file_get_contents($_FILES['uimage']['tmp_name'])));
$uheight = mysqli_real_escape_string($conn, $_POST['uheight']);
$utreeclass = mysqli_real_escape_string($conn, $_POST['utreeclass']);
$ulifecycle = mysqli_real_escape_string($conn, $_POST['ulifecycle']);

$UpdateQuery = "UPDATE plant 
                SET FamilyID='$ufamid', CommonName='$ucommonname',       
                    SynonymNames='$usynname', Species='$uspecies', 
                    Propagation='$uprop', Image='$uimageData', 
                    Height='$uheight', TreeClass='$utreeclass', 
                    Lifecycle='$ulifecycle' 
                WHERE PlantID='$_POST[uplantid]'";


if (!mysqli_query($conn,$UpdateQuery)) {
  die('Error: ' . mysqli_error($conn));
}

header("Location: plantedit.php");

mysqli_close($conn);
?>

$uimageData is the variable in which contains the uploaded file from my form in another file. The column in my database where I am trying to update the blob is called "Image".

This might help, here is the form code that is in another .php which the update plant.php gets fired from:

    <?php //display databse values
        while($plant=mysql_fetch_array($retval)) {
        echo"<form action=updateplant.php method=post>";

        echo "<tr>";
        echo "<td>" . "<input type=number name=ufamilyid value='" .$plant['FamilyID']."' ></td>";
        echo "<td>" . "<input type=text name=ucomname value='" .$plant['CommonName']."' ></td>";
        echo "<td>" . "<input type=text name=usynname value='" .$plant['SynonymNames']."' ></td>";
        echo "<td>" . "<input type=text name=uspecies value='" .$plant['Species']."' ></td>";
        echo "<td>" . "<input type=text name=uprop value='" .$plant['Propagation']."' ></td>";
        echo "<td>" . "<input type=file name=uimage></td>";
        echo "<td>" . '<img class="crop2" src="data:image/jpeg;base64,'. base64_encode( $plant['Image'] ).'"/>'." </td>";
        echo "<td>" . "<input type=number name=uheight value='" .$plant['Height']."' ></td>";
        echo "<td>" . "<input type=text name=utreeclass value='" .$plant['TreeClass']."' ></td>";
        echo "<td>" . "<input type=text name=ulifecycle value='" .$plant['Lifecycle']."' ></td>";
        echo "<td>" . "<input type=hidden name=uplantid value='" .$plant['PlantID']."' ></td>";
        echo "<td>" . "<input type=submit name=update value=Update" . " ></td>";
        echo "</tr>";




        echo "</form>";
        }//end while
        mysql_close($conn);
        ?>
share|improve this question
    
any mysql error ? – Anish Jun 13 '14 at 5:27
    
No, no MySQL errors what so ever. I also ran my code in a PHP Validator but all is okay. I am really stuck here. – SourceCoder Jun 13 '14 at 5:36
    
ok .can you echo $UpdateQuery on browser and run it on phpmyadmin – Anish Jun 13 '14 at 5:39
    
Try to remove redirect and then to watch for errors. And also are you sure you making a POST request? Your code does not check if there any data came with the request. – baldrs Jun 13 '14 at 5:40
    
Also inside double-quoted string, array reference should be wrapped like that: {$_POST['uplantid']}. And it sould not be put unescaped inside the query. – baldrs Jun 13 '14 at 5:42
up vote 1 down vote accepted

to upload a file you should mention enctype='multipart/form-data' in form . Hope this will fix your problem.

<?php 

//display databse values
            while($plant=mysql_fetch_array($retval)) {
            echo"<form action='updateplant.php' method='post'  enctype='multipart/form-data' >";

            echo "<tr>";
            echo "<td>" . "<input type=number name=ufamilyid value='" .$plant['FamilyID']."' ></td>";
            echo "<td>" . "<input type=text name=ucomname value='" .$plant['CommonName']."' ></td>";
            echo "<td>" . "<input type=text name=usynname value='" .$plant['SynonymNames']."' ></td>";
            echo "<td>" . "<input type=text name=uspecies value='" .$plant['Species']."' ></td>";
            echo "<td>" . "<input type=text name=uprop value='" .$plant['Propagation']."' ></td>";
            echo "<td>" . "<input type=file name=uimage></td>";
            echo "<td>" . '<img class="crop2" src="data:image/jpeg;base64,'. base64_encode( $plant['Image'] ).'"/>'." </td>";
            echo "<td>" . "<input type=number name=uheight value='" .$plant['Height']."' ></td>";
            echo "<td>" . "<input type=text name=utreeclass value='" .$plant['TreeClass']."' ></td>";
            echo "<td>" . "<input type=text name=ulifecycle value='" .$plant['Lifecycle']."' ></td>";
            echo "<td>" . "<input type=hidden name=uplantid value='" .$plant['PlantID']."' ></td>";
            echo "<td>" . "<input type=submit name=update value=Update" . " ></td>";
            echo "</tr>";




            echo "</form>";
            }//end while
            mysql_close($conn);
        ?>
share|improve this answer

Looks like you have a typo in the WHERE statement, $_POST[uplantid] should be ". $_POST['uplantid'] ."

share|improve this answer
    
I made the change with the typo that you stated but there was still no difference. The image still doesn't seem to UPDATE into the table of my database. Thank you for your input though. – SourceCoder Jun 13 '14 at 5:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.