php - Inserting multiple image names into database using an array -
php - Inserting multiple image names into database using an array -
i have code - check below. code works fine realised adds files uploads folder , doesn't add together database. can help me fill out blank? i'd utilize array , have simple possible.
please scroll downwards comment code executed , image goes folder.
uploader.php
<?php if (isset($_post['submit'])) { $j = 0; // variable indexing uploaded image. $target_path = "uploads/test/"; // declaring path uploaded images. ($i = 0; $i < count($_files['file']['name']); $i++) { // loop individual element array $validextensions = array("jpeg", "jpg", "png"); // extensions allowed. $ext = explode('.', basename($_files['file']['name'][$i])); // explode file name dot(.) $file_extension = end($ext); // store extensions in variable. $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1]; // set target path new name of image. $j = $j + 1; // increment number of uploaded images according files in array. if (($_files["file"]["size"][$i] < 100000) // approx. 100kb files can uploaded. && in_array($file_extension, $validextensions)) { if (move_uploaded_file($_files['file']['tmp_name'][$i], $target_path)) { // if file moved uploads folder. ?> <div id="noerror">image <?php echo $j;?>-->image uploaded!</div> <?php // file moved, execute code here // in code add together file name db } else { // if file not moved. ?> <div id="error">image <?php echo $j;?>--> <b>please seek again!</b></div> <?php } } else { // if file size , file type incorrect. ?> <div id="error">image <?php echo $j;?>--> <b>check file size or type</b></div> <?php } } } ?>
are looking this:
please add together database field names
, database table name
in below query:
mysqli_query("insert `images` set `image_name` = '".$_files['file']['name'][$i]."'") or die (mysqli_error());
your code should now:
<?php if (isset($_post['submit'])) { $j = 0; // variable indexing uploaded image. $target_path = "uploads/test/"; // declaring path uploaded images. ($i = 0; $i < count($_files['file']['name']); $i++) { // loop individual element array $validextensions = array("jpeg", "jpg", "png"); // extensions allowed. $ext = explode('.', basename($_files['file']['name'][$i])); // explode file name dot(.) $file_extension = end($ext); // store extensions in variable. $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1]; // set target path new name of image. $j = $j + 1; // increment number of uploaded images according files in array. if (($_files["file"]["size"][$i] < 100000) // approx. 100kb files can uploaded. && in_array($file_extension, $validextensions)) { if (move_uploaded_file($_files['file']['tmp_name'][$i], $target_path)) { // if file moved uploads folder. ?> <div id="noerror">image <?php echo $j;?>-->image uploaded!</div> <?php // file moved, execute code here mysqli_query("insert `images` set `image_name` = '".$_files['file']['name'][$i]."'") or die (mysqli_error()); // in code add together file name db } else { // if file not moved. ?> <div id="error">image <?php echo $j;?>--> <b>please seek again!</b></div> <?php } } else { // if file size , file type incorrect. ?> <div id="error">image <?php echo $j;?>--> <b>check file size or type</b></div> <?php } } } ?>
php arrays database mysqli
Comments
Post a Comment