1
    <html>
    <head>
    <style>
    body {font-family: "Lato", sans-serif;}

    /* Style the tab */
    div.tab {
        overflow: hidden;
        border: 1px solid #ccc;
        background-color: #f1f1f1;
    }
    fieldset{
      padding-left: 10px;

      margin-left: 360px;
      margin-right: 380px;
    }
    /* Style the buttons inside the tab */
    div.tab button {
        background-color: inherit;
        float: left;
        border: none;
        outline: none;
        cursor: pointer;
        padding: 14px 16px;
        transition: 0.3s;
        font-size: 17px;
    }

    /* Change background color of buttons on hover */
    div.tab button:hover {
        background-color: #ddd;
    }

    /* Create an active/current tablink class */
    div.tab button.active {
        background-color: #ccc;
    }

    /* Style the tab content */
    .tabcontent {
        display: none;
        padding: 6px 12px;
        -webkit-animation: fadeEffect 1s;
        animation: fadeEffect 1s;
    }

    /* Fade in tabs */
    @-webkit-keyframes fadeEffect {
        from {opacity: 0;}
        to {opacity: 1;}
    }

    @keyframes fadeEffect {
        from {opacity: 0;}
        to {opacity: 1;}
    }
    </style>

    </head>
    <body>


    <div class="tab">
      <button class="tablinks" onclick="openCity(event, 'tab1')">
     Déposez votre annonce</button>
      <button class="tablinks" onclick="openCity(event, 'tab2')">
     Annonces en ligne</button>
    </div>

    <div id="tab1" class="tabcontent">
        <fieldset>
    <form method="post" action="form.php" enctype='multipart/form-data'>

    <input type="radio" name="type" value="Particulier">Particulier
    <input type="radio" name="type" value="Professionnel">Professionnel
    <br><br>
    <label>Nom:</label>
    <input type="text" name="name">
    <br><br>
    <label>Email:</label>
    <input type="email" name="email">
    <br><br>
    <label>Téléphone:</label>
    <input type="text" name="tel" required>
    <br><br>
    <label>Ville:</label>
    <select name="ville">
      <option value="blank">« Sélectionnez ville »</option>
      <?php 
      $conn=new PDO('mysql:host=localhost;dbname=cars','root','');
    $sl= "SELECT distinct Ville from villes";
    $std=$conn->query($sl);
    $res=$std->fetchAll(PDO::FETCH_COLUMN,0);
      for($i=0;$i<count($res);$i++){
      echo " <option value=".$res[$i].">".$res[$i]."</option>";
    } ?>
    </select>
    <br><br>
    <label>Catégorie:</label>
    <select name="categ">
      <option value="blank">&nbsp;« Type de voiture » &nbsp; </option>
      <option value="occasion">Voitures occasion</option>
       <option value="neuf">Voitures neuves</option>
      </select>
       <br><br>
      <label>Sous-catégorie:</label>
      <select name="marque">
      <option value="blank">&nbsp; &nbsp;--  Marque  -- &nbsp; &nbsp; 
      </option>
     <?php 
      $conn=new PDO('mysql:host=localhost;dbname=cars','root','');
    $sl= "SELECT distinct carbrand from car";
    $std=$conn->query($sl);
    $res=$std->fetchAll(PDO::FETCH_COLUMN,0);
      for($i=0;$i<count($res);$i++){
      echo " <option value=".$res[$i].">".$res[$i]."</option>";
    } ?>
    </select>
    <br><br>
    <select name="year">
    <option value="blank">&nbsp;&nbsp;--  Année  --&nbsp;&nbsp;</option>
     <?php 
      $conn=new PDO('mysql:host=localhost;dbname=cars','root','');
    $sl= "SELECT distinct year from years";
    $std=$conn->query($sl);
    $res=$std->fetchAll(PDO::FETCH_COLUMN,0);
      for($i=0;$i<count($res);$i++){
      echo " <option value=".$res[$i].">".$res[$i]."</option>";
    } ?>
    </select> &nbsp;
    <select name="km">
    <option value="blank">&nbsp;&nbsp;--  Kilométrage  --&nbsp;&nbsp;
    </option>
     <?php 
      $conn=new PDO('mysql:host=localhost;dbname=cars','root','');
    $sl= "SELECT distinct km from kms";
    $std=$conn->query($sl);
    $res=$std->fetchAll(PDO::FETCH_COLUMN,0);
      for($i=0;$i<count($res);$i++){
      echo " <option value=".$res[$i].">".$res[$i]."</option>";
    } ?>
    </select>
    <br> <br>
    <select name="caros">
    <option value="blank">&nbsp;&nbsp;&nbsp;--  Carosserie  --&nbsp;
    &nbsp;&nbsp;</option>
     <?php 
      $conn=new PDO('mysql:host=localhost;dbname=cars','root','');
    $sl= "SELECT distinct caro from carosserie";
    $std=$conn->query($sl);
    $res=$std->fetchAll(PDO::FETCH_COLUMN,0);
      for($i=0;$i<count($res);$i++){
      echo " <option value=".$res[$i].">".$res[$i]."</option>";
    } ?>
    </select>

    </select> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <select name="color">
    <option value="blank">&nbsp;&nbsp;&nbsp;&nbsp;--  Couleur  --   &nbsp;
     &nbsp;&nbsp;&nbsp;</option>
     <?php 
      $conn=new PDO('mysql:host=localhost;dbname=cars','root','');
    $sl= "SELECT distinct color from colors";
    $std=$conn->query($sl);
    $res=$std->fetchAll(PDO::FETCH_COLUMN,0);
      for($i=0;$i<count($res);$i++){
      echo " <option value=".$res[$i].">".$res[$i]."</option>";
    } ?>
    </select>
    <br><br>
    <select name="carburant">
      <option value="blank">&nbsp;&nbsp;&nbsp;-- Carburant -- &nbsp;&nbsp;
      &nbsp; </option>
      <option value="diesel">Diesel</option>
      <option value="electrique">Electrique</option>
       <option value="essence">Essence</option>
      <option value="gpl">GPL</option>
       <option value="hybrid">Hybrid</option>
    </select>&nbsp; &nbsp; &nbsp; &nbsp;

    <select name="trans">
      <option value="blank">-- Transmission --</option>
      <option value="auto">Automatique</option>
      <option value="manuel">Manuel</option>
       <option value="man">Manuel 6</option>
    </select>
    <br><br>
    <select name="cylindre">
      <option value="blank">-- Cylindrée du moteur --</option>
      <option value="1.4">1.4</option>
      <option value="1.5">1.5</option>
       <option value="1.6">1.6</option>
      <option value="2.0">2.0</option>
       <option value="2.2">2.2</option>
        <option value="2.5">2.5</option>
    </select> &nbsp;&nbsp;
    <select name="puissance">
      <option value="blank">-- Puissance Fiscale --</option>
      <?php 
      $conn=new PDO('mysql:host=localhost;dbname=cars','root','');
    $sl= "SELECT distinct puiss from pusissance";
    $std=$conn->query($sl);
    $res=$std->fetchAll(PDO::FETCH_COLUMN,0);
      for($i=0;$i<count($res);$i++){
      echo " <option value=".$res[$i].">".$res[$i]."</option>";
    } ?>
    </select>
    <br><br>
    <select name="douane">
       <option value="blank">--Etat douanier --</option>
      <option value="local">Achetée localement</option>
      <option value="etranger">Achetée à l'étranger</option>
    </select>
    <br><br>
    <label>Description de l'annonce:</label>
     <textarea id="texte" name="desc" cols="70" rows="8"></textarea>
    <br><br>
    <label>Prix:</label>
    <input type="text" name="prix">DH
    <br><br>
    <label>Photos:</label>
    <input name="photo" type="file" id="uploaded_file">
    <br><br>
    <input type="submit" name="continue" value="Continue">

    </fieldset>
    </form>
    </div>

    <div id="tab2" class="tabcontent">
      <h3>Paris</h3>
      <p>Paris is the capital of France.</p> 
    </div>
    <script>
    function openCity(evt, cityName) {
        var i, tabcontent, tablinks;
        tabcontent = document.getElementsByClassName("tabcontent");
        for (i = 0; i < tabcontent.length; i++) {
            tabcontent[i].style.display = "none";
        }
        tablinks = document.getElementsByClassName("tablinks");
        for (i = 0; i < tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" active","");
        }
        document.getElementById(cityName).style.display = "block";
        evt.currentTarget.className += " active";
    }
    </script>

    </body>
    </html> 
    <?php
    $conn=new PDO('mysql:host=localhost;dbname=cars','root','');

    if ( isset($_POST['name']) && isset($_POST['email']) &&
    isset($_POST['type']) && isset($_POST['tel']) && isset($_POST['ville'])
     && isset($_POST['categ']) && isset($_POST['marque']) && 
     isset($_POST['year']) && isset($_POST['km']) && isset($_POST['caros'])
     && isset($_POST['color']) && isset($_POST['carburant'])&& 
     isset($_POST['trans']) && isset($_POST['cylindre']) && 
     isset($_POST['puissance']) && isset($_POST['douane']) && 
     isset($_POST['desc']) && isset($_POST['prix'])&& isset($_POST['photo'])
     && isset($_POST['continue'])){

    $sql= "INSERT INTO owner VALUES(
                '".($_POST['type'])."',
                '".$_POST['name']."',
                '".$_POST['email']."',
                '".$_POST['tel']."',
                '".$_POST['ville']."',
                '".$_POST['categ']."',
                '".$_POST['marque']."',
                '".$_POST['year']."',
                '".$_POST['km']."',
                '".$_POST['caros']."',
                '".$_POST['color']."',
                '".$_POST['carburant']."',
                '".$_POST['trans']."',
                '".$_POST['cylindre']."',
                '".$_POST['puissance']."',
                '".$_POST['douane']."',
                '".$_POST['desc']."',
                '".$_POST['prix']."',
                 '".$_POST['photo']."',')";

      $res=$conn->query($sql);
    }
    ?>

i have been trying to insert these informations in my database but nothing gets inserted please help (btw i'm still a beginner) im trying to insert the information that the user will fill in the form in my database, when i click on submit the page is blank but when i check my database my informations aren't added

  • You can't just use `` and expect `$_POST['photo']` to work like that, do some more research into file handling in PHP. In addition, you only need one `$conn=new PDO('mysql:host=localhost;dbname=cars','root','');` and I suggest moving the database insert code to before your page output. – Scuzzy Mar 23 '17 at 01:30
  • @scuzzy i tried using $_FILES['photo'] but it still doesn't work – Yvonne Styles Mar 23 '17 at 01:31
  • I'm not surprised really, you're not showing a clear grasp of http://php.net/manual/en/features.file-upload.post-method.php `$_FILES["photo"]["tmp_name"]` will be the path to the temporarily uploaded file. You will need to do something with this file, ie store it somewhere else or read it's contents. – Scuzzy Mar 23 '17 at 01:32
  • Are you trying to store the entire contents of the file in your database or just a path reference? if you used `$_FILES['photo']` you would probably see the word `Array` in your database column. – Scuzzy Mar 23 '17 at 01:35
  • i tried it before but nothing gets inserted in my database, and im trying to store the full image in my database – Yvonne Styles Mar 23 '17 at 01:45
  • Please read [How to ask a good question](http://stackoverflow.com/help/how-to-ask) and [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [Minimal, Complete and Verifiable example](http://stackoverflow.com/help/mcve) – RiggsFolly Mar 23 '17 at 02:05
  • Start here [When all else fails Read The Manual](http://php.net/manual/en/features.file-upload.post-method.php) – RiggsFolly Mar 23 '17 at 02:07
  • Please include only the important part of your code in your post. Check also - [ask] – Logan Wayne Mar 23 '17 at 02:23
  • Try to see what the $sql variable is by "echo $sql;". Then try to execute this sql directly in the database. I think the last coma in '".$_POST['photo']."',')"; is a problem. I think it should be '".$_POST['photo']."')"; – blokeish Mar 23 '17 at 03:47
  • Please advise if my answer hasn't been helpful. If it has been helpful, please upvote; you can mark it as accepted if you feel it solved your problem. See also http://stackoverflow.com/help/someone-answers – miken32 Mar 24 '17 at 04:58

1 Answers1

0

The answer to your problem, as has been pointed out, is that file uploads are found in $_FILES but there are a ton of other problems with this code.

Mixing PHP and HTML like this is asking for trouble. It makes updates and debugging much more difficult than it needs to be. Using alternative syntax and short echo tags makes this much more seamless. In addition, you need to escape output before inserting it into HTML.

You don't need to create a new database object every time you want to talk to the database. This is adding a whole load of overhead to your program. Do all your database queries on the same object. Also do it at the top of your file, to keep it separated as mentioned above.

isset() will always return true in this case, there's no point in checking it. Also you must escape user input before putting it in a database. Use prepared statements to do this.

Here's how I would start on this file:

<?php
$conn=new PDO('mysql:host=localhost;dbname=cars','root','');
$sl = "SELECT distinct Ville from villes";
$std = $conn->query($sl);
$villes = $std->fetchAll(PDO::FETCH_COLUMN,0);
$sl = "SELECT distinct carbrand from car";
$std = $conn->query($sl);
$marques = $std->fetchAll(PDO::FETCH_COLUMN,0);
$sl = "SELECT distinct year from years";
$std = $conn->query($sl);
$annees = $std->fetchAll(PDO::FETCH_COLUMN,0);
$sl = "SELECT distinct km from kms";
$std = $conn->query($sl);
$kms = $std->fetchAll(PDO::FETCH_COLUMN,0);
$sl = "SELECT distinct caro from carosserie";
$std = $conn->query($sl);
$caros = $std->fetchAll(PDO::FETCH_COLUMN,0);
$sl = "SELECT distinct color from colors";
$std = $conn->query($sl);
$couleurs = $std->fetchAll(PDO::FETCH_COLUMN,0);
$sl = "SELECT distinct puiss from pusissance";
$std = $conn->query($sl);
$puissances = $std->fetchAll(PDO::FETCH_COLUMN,0);

if (isset($_POST)) {
    $photo = file_get_contents($_FILES["photo"]["tmp_name"]);
    $sql= "INSERT INTO owner VALUES(:type, :name, :email, :tel, :ville, :categ, :marque, :year, :km, :caros, :carburant, :trans, :cylindre, :puissance, :douane, :desc, :prix, :photo)";
    $stmt = $con->prepare($sql);
    $stmt->execute(array(
        ":type" => $_POST['type'],
        ":name" => $_POST['name'],
        ":email" => $_POST['email'],
        ":tel" => $_POST['tel'],
        ":ville" => $_POST['ville'],
        ":categ" => $_POST['categ'],
        ":marque" => $_POST['marque'],
        ":year" => $_POST['year'],
        ":km" => $_POST['km'],
        ":caros" => $_POST['caros'],
        ":color" => $_POST['color'],
        ":carburant" => $_POST['carburant'],
        ":trans" => $_POST['trans'],
        ":cylindre" => $_POST['cylindre'],
        ":puissance" => $_POST['puissance'],
        ":douane" => $_POST['douane'],
        ":desc" => $_POST['desc'],
        ":prix" => $_POST['prix'],
        ":photo" => $photo
    ));
}
?>
<!doctype html>
<html>
<head>
<style>
body {font-family: "Lato", sans-serif;}

/* Style the tab */
div.tab {
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #f1f1f1;
}
fieldset{
  padding-left: 10px;

  margin-left: 360px;
  margin-right: 380px;
}
/* Style the buttons inside the tab */
div.tab button {
    background-color: inherit;
    float: left;
    border: none;
    outline: none;
    cursor: pointer;
    padding: 14px 16px;
    transition: 0.3s;
    font-size: 17px;
}

/* Change background color of buttons on hover */
div.tab button:hover {
    background-color: #ddd;
}

/* Create an active/current tablink class */
div.tab button.active {
    background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
    display: none;
    padding: 6px 12px;
    -webkit-animation: fadeEffect 1s;
    animation: fadeEffect 1s;
}

/* Fade in tabs */
@-webkit-keyframes fadeEffect {
    from {opacity: 0;}
    to {opacity: 1;}
}

@keyframes fadeEffect {
    from {opacity: 0;}
    to {opacity: 1;}
}
</style>

</head>
<body>


<div class="tab">
  <button class="tablinks" onclick="openCity(event, 'tab1')">
 Déposez votre annonce</button>
  <button class="tablinks" onclick="openCity(event, 'tab2')">
 Annonces en ligne</button>
</div>

<div id="tab1" class="tabcontent">
    <fieldset>
<form method="post" action="form.php" enctype='multipart/form-data'>

<input type="radio" name="type" value="Particulier">Particulier
<input type="radio" name="type" value="Professionnel">Professionnel
<br><br>
<label>Nom:</label>
<input type="text" name="name">
<br><br>
<label>Email:</label>
<input type="email" name="email">
<br><br>
<label>Téléphone:</label>
<input type="text" name="tel" required>
<br><br>
<label>Ville:</label>
<select name="ville">
  <option value="blank">« Sélectionnez ville »</option>
<?php foreach($villes as $ville):?>
  <option><?=htmlspecialchars($ville)?></option>
<?php endforeach?>
</select>
<br><br>
<label>Catégorie:</label>
<select name="categ">
  <option value="blank">&nbsp;« Type de voiture » &nbsp; </option>
  <option value="occasion">Voitures occasion</option>
   <option value="neuf">Voitures neuves</option>
  </select>
   <br><br>
  <label>Sous-catégorie:</label>
  <select name="marque">
  <option value="blank">&nbsp; &nbsp;--  Marque  -- &nbsp; &nbsp;</option>
 <?php foreach($marques as $marque):?>
  <option><?=htmlspecialchars($marque)?></option>
 <?php endforeach?>
</select>
<br><br>
<select name="year">
<option value="blank">&nbsp;&nbsp;--  Année  --&nbsp;&nbsp;</option>
 <?php foreach($annees as $annee):?>
  <option><?=htmlspecialchars($annee)?></option>
<?php endforeach?>
</select> &nbsp;
<select name="km">
<option value="blank">&nbsp;&nbsp;--  Kilométrage  --&nbsp;&nbsp;
</option>
 <?php foreach($kms as $km):?>
  <option><?=htmlspecialchars($km)?></option>
 <?php endforeach?>
</select>
<br> <br>
<select name="caros">
<option value="blank">&nbsp;&nbsp;&nbsp;--  Carosserie  --&nbsp;
&nbsp;&nbsp;</option>
 <?php foreach($caros as $caro):?>
 <option><?=htmlspecialchars($caro)?></option>
 <?php endforeach?>
</select>

</select> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<select name="color">
<option value="blank">&nbsp;&nbsp;&nbsp;&nbsp;--  Couleur  --   &nbsp;
 &nbsp;&nbsp;&nbsp;</option>
 <?php foreach($couleurs as $couleur):?>
  <option><?=htmlspecialchars($couleur)?></option>
 <?php endforeach?>
</select>
<br><br>
<select name="carburant">
  <option value="blank">&nbsp;&nbsp;&nbsp;-- Carburant -- &nbsp;&nbsp;
  &nbsp; </option>
  <option value="diesel">Diesel</option>
  <option value="electrique">Electrique</option>
   <option value="essence">Essence</option>
  <option value="gpl">GPL</option>
   <option value="hybrid">Hybrid</option>
</select>&nbsp; &nbsp; &nbsp; &nbsp;

<select name="trans">
  <option value="blank">-- Transmission --</option>
  <option value="auto">Automatique</option>
  <option value="manuel">Manuel</option>
   <option value="man">Manuel 6</option>
</select>
<br><br>
<select name="cylindre">
  <option value="blank">-- Cylindrée du moteur --</option>
  <option value="1.4">1.4</option>
  <option value="1.5">1.5</option>
   <option value="1.6">1.6</option>
  <option value="2.0">2.0</option>
   <option value="2.2">2.2</option>
    <option value="2.5">2.5</option>
</select> &nbsp;&nbsp;
<select name="puissance">
  <option value="blank">-- Puissance Fiscale --</option>
  <?php foreach($puissances as $puissance):?>
  <option><?=htmlspecialchars($puissance)?></option>
  <?php endforeach?>
</select>
<br><br>
<select name="douane">
   <option value="blank">--Etat douanier --</option>
  <option value="local">Achetée localement</option>
  <option value="etranger">Achetée à l'étranger</option>
</select>
<br><br>
<label>Description de l'annonce:</label>
 <textarea id="texte" name="desc" cols="70" rows="8"></textarea>
<br><br>
<label>Prix:</label>
<input type="text" name="prix">DH
<br><br>
<label>Photos:</label>
<input name="photo" type="file" id="uploaded_file">
<br><br>
<input type="submit" name="continue" value="Continue">

</fieldset>
</form>
</div>

<div id="tab2" class="tabcontent">
  <h3>Paris</h3>
  <p>Paris is the capital of France.</p> 
</div>
<script>
function openCity(evt, cityName) {
    var i, tabcontent, tablinks;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active","");
    }
    document.getElementById(cityName).style.display = "block";
    evt.currentTarget.className += " active";
}
</script>

</body>
</html> 
miken32
  • 42,008
  • 16
  • 111
  • 154