0

I want to insert image into my database table. I have search google but I can not insert image into my database. My database field name is 'photo' and data type is 'blob'. please help...

here is my code:

<?php
$id="";
$opr="";
if(isset($_GET['opr']))
    $opr=$_GET['opr'];

if(isset($_GET['rs_id']))
    $id=$_GET['rs_id'];
//--------------add data-----------------   
if(isset($_POST['btn_sub'])){
    $fac_name=$_POST['factxt'];
    $batch=$_POST['batchtxt'];
    $reg=$_POST['regtxt'];
    $s_name=$_POST['snametxt'];
    $f_name=$_POST['fnametxt'];
    $gender=$_POST['gender']
    $phone=$_POST['phonetxt'];
    $mail=$_POST['emailtxt'];
    $photo=$_POST['photos'];    

    $sql_ins=mysql_query("INSERT INTO stu_tbl 
                          VALUES(
                            NULL,
                            '$fac_name',
                            '$batch',
                            '$reg',
                            '$s_name',
                            '$f_name' ,
                            '$gender',
                            '$phone',
                            '$mail',
                            '$photo'
                            )
                    ");
    if($sql_ins==true)
        $msg="1 Row Inserted";
    else
        $msg="Insert Error:".mysql_error();
}
?>

<!DOCTYPE html>
<head>
</head>
<body>
    <h2 class="custom-light-heading" align="center">Add New Student</h2>
    <hr>
    <form method="post">
        <table>
            <tr>
                <td>Faculties's Name</td>
                <td>
                    <input type="text" />
                </td>
            </tr>
            <tr>
                <td>Batch:</td>
                <td>
                    <input type="text" class="form-control" name="batchtxt" id="textbox" />
                </td>
            </tr>

            <tr>
                <td>Registration:</td>
                <td>
                    <input type="text" class="form-control" name="regtxt" id="textbox" />
                </td>
            </tr>
            <tr>
                <td>Student Name:</td>
                <td>
                    <input type="text" class="form-control" name="snametxt" id="textbox" />
                </td>
            </tr>
            <tr>
                <td>Father's Name:</td>
                <td>
                    <input type="text" class="form-control" name="fnametxt" id="textbox" />
                </td>
            </tr>
            <tr>
                <td>Gender:</td>
                <td>
                    <input type="radio" name="gender" value="Male" checked="checked" />Male
                    <input type="radio" name="gender" value="Female" />Female
                </td>
            </tr>
            <tr>
                <td>Phone:</td>
                <td>
                    <input type="text" class="form-control" name="phonetxt" id="textbox" />
                </td>
            </tr>
            <tr>
                <td>E-mail:</td>
                <td>
                    <input type="text" class="form-control" name="emailtxt" id="textbox" />
                </td>
            </tr>
            <tr>
                <td>photo:</td>

                <td>
                    <input type="File" name="photos" />
                </td>
            </tr>
            <tr>
                <td>

                    <input type="submit" name="btn_sub" class="btn btn-success" value="Add" id="button-in" />
                </td>
            </tr>
        </table>
        </div>
    </form>
    </div>
    <?php ?>
</body>
</html>
garryp
  • 5,508
  • 1
  • 29
  • 41
  • Please check the marked answer of this link:- http://stackoverflow.com/questions/7052655/insert-blobs-in-mysql-databases-with-php – Alive to die - Anant May 10 '15 at 14:34
  • 1
    I don't see an opening `
    ` tag. plus it needs to be a post method with a valid enctype.
    – Funk Forty Niner May 10 '15 at 14:35
  • you're welcome. consult my answer below. You missed something important, the "enctype". – Funk Forty Niner May 10 '15 at 14:43
  • I noticed something else and have made a slight edit to my answer. Please reload it if you seen the original one I did. `$photo=$_POST['photos'];` needs to be `$photo=$_FILES['photos'];` – Funk Forty Niner May 10 '15 at 14:46
  • Then this `` for `Faculties's Name` you don't have a name attribute for it. Reload my answer again and if I missed something, use error reporting as outlined in my answer. Your query will also fail because of that. Make sure everything is accounted for. – Funk Forty Niner May 10 '15 at 14:53
  • ok, where are we at here? did you see my answer? are you getting errors? plus, if you're going to make additional edits, don't overwrite your original, mark it as an edit under your original post, otherwise I stand at getting downvoted for it. – Funk Forty Niner May 10 '15 at 15:12
  • Thank you very much for your cordial help :) – Tasnim Azam May 10 '15 at 15:40
  • You're quite welcome, *cheers* – Funk Forty Niner May 10 '15 at 15:41

1 Answers1

4

As I said in comments: I don't see an opening <form> tag. plus it needs to be a post method with a valid enctype when working with files.

This being in your original posted question:

Add the following to your code, replacing the action to your own.

<form action="handler.php" method="post" enctype="multipart/form-data">

If doing this inside the same file, you can just do action="" as "self".

Also this $photo=$_POST['photos']; needs to be $photo=$_FILES['photos']; using $_FILES and not $_POST since it's a file and not a text input or anything other than a file.

Then this <input type="text" /> for Faculties's Name you don't have a name attribute for it, and seems related to $_POST['factxt'].

So modify it to <input type="text" name="factxt" />.

  • Using error reporting would have caught that. See my notes below about it.

Sidenote: Your present code is open to SQL injection. Use mysqli with prepared statements, or PDO with prepared statements, they're much safer.


Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.


Footnotes:

  • You should use proper bracing techniques. Many of your conditional statements don't have braces if(condition){...} and this could cause havoc with your code.

  • If you truly want to check if your query was successful, use mysql_affected_rows().

  • If you see notices about mysql_ being deprecated, then it will be time for you to switch over to mysqli_ or PDO, which you should be using anyway, since it will be removed from future PHP releases.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141