Update and delete for specific record - php

I have written code to retrieve all the images from database for a specific city, and I want to be able to delete a specific image or to change the caption.
The problem is:
the code always work on the last image only!
I hope you guys will be able to help me with this problem.
Retrieve code:
<?php
$City_name=$_REQUEST['id'];
$Image_query = "SELECT * FROM image where City_name ='".$City_name."' ";
$Image_result = mysqli_query($dbcon,$Image_query);
echo "<table>";
while ($row = mysqli_fetch_array($Image_result))
{
$image_id = $row['Image_id'];
$image = $row['Image_url'];
$Caption = $row['Caption'];
echo "<tr style='float:right;'>";
echo "<td>"; ?> <img src="<?php echo $image ; ?>"/> <br>
<input type="text" name="caption" value="<?php echo $Caption ;?>" />
<br> <input name="delete" type="submit" value="Delete picture" />
<br> <input name="Update_caption" type="submit" value="change caption" />
<?php echo "</td>";
echo "<td>"; ?> <input class="input-image" type="hidden" name="id" value="<?php echo $image_id ;?>" />
<?php echo "</td>";
} /* End of while loop */
echo "</tr>";
echo"</table>";
?>
Update code :
if (isset($_POST['Update_caption'])) {
$ImageID = $_POST['id'];
$ImageCaption = $_POST['caption'];
$sql = mysqli_query ($dbcon,"UPDATE `image` SET `Caption`='".$ImageCaption."' WHERE `Image_id`='".$ImageID."' ");
if ($sql) {
echo "done";
} else { echo "error"; }
}
Delete code :
if (isset($_POST['delete'])) {
$ImageID = $_POST['id'];
$sql = mysqli_query ($dbcon,"DELETE FROM `image` where `Image_id` = '".$ImageID."' ");
if ($sql) {
echo "done";
} else { echo "error"; }
}

$_POST['id'] (sql injection alert!) contains the contents of the input element that has a name attribute id.
You are echoing out your input elements in a loop, all with the same name so the last one will overwrite all the previous ones.
You should use an array like for example:
<input class="input-image" type="hidden" name="id[<?php echo $image_id ;?>]" value="<?php echo $image_id ;?>" />
So that your $_POST['id'] is an array containing all elements.
The same applies to other input elements like the caption.
An alternative, especially for your delete option, would be to wrap every image in its own form. But keep in mind that you will need valid html for that to work, you can't have a form that opens in a row element and spans different columns.
And note that you should really use a prepared statement to close the sql injection hole you have now.

Related

How to add caption field for every image preview and post all caption through one submit button

This is my code I want to add one input field for every image preview and save it to db.. the field is coming but I'm not getting any data.. can anyone suggest how can I post them???
$fetch_imgid=$con->prepare("SELECT * FROM attempt010 where link='$rand'");
$fetch_imgid->setFetchMode(PDO:: FETCH_ASSOC);
$fetch_imgid->execute();
?>
<ul class="reorder_ul reorder-photos-list" id="previewImg">
<?php
while($row = $fetch_imgid->fetch()):
$delid = $row['id'];
//echo $row['id'].' '.$row['name'].'<br/>';?>
<li id="image_li_<?php echo $row['id']; ?>" class="ui-sortable-handle" data-image-id="<?php echo $delid; ?>">
<img src="uploads/<?php echo $row['name']; ?>" alt="">
<input type="submit" class="del_btn" value="Delete Image" />
<input type="text" id="cap" name="cap[]" placeholder="Enter Caption Here" />
<input type="hidden" id="cap_id" value="<?php echo $row['id']; ?>" />
<?php
endWhile;
?>
</ul>
<input type="submit" value="Add Caption" name="addcap" /> <?php include('addcap.php'); ?>
and this is addcap.php
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
if(isset($_POST['addcap'])){
foreach($_POST['cap'])
{
$imgcap = $_POST['cap'];
if($imgcap!=empty())
{
try
{
$con=new PDO("mysql:host=localhost;dbname=newimg","root","");
$sql=$con->prepare("UPDATE attempt010 SET caption='$imgcap' WHERE id='$cap_id'");
$sql->execute();
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
}
}
}
?>
<input type="hidden" id="cap_id" value="<?php echo $row['id']; ?>" />
Must have unique id. You can't send multiple fields with same id. You will get only last one.
For example:
$fetch_imgid=$con->prepare("SELECT * FROM attempt010 where link='$rand'");
$fetch_imgid->setFetchMode(PDO:: FETCH_ASSOC);
$fetch_imgid->execute();
?>
<form action="addcap.php" method="post">
<ul class="reorder_ul reorder-photos-list" id="previewImg">
<?php
$id_array="";
while($row = $fetch_imgid->fetch()):
$id_array = $id_array.$row['id'].",";
$delid = $row['id'];
//echo $row['id'].' '.$row['name'].'<br/>';?>
<li id="image_li_<?php echo $row['id']; ?>" class="ui-sortable-handle" data-image-id="<?php echo $delid; ?>">
<img src="uploads/<?php echo $row['name']; ?>" alt="">
<input type="text" id="cap_<?php echo $row['id']; ?>" placeholder="Enter Caption Here" />
<?php
endWhile;
$id_array = substr($id_array, 0, -1);
?>
<input type="hidden" id="cap_ids" value="<?php echo $id_array ; ?>" />
</ul>
<input type="submit" value="Add Caption" name="addcap" />
</form>
<!--addcap.php-->
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
if(isset($_POST['addcap'])){
if(isset($_POST['cap_ids'])){
$ids_array = explode(",", $_POST['cap_ids']);
foreach($ids_array as $ids)
{
$idcap = 'cap_'.$ids;
$imgcap = $_POST[$idcap];
if($imgcap!=empty())
{
try
{
$con=new
PDO("mysql:host=localhost;dbname=newimg","root","");
$query = "UPDATE attempt010 SET
caption='$imgcap' WHERE id='$ids'";
echo $query;
$sql=$con->prepare($query);
$sql->execute();
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
}
}
}
}
?>
This code looks like it can't work. Because you have submit and form handling code in same page. The idea behind form is to post data to different page(set in form action) and this page will do something with this data and display results to the user. For your example to work make form in your first file like:
<form action="addcap.php">
<inputs here>
</form>
Nowadays it is common that database operations are done asynchronic on server side, when user can continue using the page/app.
So learn how to use jQuery and AJAX. Maybe nodeJS or other new stuff.

Mysql fetch data selection from html checkbox

I want to get multiple return value for checkbox. But problem is with following code check box only can echo a single value. Even I select multiple checkbox I am getting only single value form it. How to solve it
$q2 = mysqli_query($conn,"SELECT product_img_url FROM temp_img");
while ($row = mysqli_fetch_array($q2)) {
$product_img_url = $row['product_img_url'];
$target_dir = "../assets/img/temp_img/";
$img_link = $target_dir.$product_img_url;
echo '<br>Add it<br><input type="checkbox" name="pic_tobe_add" value="'.$img_link.'"><br><img src="'.$img_link.'" class="img-rounded" alt="Uploaded image" width="152" height="118"><br><br>';
}
if(isset($_POST['submit_p'])) {
if (empty($_POST['pic_tobe_add'])) {
echo "Error: select a pic";
}else{echo $pic_tobe_add = $_POST['pic_tobe_add'];}
}
echo '<br><input type="submit" name="submit_p" value="Add this product"><form>';
Actually name of each check-box is same that's why you get only single value which is last-one.
change name="pic_tobe_add" to name="pic_tobe_add[]" (Make it array type so that you will get all the value)
On the next page confirm it by printing out POST value using echo "<pre/>";print_r($_POST);
use <input type="checkbox" name="pic_tobe_add[]" value="'.$img_link.'">
$q2 = mysqli_query($conn,"SELECT product_img_url FROM temp_img");
while ($row = mysqli_fetch_array($q2)) {
$product_img_url = $row['product_img_url'];
$target_dir = "../assets/img/temp_img/";
$img_link = $target_dir.$product_img_url;
echo '<br>Add it<br><input type="checkbox" name="pic_tobe_add[]" value="'.$img_link.'"><br><img src="'.$img_link.'" class="img-rounded" alt="Uploaded image" width="152" height="118"><br><br>';
}
//
if(isset($_POST['submit_p'])) {
if (empty($_POST['pic_tobe_add'])) {
echo "Error: select a pic";
}else{echo $pic_tobe_add = $_POST['pic_tobe_add'];}
}
echo '<br><input type="submit" name="submit_p" value="Add this product"><form>';

Removing a specific row result obtained through mysql_query?

I have this code where I get all the rows out of my mySQL database:
<?php
$result = mysql_query("SELECT * FROM `photos` WHERE `photo_category` = 1 AND `photo_active` = 1");
while($row = mysql_fetch_array($result)) {;
?>
<img src="<?php echo $row['thumb_path'] ?>"><br>
Creatie datum: <?php echo $row['photo_date'] ?><br>
Delete picture
<?php }; ?>
I would like to delete the image where my delete button is on. I have no idea how I should start or how I can achieve this. Any help is welcome!
Like others have stated, check out PDO or mysqli for your interface between your database. To your question. You need to issue a MySQL DELETE command. In order for this to work, your mysql user needs to have delete privileges on the table you are deleting the row from. Using your example above, the sql would look like this:
<html>
<img src="<?php echo $row['thumb_path'] ?>"><br>
Creatie datum: <?php echo $row['photo_date']; ?><br>
<form name="photo_<?php echo $row['photo_id']; ?>_delete" action="doDelete.php" method="post">
<input type="hidden" name="photo_id" value="<?php echo $row['photo_id']; ?>">
<input class="delete_button" type="submit" name="submit" value="delete">
</form>
</html>
<?php //doDelete.php
if ($_POST['submit'] == 'delete') {
$photo_id = $_POST['photo_id'];
$sql = "DELETE FROM photos WHERE photo_id = " . $photo_id . " LIMIT 1";
if (mysql_query($sql)) {
//photo deleted.
} else {
//handle errors.
}
}
?>
In order for this to work, you're going to need to add a primary key to your photos table.

Delete image in database

I'm trying to delete specific images in a database through PHP.
I have a page where all images in the database are displayed and I wanted a button under each one of them so I could delete them individually through their id but I don't know how.
Here's the PHP code for showing all images:
<?php
$result = mysqli_query($con, "SELECT * FROM galeria");
?>
<h5>Images:</h5>
<?php
while ($row = mysqli_fetch_array($result)) {
?><h6> <?php echo $row['titleimg']; ?></h6>
<p><?php echo $row['events_id']; ?></p>
<img src="../images/<?php echo $row["img"]; ?>" width="301px" height="200px"/>
<form action="delete_images.php" method="post">
<input type="submit" name="delete" value="Delete" />
</form>
<?php
echo "<br>";
echo "<br>";
}
?>
So now, what's the code I should have in my "delete_images.php" file?
Your form needs an additional piece of information, an identifier for the image to be deleted. Something like:
<form action="delete_images.php" method="post">
<input type="hidden" name="id" value="<?php echo $row['img_id'] ?>" />
<input type="submit" name="delete" value="Delete" />
</form>
Naturally, I'm guessing on the column name (img_id), but any identifier for that specific image will do the trick. With that, your POST to delete_images.php will have that value (in $_POST['id']) and can use it in the DELETE query to the database.
Put a hidden input field that will contain the imageName to which you want to delete.
<input type="hidden" value="'.$row["img"].'" name="imageName" />
// Now write some server side code in delete_images.php that will delete file
if (array_key_exists('imageName', $_POST)) {
$filename = $_POST['imageName'];
if (file_exists($filename)) {
unlink($filename);
// Write Mysql query that will delete the row from database
echo 'File '.$filename.' has been deleted';
} else {
echo 'Could not delete '.$filename.', file does not exist';
}
}

Checkbox inside a php foreach loop to delete whatever is checked

echo "<form name='input' action='admin_selecteren_voor_verwijderen.php' method='post'>";
$sql_bestelling= "SELECT * FROM producten";
foreach($dbh->query($sql_bestelling) as $row)
{
$product_id=$row['product_id'];
$product_naam=$row['product_naam'];
$prijs=$row['prijs'];
$foto=$row['foto'];
echo "
<br>
<img src='$foto' height='70' width='50' border='0'>
<b>$product_naam</b> <input type='checkbox' name='$product_naam' value='$product_naam'></br>
</br></br></br>";
//if (isset($_POST['submit'])){
// $sql = "DELETE FROM `producten` WHERE product_naam='$product_naam'";
// $query = $dbh->prepare( $sql );
// $result = $query->execute();
//}
}
if(!empty($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $check) {
echo "check: ", $check;
}
}
echo "
<input type='submit' value='Delete'>
</form>";
?>
I want to have a list of product in my webshop administrator page. Every product has a checkbox. I want to be able to delete all of the product of the checkboxes are checked. But I don't know how.. The above is what I have so far. The added picture is how it looks on the page. But the page is a list of product selected from the database with a foreach loop as can be seen in the code. The checkbox also is in the loop. I don't know how to assign every product which is check to a variable and then delete them from the database. Can anyone help me?
Name all the checkboxes a same, like delete[] , and and put the name of product in the value of each checkbox.
Example :
<form action="..." method='post' >
user1<input type="checkbox" name="delete[]"
value="<?php echo $product_naam ?>" /><br />
user2<input type="checkbox" name="delete[]"
value="<?php echo $product_naam ?>" /><br />
user3<input type="checkbox" name="delete[]"
value="<?php echo $product_naam ?>" /><br />
user4<input type="checkbox" name="delete[]"
value="<?php echo $product_naam ?>" /><br />
<input type='submit' value='delete' />
</form>
Delete query :
<?php
if(isset($_POST['delete'])){
$ids = $_POST['delete'];
$sql = "DELETE FROM `producten` WHERE product_naam
IN('".implode("','", $ids)."')";
//execute query
}
?>
You're looking for $_POST['checkbox'] but that's not what you have in your form. Name the checkboxes all checkbox[] and use $product_naam as the value.
<input type='checkbox' name='checkbox[]' value='$product_naam'>
Now you can loop over it and delete with your foreach loop.
you should not name the checkbox to the product name, just call it delete or something. Then you can use the foreach method to delete them from the database, like this:
<?php
echo "<form name='input' action='admin_selecteren_voor_verwijderen.php' method='post'>";
$sql_bestelling= "SELECT * FROM producten";
foreach($dbh->query($sql_bestelling) as $row)
{
$product_id=$row['product_id'];
$product_naam=$row['product_naam'];
$prijs=$row['prijs'];
$foto=$row['foto'];
echo "
<br>
<img src='$foto' height='70' width='50' border='0'>
<b>$product_naam</b> <input type='checkbox' name=delete' value='$product_naam'></br>
</br></br></br>";
//if (isset($_POST['submit'])){
// $sql = "DELETE FROM `producten` WHERE product_naam='$product_naam'";
// $query = $dbh->prepare( $sql );
// $result = $query->execute();
//}
}
if(isset($_POST['delete'])) {
foreach($_POST['delete'] as $delete){ {
$sql = "DELETE FROM producten WHERE product_naam= $delete";
$query = $dbh->prepare( $sql );
$result = $query->execute();
}
}
echo "
<input type='submit' value='Delete'>
</form>";
?>
Also, it seems to me you're mistaking "value" with "name", read up on it. And it would be good to read something about PDO, mysql isn't safe. http://www.php.net/manual/en/book.pdo.php

Categories