I wrote a upload script on my site where users can upload a predefined number of items (12 in my case). A user can edit his project (of course...).
So I also wrote an update-script. Project are read from the database and a user can reupload or delete certain files. But what I have write know is, when a users doesn't upload a new file, the original file is overwritten by an empty value (makes sense, since the <input> is empty.
But I want to prevent that. How do I do this?
Form:
$activities = Project::getProjectById($_DB, $projectnumber);
if(!empty($activities)) {
foreach($activities as $k => $v) {
echo "<input type='text' value='".$v['name']."' name = 'newproject_name' />";
$i = 1;
while ($i <= 12) {
if(!empty($v["photo$i"])) {
echo "<img src='../files/".$v["photo$i"]."' width='200' height='200' />";
echo "<input type='file' name='images[]' />";
}
$i++;
}
}
echo "<input type=submit value='Update' name='submitted'>";
echo "</form>";
}
PHP:
$project = new Project();
$project->name = $_POST['newproject_name'];
$project->photo1 = $_FILES['images']['name'][0];
$project->photo2 = $_FILES['images']['name'][1];
$project->photo3 = $_FILES['images']['name'][2];
$project->photo4 = $_FILES['images']['name'][3];
$project->photo5 = $_FILES['images']['name'][4];
$project->photo6 = $_FILES['images']['name'][5];
$project->photo7 = $_FILES['images']['name'][6];
$project->photo8 = $_FILES['images']['name'][7];
$project->photo9 = $_FILES['images']['name'][8];
$project->photo10 = $_FILES['images']['name'][9];
$project->photo11 = $_FILES['images']['name'][10];
$project->photo12 = $_FILES['images']['name'][11];
if($project->updateProject($_DB, $projectnummer)) {
$feedback = "<div class='feedback good'>ok!!</div>";
} else {
$feedback = "<div class='feedback bad'>NOT ok!</div>";
}
You don't have to worry about the getProjectById and updateProject-function, they work just fine. But as you can see, the script will upload all the empty values and overwrite them in the database...
I would like to prevent that! Any advice?
EDIT:
if (!empty($_FILES['images']['name'][0])) {
$project->photo1 = $_FILES['images']['name'][0];
}
if (!empty($_FILES['images']['name'][1])) {
$project->photo2 = $_FILES['images']['name'][1];
}
if (!empty($_FILES['images']['name'][2])) {
$project->photo3 = $_FILES['images']['name'][2];
}
if (!empty($_FILES['images']['name'][3])) {
$project->photo4 = $_FILES['images']['name'][3];
}
if (!empty($_FILES['images']['name'][4])) {
$project->photo5 = $_FILES['images']['name'][4];
}
if (!empty($_FILES['images']['name'][5])) {
$project->photo6 = $_FILES['images']['name'][5];
}
if (!empty($_FILES['images']['name'][6])) {
$project->photo7 = $_FILES['images']['name'][6];
}
if (!empty($_FILES['images']['name'][7])) {
$project->photo8 = $_FILES['images']['name'][7];
}
if (!empty($_FILES['images']['name'][8])) {
$project->photo9 = $_FILES['images']['name'][8];
}
if (!empty($_FILES['images']['name'][9])) {
$project->photo10 = $_FILES['images']['name'][9];
}
if (!empty($_FILES['images']['name'][10])) {
$project->photo11 = $_FILES['images']['name'][10];
}
if (!empty($_FILES['images']['name'][11])) {
$project->photo12 = $_FILES['images']['name'][11];
}
$project->photo1 = $_FILES['images']['name'][0];
$project->photo2 = $_FILES['images']['name'][1];
$project->photo3 = $_FILES['images']['name'][2];
$project->photo4 = $_FILES['images']['name'][3];
$project->photo5 = $_FILES['images']['name'][4];
$project->photo6 = $_FILES['images']['name'][5];
$project->photo7 = $_FILES['images']['name'][6];
$project->photo8 = $_FILES['images']['name'][7];
$project->photo9 = $_FILES['images']['name'][8];
$project->photo10 = $_FILES['images']['name'][9];
$project->photo11 = $_FILES['images']['name'][10];
$project->photo12 = $_FILES['images']['name'][11];
can be rewriten as
for ($i = 0; $i <=11; $i++) {
$photo = 'photo' . ($i + 1);
$project->$photo = $_FILES['images']['name'][$i];
}
And you can resume all your edit with:
for ($i = 0; $i <=11; $i++) {
$photo = 'photo' . ($i + 1);
if ( ! empty($_FILES['images']['name'][$i])) {
$project->$photo = $_FILES['images']['name'][$1];
}
}
Maybe You should use is_uploaded_file() function (LINK)?
EDIT:
for($i = 0; $i <= 11; $i++) {
if(!is_uploaded_file($_FILES['images']['name'][$i]))
$project->photo[$i] = $_FILES['images']['name'][$i];
}
Related
I am unable to enter multiple data, it enter only single data. I have tried using for loop and then entering data, using 3 user and 2 task, there is an error previously offset.
public function add($postData)
{
// dd($postData);
$c = count($postData['user_name']);
$t = count($postData['task_name']);
for ($i = 0; $i < $c; $i++) {
$user_name = $postData['user_name'][$i];
$user_email = $postData['user_email'][$i];
$data['insert']['user_name'] = $user_name;
$data['insert']['user_email'] = $user_email;
}
for ($j = 0; $j < $t; $j++) {
$task_name = $postData['task_name'][$j];
$data['insert']['task_name'] = $task_name;
}
$data['insert']['name'] = $postData['name'];
$data['insert']['description'] = $postData['description'];
$data['insert']['customer_name'] = $postData['customer_name'];
$data['insert']['billing_method'] = $postData['billing_method'];
$data['insert']['dt_created'] = DT;
$data['table'] = PROJECT;
$result = $this->insertRecord($data);
if ($result == true) {
$response['status'] = 'success';
$response['message'] = 'Project created';
} else {
$response['status'] = 'danger';
$response['message'] = DEFAULT_MESSAGE;
}
return $response;
}
As Per lack of question details attached, I am supposing that you want to insert multiple task entry with project name, description etc.
Here is updated code:
<?php
// dd($postData);
$username = $postData['username'];
$user_email = $postData['user_email'];
$task_name = $postData['task_name'];
foreach ($username as $key => $value) {
$data['insert']['name'] = $postData['name'];
$data['insert']['description'] = $postData['description'];
$data['insert']['customer_name'] = $postData['customer_name'];
$data['insert']['billing_method'] = $postData['billing_method'];
$data['insert']['username'] = $value;
$data['insert']['user_email'] = $user_email[$key];
$data['insert']['task_name'] = $task_name[$key];
$data['insert']['dt_created'] = DT;
$data['table'] = PROJECT;
$result = $this->insertRecord($data);
}
How to delete multiple images in PHP.But Single product delete is working fine.
if (isset($_REQUEST['delete_product'])) {
$ca_in_id = $_REQUEST['delete_product'];
$where = array("ca_in_id" => $ca_in_id);
$data = $this->select_where('catalog_inventory', $where);
//$res = $data->fetch_object();
$result = $this->delete_where('catalog_inventory', $where);
if ($result) {
// (file_exists($pic1)) {
// print_r($pic1);
// exit();
while ($row = mysqli_fetch_array($data)) {
$image = $row["upload_file"];
$pic1 = ("$image");
unlink('product_images/' . $pic1);
print_r($pic1);
}
exit();
//}
?>
<script>
alert("Delete Sucess");
window.location = "list-product";
</script>
<?php
} else {
?>
<script>
alert("Delete Fails");
window.location = "list-product";
</script>
<?php
}
}
And multiple Image delete show this error :-
How to solve this issues?
THANKS.
I have Solve issues First get all values in database and using explode function use the count database columns values and use finally for loop.
This My Code:-
This code means is get values in database.
$data = $this->select_where('catalog_inventory', $where);
$res = $data->fetch_object();
$image = $res->upload_file;
$allimages = explode(",", $image);
$countallimages = count($allimages);
After get multiple values and values run the looping
for ($i = 0; $i < $countallimages; $i++) {
if (file_exists("product_images/" . $allimages[$i]) == false) {
echo "error ";
exit();
}
}
for ($i = 0; $i < $countallimages; $i++) {
$path = "product_images/" . $allimages[$i];
if (empty($path)) {
} else {
unlink($path);
}
}
Thanks ...
I am working on an android app that should create some records on a MySQL database.
This is the PHP file that receives the POST values from the app.
As you may see, there are three arrays that collect specific POST.
The first and second loops are working fine and executing the related guardar_post_media() function.
But the third loop is not executed and there are real values on the variables inside the third array.
May be there is something wrong that I canĀ“t detect, may be you can.
<?php
require_once '../mis_php_functions/funciones_basicas.php';
if($_SERVER['REQUEST_METHOD']=='POST'){
$val39 = $_POST['val39'];
$val40 = $_POST['val40'];
$val46 = $_POST['val46'];
$val48 = $_POST['val48'];
$val50 = $_POST['val50'];
$val52 = $_POST['val52'];
$val54 = $_POST['val54'];
$val56 = $_POST['val56'];
$val58 = $_POST['val58'];
$val60 = $_POST['val60'];
$val62 = $_POST['val62'];
$val64 = $_POST['val64'];
$val65 = $_POST['val65'];
$val67 = $_POST['val67'];
$val69 = $_POST['val69'];
$val71 = $_POST['val71'];
$val73 = $_POST['val73'];
$val75 = $_POST['val75'];
$val77 = $_POST['val77'];
$val79 = $_POST['val79'];
$val81 = $_POST['val81'];
$val82 = $_POST['val82'];
$val83 = $_POST['val83'];
$val84 = $_POST['val84'];
$val85 = $_POST['val85'];
$val86 = $_POST['val86'];
$val87 = $_POST['val87'];
$val88 = $_POST['val88'];
$val89 = $_POST['val89'];
$val100 = $_POST['val100'];
$val101 = $_POST['val101'];
$val102 = $_POST['val102'];
$val103 = $_POST['val103'];
$val104 = $_POST['val104'];
$status = 1;
$post = guardar_post($val40,$val39,$val100,$val102,$status,$val103);
if ($post != false) {
$fotos = array($val48,$val50,$val52,$val54,$val56,$val58,$val60,$val62,$val64);
$arrayLength = count($fotos);
echo "Numero de fotos ".$arrayLength;
$i = 0;
while ($i < $arrayLength)
{
if ($fotos[$i] == 0){
}
else{
guardar_post_media(1,$fotos[$i],$val102,$val100,$post);
}
echo "<br />".$fotos[$i] ."<br />";
$i++;
}
$videos = array($val67,$val69,$val71,$val73,$val75,$val77,$val79,$val81,$val83);
$arrayLength2 = count($videos);
echo "Numero de videos ".$arrayLength2;
$i = 0;
while ($i < $arrayLength2)
{
if ($videos[$i] == 0){
}
else{
guardar_post_media(2,$videos[$i],$val102,$val100,$post);
}
echo "<br />".$videos[$i] ."<br />";
$i++;
}
$youtube = array($val85,$val86,$val87,$val88,$val89);
$arrayLength3 = count($youtube);
echo "Numero de youtube ".$arrayLength3;
$i = 0;
while ($i < $arrayLength3)
{
if ($youtube[$i] == 0){
}
else{
guardar_post_media(3,$youtube[$i],$val102,$val100,$post);
}
echo "<br />".$youtube[$i] ."<br />";
$i++;
}
sendMessageNuevoPost($val39,$val102,$val103,$val104); // envio de push
}
else{
echo 'error';
}
}
?>
You have:
if ($youtube[$i] == 0){
}
else {
}
But POST vars are all strings. Change to:
if ($youtube[$i] == "0"){
}
else {
}
In otherwords, an equality on a string to numerical 0 will be true in your cases. And thus your else never executes.
*** Edit. PROOF
$test1 = "filename.dat";
$test2 = "2939";
$test3 = "some useful data";
$test4 = "0";
if ($test1 == 0) {
// Dont do anything
}
else {
echo "Do Work 1.";
}
if ($test2 == 0) {
// Dont do anything
}
else {
echo "Do Work 2.";
}
if ($test3 == 0) {
// Dont do anything
}
else {
echo "Do Work 3.";
}
if ($test4 == 0) {
// Dont do anything
}
else {
echo "Do Work 4.";
}
Only echoes out Do Work 2.. All other echo()s are not run because the equality of a string to a number 0 will return true except in those cases where the string also is numerical, and then the interpreter will compare the numerical values.
As how this relates to the OP's question: It can be inferred that the OP's POST vars must contain some non-numerical data because the OP insists that the 3rd array is populated:
But the third loop is not executed and there are real values on the variables inside the third array.
I will add likely the loop is executed, but not giving the expected the results due the reasons so mentioned.
Tested on PHP 5 and 7.
Hello im trying to sort values (NUMERIC) from for loop, my code:
$result_itemid_count = count($result_itemid);
for($x = 0; $x < $result_itemid_count; $x++) {
echo $result_itemid[$x][0];
echo "<br />";
}
output:
172505254931 172505254931 172505254931 172505254931 172505254931
172505254931 172295676941 172295676941 172195402570 172195402570
172195402570 172295676941 172408848472 172195402570 172195402570
172593285746 172593285746 172593285746 172593285746 172263227044
172263227044 172195402570 172263227044 172408848472 172408848472
i have no idea how to sort it, i tried sort, usort, ksort
any ideas?
first loop:
if ($resp->ack == "Success") {
$results = '';
$result_itemid = array();
// If the response was loaded, parse it and build links
foreach($resp->searchResult->item as $item) {
$pic = $item->galleryURL;
$link = $item->viewItemURL;
$title = $item->title;
$location = $item->location;
$itemid = $item->itemId;
$multiv = $item->isMultiVariationListing;
if($multiv == "true")
{
$result_itemid[] = [$itemid, $title];
} else {
// For each SearchResultItem node, build a link and append it to $results
$results .= "<tr><td align = 'center'><img src=\"$pic\"></td><td>$title - $location</td></tr>";
}
}
}
// If the response does not indicate 'Success,' print an error
else {
$results = "<h3>Oops! The request was not successful. Make sure you are using a valid ";
$results .= "AppID for the Production environment.</h3>";
}
second loop:
$result_itemid_count = count($result_itemid);
for($x = 0; $x < $result_itemid_count; $x++) {
echo $result_itemid[$x][0];
echo "<br />";
}
I hope you expected like below
$result_itemid[0][0]=121223;
$result_itemid[1][0]=121883;
$result_itemid[2][0]=121203;
$result_itemid[3][0]=121213;
$result_itemid[4][0]=121235;
$new_array=[];
$result_itemid_count = count($result_itemid);
for($x = 0; $x < $result_itemid_count; $x++) {
echo $result_itemid[$x][0];
echo "<br />";
array_push($new_array, $result_itemid[$x][0]);
}
echo "<hr/>";
sort($new_array);
$result_count = count($new_array);
for($x = 0; $x < $result_count; $x++) {
echo $new_array[$x];
echo "<br>";
}
<input type="checkbox" name="f[]"value="sport">
<input type="checkbox" name="f[]"value="reading">
<input type="checkbox" name="f[]"value="arguments">
<input type="checkbox" name="f[]"value="tv">
and this is the php:
if(isset($_POST['f'])){
if(in_array("sport",$_POST['f'])){
$sport = $_POST['f'];
$sport = 1;}
if(in_array("reading",$_POST['f'])){
$reading = $_POST['f'];
$reading = 1;}
if(in_array("arguments",$_POST['f'])){
$argument = $_POST['f'];
$argument = 1;}
if(in_array("tv",$_POST['f'])){
$tv = $_POST['tv'];
$tv = 1;}
$problem = false;
} else{
$e = false;
$sport = 0;
$reading = 0;
$argument = 0;
$tv = 0;
}
so what is wrong? what should i do?problem comes when i won`t select any of those checkboxes! plus it dose not send to mysql it set record but does not show that! even the code says that if none of them were checked get them value of 0! and it must get the 0 in mysql but it refuse to do that....
Try this instead of your code,
if (isset($_POST['f'])) {
if (in_array("sport", $_POST['f'])) {
$sport = 1;
} else {
$sport = 0;
}
if (in_array("reading", $_POST['f'])) {
$reading = 1;
} else {
$reading = 0;
}
if (in_array("arguments", $_POST['f'])) {
$argument = 1;
} else {
$argument = 0;
}
if (in_array("tv", $_POST['f'])) {
$tv = 1;
} else {
$tv = 0;
}
$problem = false;
}