使用php在数据库中插入照片

时间:2017-03-23 01:28:03

标签: php mysql phpmyadmin

    <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);
    }
    ?>

我一直在尝试在我的数据库中插入这些信息,但没有插入任何内容请帮助(顺便说一句,我还是初学者) 我试图插入用户将填写表格在我的数据库中的信息,当我点击提交页面是空白但当我检查我的数据库我的信息没有添加

1 个答案:

答案 0 :(得分:0)

正如已经指出的那样,问题的答案是file uploads are found in $_FILES,但此代码存在大量其他问题。

像这样混合使用PHP和HTML会让人感到麻烦。它使更新和调试变得更加困难。使用alternative syntaxshort echo tags可以使这更加无缝。此外,在将其插入HTML之前,您需要escape output

每次要与数据库通信时,都不需要创建新的数据库对象。这会给您的程序增加一大笔开销。对同一对象执行所有数据库查询。也可以在文件顶部进行操作,以便按上述方法将其分开。

在这种情况下,

isset()将始终返回true,检查它是没有意义的。此外,您必须在将用户输入放入数据库之前将其转义。使用prepared statements执行此操作。

以下是我将如何开始使用此文件:

<?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>