1

hello i have a form with many input ( name, last name, picture,age..) and i want to submit all this information into my database , i know how to do it for a simple input but with the picture i have some problem can someone help me please my html file is

<form action="insertion.php" method="post" >
<table >
 <tr>
 <td><strong>Titre</strong></td>
 <td><input name="titre" type="text" value=""  /></td>
 </tr>
 <tr>
 <td><strong>Annee</strong></td>
 <td><input name="annee" type="number" value=""  /></td>
 </tr>
 <tr>
 <td><strong>Genre musical</strong></td>
 <td><input name="Gmusical" type="texte" value="" /></td>
 </tr>
 <tr>
 <td>
 <strong>Picture</strong>
 </td>
 <td>
 <input type="file" name="img"/>
 </td>
 </tr>
</table>
 <input type="submit" value="Submit "  />
</form>

my file insertion.php to sublmit into the database is

<?php
include("connexion.php");
$titre=$_POST['titre'];
$annee=$_POST['annee'];
$Gmusical=$_POST['Gmusical'];
$picture=$_POST['img'];

$req="INSERT INTO `cd`
(`titre`, `Annee`, `genremusical`, `Image`) 
VALUES 
 ('$titre','$annee','$Gmusical','$picture');";

            if (mysql_query($req))
            {
            echo "ok";
            }
            else 
            echo 'ko';
            }
Marooweb
  • 476
  • 3
  • 8
  • 21
  • 1
    you probably need to see this http://stackoverflow.com/questions/17153624/using-php-to-upload-file-and-add-the-path-to-mysql-database – Abhik Chakraborty Mar 06 '14 at 21:11
  • 3
    Sidenote: In order to process image-related forms, your form will need to include `enctype='multipart/form-data'` first and foremost. Plus this `$picture=$_POST['img'];` should most likely be `$picture=$_FILES['img'];` – Funk Forty Niner Mar 06 '14 at 21:11
  • You should NOT store binary image data to the database. Just save them as image files and save the path/filename to the database. – djot Mar 06 '14 at 21:19
  • You can store binary data if you want to - but you should know why you want to! – Strawberry Mar 06 '14 at 22:04

4 Answers4

3

Generally you do not want to store actual BLOB(binary large object) data types in a database. You store the path to the image, located somewhere on your web server.

So in the column "Image", you would store the path "images/photo1103.jpg".

To display the photo: echo "<img src=". $image_query_fetch['Image'] .'" alt=\"\" />";

jon.r
  • 896
  • 8
  • 16
1

You have to set the column-type of the "image" column in the databse to BLOB or LONGBLOB (or others), for example:

CREATE TABLE cd (
 ... 
 Image LONGBLOB, 
); 

And then simply insert the data like you did.

A usually better way to solve this is to store the file in the filesystem and only save the path to the file in the database. If you want to do so, you may want to check out this SO question. (See also this one.)

(As user Fred -ii- pointed out in the comment, you will also have to set the enctype of the form-Tag to "multipart/form-data".)

Stefan
  • 171
  • 1
  • 13
1

the column type can be a varchar I would also suggest using mysqli instead of mysql

and the following code should help you: Edited the code try this as one file and maybe try to debug it by using echo $picture_name; if you haven't tried this yet.

(Optional: One more thing that could help is ob_start();place this at the very top of the file just after the <?php tag and than at the bottom of the page close it with ob_flush(); before ?> tag so you get something like: enter image description here)

<?php
include('connexion.php');

function outout_errors($error) {
    echo '<ul><li>',$error.'</li></ul>';
}

if($_POST) {
    $titre = mysql_real_escape_string(strip_tags($_POST['titre']));
    $annee = mysql_real_escape_string(strip_tags($_POST['annee']));
    $Gmusical = mysql_real_escape_string(strip_tags($_POST['Gmusical']));
    $picture_tmp = $_FILES['img']['tmp_name'];
    $picture_name = $_FILES['img']['name'];
    $picture_type = $_FILES['img']['type'];

    $allowed_type = array('image/png', 'image/gif', 'image/jpg', 'image/jpeg');

    if(in_array($picture_type, $allowed_type)) {
        $path = 'images/'.$picture_name; //change this to your liking
    } else {
        $error[] = 'File type not allowed';
    }

    if(!is_numeric($annee)) {
        $error[] = $annee.' is not a number';
    }

    if(!empty($error)) {
        echo '<font color="red">'.output_errors($error).'</font>';

    } else if(empty($error)) {
        $req="INSERT INTO `cd` (`titre`, `Annee`, `genremusical`, `Image`)  VALUES ('$titre', '$annee', '$Gmusical', '$path')";
        move_uploaded_file($picture_tmp, $path);

        if (mysql_query($req)) {
            echo 'ok';
        } else {
            echo 'ko';
        }
    }
}


?>

<form action="" method="post" enctype="multipart/form-data">
    <table>
     <tr>
        <td><strong>Titre</strong></td>
        <td><input name="titre" type="text"></td>
     </tr> <tr>
        <td><strong>Annee</strong></td>
        <td><input name="annee" type="text"></td>
     </tr><tr>
        <td><strong>Genre musical</strong></td>
        <td><input name="Gmusical" type="text"></td>
     </tr><tr>
        <td><strong>Picture</strong></td>
     <td><input type="file" name="img"></td>
     </tr>
     <tr>
        <input type="submit">
     </tr>
    </table>
</form>
SuperDJ
  • 7,488
  • 11
  • 40
  • 74
-1
<?php include("config.php");?>
<?php
error_reporting("0");
if(isset($_POST['submit'])) {   
    // Get image name   
    $image = $_FILES['image']['name'];
    // Get text
    $image_name= $_POST['image_name'];
    // Image file directory 
    $target = "images/".basename($image);
    // Now insert query
    $sql = "INSERT INTO addimage(image, image_name) values ('$image','$image_name')";
    // Execute query
    mysqli_query($dbcon,$sql);  
    if(move_uploaded_file($_FILES['image']['tmp_name'],$target))    
    {
      $img = "image uploading successfully";
    } else {
      $img = "faild image uploading";   
    }   
}   
?>  

<!DOCTYPE html>
<html>    
<head>
  <title>Upload image with text Field</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script type="text/javascript" src="script.js"></script>
</head>

<body>
  <div class="container">
    <div class="col-sm-10">
      <h1>Image uploading with text field</h1>
      <div class="card-box">
        <div class="row">
          <div class="col-md-6">
            <div class="p-20">
              <form method="post" enctype="multipart/form-data">
                <h5 class="alert-success">
                  <?php if(isset($img)){ echo $msg;}?>
                </h5>
                <div class="form-group">
                  <label class="control-label">Image Name</label>
                  <input type="text" class="form-control" data-size="sm" name="image_name">
                </div>
                <div class="form-group">
                  <label class="control-label">Small file style</label>
                  <input type="file" class="filestyle" data-size="sm" name="image">
                </div>
                <div class="form-group">
                  <input type="submit" class="btn btn-success" value="Save" name="submit">
                </div>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>    
</html>
mhrabiee
  • 805
  • 10
  • 23