1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| <?php
$dsn = 'mysql:dbname=tuto;host=127.0.0.1';
$user = 'root';
$password = '';
try {
$connexion = new PDO($dsn, $user, $password);
} catch( PDOException $e) {
throw new PDOException('La connexion à la base de données à échoué !');
}
?>
<form name="form1" action="" method="post" enctype="multipart/form-data">
<table border="1">
<tbody>
<tr>
<th>Nom du produit</th>
<td><input type="text" name="pnm"></td>
</tr>
<tr>
<th>Prix du produit</th>
<td><input type="text" name="pprice"></td>
</tr>
<tr>
<th>Image du produit</th>
<td><input type="file" name="pimage"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit1" value="upload"></td>
</td>
</tr>
</tbody>
</table>
</form>
<?php
if (isset($_POST['submit1'])) {
$v1 = rand(1111, 9999);
$v2 = rand(1111, 9999);
$v3 = md5($v1 . $v2);
$fnm = $_FILES['pimage']['name'];
$dst = 'product_image/' . $v3 . $fnm;
move_uploaded_file($_FILES['pimage']['name'], $dst);
$requete = $connexion->prepare("INSERT INTO products (name, price, image) VALUES(:name, :price, :image);"); // on prépare notre requête
$reponse = $requete->execute([
'name' => $_POST['pnm'],
'price' => (float) $_POST['pprice'],
'image' => $dst
]); // on execute la requête en lui fournissant les données
if($reponse)
{
echo '<font color="red">Vous avez ajouté un produit</font>';
}
} |