I want to update image in one by one.
Example : I want to update only IMG1.
img1 : img_id = 1, img_name = demo1.jpg
img2 : img_id = 2, img_name = demo2.jpg
When I press update button with only IMG1 it should send img_id = 1 to $id = $_POST['image_id']; But it send img_id = 2 Then will make IMG2 has been update not IMG1.
updateimage.php
<div class="col-md-4 col-sm-6 col-xs-12">
<div class="text-center">
<?php $result = mysql_query("SELECT img_id, img_name FROM fm_product_image WHERE p_id_img = '$ID'")or die(mysql_error()); ?>
<?php while ($imagerows = mysql_fetch_array($result)) { $imgid = $imagerows['img_id']; ?>
<img class="avatar img-circle img-thumbnail" style="border-radius: 2px; width: 70%;" src="upload/<?php echo $imagerows['img_name']; ?>">
<h6>Upload your product image.</h6>
<input type="file" name="image" class="text-center center-block well well-sm">
<input type="text" required value="<?php echo $imgid; ?>" name="image_id" class="text-center center-block well well-sm">
<button type="submit" name="image" class="btn btn-success" style="margin: auto;">Update</button>
<br>
<br>
<?php } ?>
</div>
</div>
<?php
$id = $_POST['image_id'];
if (isset($_POST['image'])) {
$image = $_FILES["image"]["name"];
$image_name= addslashes($_FILES['image']['name']);
$size = $_FILES["image"] ["size"];
move_uploaded_file($_FILES["image"]["tmp_name"],"upload/" . $_FILES["image"]["name"]);
$product_img = $_FILES["image"]["name"];
mysql_query(" UPDATE fm_product_image SET img_name = '$product_img' WHERE img_id = '$id'")or die(mysql_error());
header('location:product.php');
} ?>
The problem is in your loop. You are submitting the same form, and that form contains the text fields which each image's ID. So, even if you have ten images, the IDs for all ten will be submitted, and the server will most likely use the last ID, which would be 10, and update that one.
The solution is to create a form on each iteration of your loop, that way, when you hit the submit button, it submits only the form containing the ID of a given image. Here is a solution below:
<?php $result = mysql_query("SELECT img_id, img_name FROM fm_product_image WHERE p_id_img = '$ID'")or die(mysql_error()); ?>
<?php while ($imagerows = mysql_fetch_array($result)) : ?>
<?php $imgid = $imagerows['img_id']; ?>
<img class="avatar img-circle img-thumbnail" style="border-radius: 2px; width: 70%;" src="upload/<?php echo $imagerows['img_name']; ?>">
<h6>Upload your product image.</h6>
<form method="post" action="YOUR_ACTION_PAGE" enctype="multipart/form-data">
<input type="file" name="image" class="text-center center-block well well-sm">
<input type="text" required value="<?php echo $imgid; ?>" name="image_id" class="text-center center-block well well-sm">
<button type="submit" name="image" class="btn btn-success" style="margin: auto;">Update</button>
</form>
<br>
<br>
<?php endwhile; ?>
Notice I created a new form on each iteration through the loop.
I must also mention that you need to consider switching to the better and safer mysqli_* family of functions.
The problem is that you cannot have multiple inputs of type text / file with the same name attribute in the same form tag or in the DOM in there is no form tag around the inputs.
To solve this problem, right after you open the while, add a form with a different name (e.g. name of the image that you want updated) and right before closing the while loop, close the form tag.
EDIT:
This is what I was talking about.
<?php while ($imagerows = mysql_fetch_array($result)) { $imgid = $imagerows['img_id']; ?>
<form name="update_<?php echo $imagerows['img_name']; ?>_form" method="post">
<img class="avatar img-circle img-thumbnail" style="border-radius: 2px; width: 70%;" src="upload/<?php echo $imagerows['img_name']; ?>">
<h6>Upload your product image.</h6>
<input type="file" name="image" class="text-center center-block well well-sm">
<input type="text" required value="<?php echo $imgid; ?>" name="image_id" class="text-center center-block well well-sm">
<button type="submit" name="image" class="btn btn-success" style="margin: auto;">Update</button>
<br>
<br>
</form>
<?php } ?>
Related
Quick rundown:
Everything ran perfectly as it should when I had my own custom textarea field to send data
I had a custom text editor widget for inputs, but tried to add CKEditor for more functionality
When I added CKEditor package (https://ckeditor.com/ckeditor-4/download/?undefined-addons=) the editor was there, but when I clicked "Add" button and sent data, the input was doubled and there was a space between inputs (for example - input was:"test" and output was "test<br><br>test"), as if I put <br> tag and multiplied my input somewhere.
When I stopped trying with CKEditor, I went back to original code and my own original custom text editor, which before worked perfectly. However, to my surprise, it now still doubles the input string, but it doesn't put a <br> tag in between.
In DB the string value is normal and not doubled.
I have no idea how this happened, I've been going over the code for a hour or so now, trying to see if I overlooked something, but I even created a separate file with functioning code, before I was trying with CKEditor in case something broke and I could just replace it with old code and try tomorrow, but now it's messed up and I have no idea how and where.
notifications.php
<form class="form-inline" action="/action_page.php">
</form>
</nav>
<div class="container-fluid m-0 p-0">
<div class="row justify-content-center">
<div class="col-md-10">
<?php if (isset($_SESSION['response'])) { ?>
<div class="alert alert-<?= $_SESSION['res_type']; ?> alert-dismissible text-center">
<button type="button" class="close" data-dismiss="alert">×</button>
<b><?= $_SESSION['response']; ?></b>
</div>
<?php } unset($_SESSION['response']); ?>
</div>
</div>
<div class="row">
<div class="">
<?php
$query = 'SELECT * FROM crud';
$stmt = $conn->prepare($query);
$stmt->execute();
$result = $stmt->get_result();
?>
<table class="table table-hover" id="data-table">
<tbody>
<?php while ($row = $result->fetch_assoc()) { ?>
<tr>
<td></td>
<td class="pt-2"><?=
$longString=$row['name'];
$link = $row['id'];
$longStringshortcut = strlen($longString);
//echo substr($longString, 0, 100).'... Read More';
if ($longStringshortcut > 250) {
echo substr($longString, 0, 250).".. <a href='details.php?details=$link'><strong>Preberi več...</strong></a>"; }
else {
echo $longString;
}
?>
Details
Delete
Edit
</td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="col-md-4 p-0">
<h5 class="">Add notification:</h5>
<form action="action.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?= $id; ?>">
<div class="form-group">
<textarea name="name" value="<?= $name; ?>" class="form-control" placeholder="This is the default text" required></textarea>
</div>
<div class="form-group">
<?php if ($update == true) { ?>
<input type="submit" name="update" class="btn btn-success btn-block" value="Change notification">
<?php } else { ?>
<input type="submit" name="add" class="btn btn-primary btn-block" value="Add">
<?php } ?>
</div>
<div class="form-group">
<input type="hidden" name="oldimage" value="<?= $photo; ?>">
<input type="file" name="image" class="custom-file">
<img src="<?= $photo; ?>" width="120" class="img-thumbnail">
</div>
</form>
</div>
</div>
</div>
</div>
action.php
<?php
session_start();
include 'config.php';
$update=false;
$id="";
$name="";
$photo="";
if(isset($_POST['add'])){
$name=$_POST['name'];
$photo=$_FILES['image']['name'];
$upload="uploads/".$photo;
$query="INSERT INTO crud(name,photo)VALUES(?,?)";
$stmt=$conn->prepare($query);
$stmt->bind_param("ss",$name,$upload);
$stmt->execute();
move_uploaded_file($_FILES['image']['tmp_name'], $upload);
header('location:index.php');
$_SESSION['response']="Successfully Inserted to the database!";
$_SESSION['res_type']="success";
}
The problem was with the <?= syntax (echoing), <?php solved it.
I want to update my profile image but I don't know how to update it. I successfully insert it into my database and folder but when user wants to update it So I don't know how to update this image. Can you tell me how this is possible? Thanks.
Code of my HTML form
<form class="form" action="accountsetting.php" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-12 col-sm-auto mb-3">
<div class="mx-auto" style="width: 140px;">
<div class="rounded-circle avatar avatar-xl mb-3">
<img class="rounded-circle" id="preview_avatar" name="avatar" src="<?php echo
$_SESSION['user']['avatar']; ?>" width="100" height="100">
</div>
</div>
</div>
<div class="col d-flex flex-column flex-sm-row justify-content-between mb-3">
<div class="text-center text-sm-left mb-2 mb-sm-0">
<h4 class="pt-sm-2 pb-1 mb-0 text-nowrap"><?php echo $_SESSION['user']['fullname']; ?></h4>
<p class="mb-0"><?php echo $_SESSION['user']['username']; ?></p>
<div class="form-group mb-2 pt-2">
<input class='input' type='hidden' name='id' value="<?php echo $_SESSION['user']['id']; ?>" />
<input id="avatar" type="file" name="avatar" hidden="" accept="image/png, image/jpeg,
image/jpg">
<button id="uploadBtn" type="submit" name="profile" class="btn btn-primary btn-file " ><i
class="fa fa-camera" aria-hidden="true"></i>   Upload Avatar</button>
</div>
<div class="row">
<div class="col d-flex justify-content-end pr-5">
<button class="btn btn-primary " type="submit" name="profileupdate">Save Changes</button>
</div>
</div>
</div>
</div>
</div>
</form>
Code of my PHP side
// sava image into directoray
$id="";
$filename = $avatar['name'];
$fileerror = $avatar['error'];
$filetmp = $avatar['tmp_name'];
$fileext = explode('.',$filename);
$filecheck = strtolower(end($fileext));
$fileextstored =array('png', 'jpg', 'jpeg');
$destinationfile = 'upload/';
$destinationfile = $destinationfile .$filename;
$destinationfile1 = 'userside/upload/';
$destinationfile1 = $destinationfile1 .$filename;
try {
//throw exception if can't move the file
if (!move_uploaded_file($filetmp, $destinationfile)) {
throw new Exception('Could not move file');
}
if(!copy ( $destinationfile , $destinationfile1 ))
{
throw new Exception('Could not move 2nd file');
}
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
// image insert
if(in_array($filecheck, $fileextstored))
{
$query = "INSERT INTO users (avatar) VALUES('$destinationfile')";
$displayquery = "select * from users where id= $id";
$displayquery = mysqli_query($db,$displayquery);
}
What's type of code i will use. When the user click on the save button the image will successfully update.
It looks like you need a condition in your INSERT INTO statement. Shouldn't you insert the avatar in the users table where id = $id?
Here:
"INSERT INTO users (avatar) VALUES('$destinationfile')";
Change to:
"INSERT INTO users (avatar) VALUES('$destinationfile') WHERE id = $id";
Update: if you're inserting the path to the image, then maybe you need to put the URL in with the image tag:
<!-- insert code for http or https:// -->
<img class="rounded-circle" id="preview_avatar" name="avatar" src="<?php echo "http://" . $_SERVER['SERVER_NAME'] . "/" . $_SESSION['user']['avatar']; ?>" width="100" height="100">
This relies on that path being valid and accessible from the server name, like http://localhost/uploads/image_name.jpg if the path is .../uploads/image_name.jpg. You can put the path in the session variable, then just add the image name on the end, e.g. $_SESSION['image_path'] = 'http://localhost/uploads/'.
I having a problem getting image and store them in database
The problem is it's showing that pimage is Undefined and the image I choose is not storing in the database
This is the Image when I try to submit it to show you what the problem is
This is my database look like
It's not storing my image
$fol = $_POST['folder'];
$lin = $_POST['link'];
$target_dir="image/";
$target_file=$target_dir.basename($_FILES['pimage']['name']);
move_uploaded_file($_FILES['pimage']['tmp_name'],$target_file);
if(empty($fol)){
echo "<div class='alert-danger'><i class='fa fa-exclamation-circle'>
<strong>Please Enter Folder Name</strong>
</i></div>";
}
if(empty($lin)) {
echo "<div class='alert-danger'><i class='fa fa-exclamation-circle'>
<strong>Please Enter Link</strong>
</i></div>";
}
else{
$sql="SELECT id FROM filelink WHERE folder_name ='$fol' LIMIT 1";
$check_query =mysqli_query($conn,$sql);
$count_folder = mysqli_num_rows($check_query);
if($count_folder>0){
echo "<div class='alert-danger'><i class='fa fa-exclamation-circle'>
<strong>Sorry Folder Name Already Exist</strong>
</i></div>";
}
else{
$sql ="INSERT INTO filelink(link,folder_name,folder_image)VALUES('$lin','$fol','$target_file')";
$run_query=mysqli_query($conn,$sql);
if($run_query){
echo "<div class='alert-success'>
<strong>Add File Successful Refresh Page to see Change</strong></i>
</div>";
}
}
}
<div class="form-popup" id="myForm">
<form action="/action_page.php" enctype="multipart/form-data" class="form-container">
<div id="e_msg" style="font-size: 13; text-align: center; margin-bottom: 10px; margin-top: 15px;">
<!-- message for adding file if success-->
</div>
<label for="folder"><b>Folder</b></label>
<input type="text" placeholder="Enter Folder Name" name="folder" required>
<label for="link"><b>Link</b></label>
<input type="text" placeholder="Enter Link" name="link" required>
<div class="custom-file">
<input type="file" name="pimage" class="custom-file-input" id="customFile" required>
<label for ="customFile" class="custom-file-label">Choose Product Image</label>
</div>
<button style="margin-top: 15px;" type="submit" class="btn btnsub">Submit</button>
Close
</form>
</div>
Hi guys please help me out, I have this php code where I get data from the database and displays on the web and if you check the input hidden tag below I gave it a value but instead not all the values are displaying properly in it. Instead it's creating an attribute.
PHP CODE
<?php
$output = '';
$sqlNO = "SELECT * FROM pnewoffer";
$NOresult = mysqli_query($conn, $sqlNO);
if(mysqli_num_rows($NOresult) > 0) {
while($row = mysqli_fetch_array($NOresult)) {
$output .= '<div class="offer-card">
<form action="./bked/savedit.php" method="post">
<a href="#" class="offer-card-inner">
<div class="offer-img">
<img src="./image/'.$row['offerPImage'].'" alt="New Offer Image" width="200">
</div>
<div class="offer-info">
<h3 class="offer-title">'.$row['offerPName'].'</h3>
<div class="clearfix rating marT8 ">
<div class="rating-stars ">
<div class="grey-stars"></div>
<div class="filled-stars" style="width:60.0%"></div>
</div>
</div>
<h4 class="offer-product-price">N'.$row['offerPPrice'].' </h4>
</div>
</a>
<input type="hidden" name="newPPrice" value='.$row['offerPPrice'].'>
<input type="hidden" name="newPName" value='.$row['offerPName'].'>
<input type="hidden" name="newPImg" value='.$row['offerPImage'].'>
<div class="offer-bt-btn">
<div class="offer-btn">
<button type="submit" name="saveForLater" class="favorite-offer-btn">
<i class="far fa-heart"></i>
</button>
<a href="#" class="offer-product-info-btn">
<i class="fa fa-info"></i>
</a>
</div>
</div>
</form>
</div>';
}
}
echo $output;
?>
RESULT
<input type="hidden" name="newPName" value="Blue" louis="" vuitton="" women="" bag="">
<input type="hidden" name="newPName" value="Samsung" galaxy="" s9="" 6gb="" ram,="" 32gb="" rom,="" 16mpbs="">
its actually supposed to be
<input type="hidden" name="newPName" value="Samsung Galaxy S9 6gb ram, 32gb rom, 16mpbs">
<input type="hidden" name="newPName" value="Blue Louis Vuitton Women Bag">
Please, how do I go about it?
If any of the value contain ' characters, that will end the value attribute and start a new attribute. Use htmlspecialchars() to encode it and prevent this.
<input type="hidden" name="newPPrice" value='.htmlspecialchars($row['offerPPrice'], ENT_QUOTES).'>
<input type="hidden" name="newPName" value='.htmlspecialchars($row['offerPName'], ENT_QUOTES).'>
<input type="hidden" name="newPImg" value='.htmlspecialchars($row['offerPImage'], ENT_QUOTES).'>
Please guys my code isn't working and i don't know what to do anymore. All i'm trying to accomplish is a simple mysql update using php. The header query value works at the top of my code which i use to insert values in the form, but when i attempt to submit, for some reason unknown to me, my script succeeds but it doesn't update the database. The result of clicking the submit will be my "Javascript success alert" then " all the variables in my page become Undefined. My script is below.
<?php
//Catch the id from the header and query database
if(isset($_GET['id']))
$workID= mysqli_real_escape_string($connection, $_GET['id']);
$query = "SELECT * FROM music WHERE musicID = $workID";
$result= mysqli_query($connection, $query);
$row = mysqli_fetch_array($result);
$artID = $row['IDartiste'];
$query2 = mysqli_query($connection,"SELECT music.*, artiste.* FROM music INNER JOIN artiste ON music.IDartiste=artiste.artisteID WHERE artiste.artisteID = $artID");
$row2 = mysqli_fetch_array($query2);
?>
**This is the form for editing**
<form role="form" method="get" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<!-- Artiste name field--> <div class="form-group">
<span><label for="topic" style="color: #55AAFF">Artiste ID: </span> <span><?php echo $row['IDartiste'];?></span></label>
<!-- Article topic field--> <div class="form-group">
<label for="topic" style="color: #55AAFF">Music title:</label>
<input type="text" class="form-control" name="title" value="<?php echo $row['musicTitle'];?>" autofocus>
</div>
<!--Article textarea--> <div class="form-group">
<label for="body" style="color: #55AAFF">Content (text):</label>
<textarea name="article" class="form-control"><?php echo $row['musicArticle'];?>
</textarea>
</div>
<!-- Upload image start--> <div class="col-ms-6 col-xs-12" >
<div class="panel panel-default">
<div class="panel-heading" style="color: #55AAFF">Upload Image</div>
<div class="panel-body">
<iframe src="fileupload.php" width="100%" style="border:none"></iframe>
</div>
</div>
</div>
<!--Image link field--> <div class="form-group">
<label for="image_link" style="color: #55AAFF">Input image link: </label>
<input type="text" name="image" value="<?php echo $row['musicPhoto'];?>" class="form-control"/>
</div>
<!-- Upload music start--> <div class="col-ms-6 col-xs-12" >
<div class="panel panel-default">
<div class="panel-heading" style="color: #55AAFF">Upload Music</div>
<div class="panel-body">
<iframe src="music_file_upload.php" width="100%" style="border:none"></iframe>
</div>
</div>
</div>
<!--Music link field--> <div class="form-group">
<label for="music_link" style="color: #55AAFF">Input music link: </label>
<input type="text" name="file" class="form-control" value="<?php echo $row['musicFile'];?>"/>
</div>
<center>....<button type="submit" class="btn btn-default" id="btnsubmit" name="btnsubmit" style="color: #55AAFF">Update</button>....</center>
<br />
</form>
This is the update script
<?php
if(isset($_GET['btnsubmit'])){
date_default_timezone_set('Africa/Lagos');
$setdate= date('Y-m-d-h-m-s');
$date= strftime($setdate);
$time= strftime('%X');
$title = $_GET["title"];
$article = $_GET["article"];
$artisteImage = $_GET["image"];
$musicFile = $_GET["file"];
$sql_music = "UPDATE kingdomjoy.music SET musicTitle = '$title',
musicFile = '$musicFile',
musicPhoto = '$artisteImage',
musicArticle = '$article',
entryDate = '$date'
WHERE musicID = '$workID' LIMIT 1";
$musicupdate = $connection->query($sql_music);
if ($musicupdate === FALSE) {
echo "Error: " . die(mysqli_error($connection));}
else {echo"<script>swal('Success! music library updated')</script>";}
}
?>