Php delete images from mysql table without showing ID to frontend - php

My Database tables:
posts:
ID
post_title
etc.......
12
Title
more
images:
ID
post_id
img_url
1
12
uploads/61d878f77397a4.61355537.jpg
My current method of deleting images:
<?php
$stmt_images = $conn_posts->prepare("SELECT * FROM `images` WHERE `post_id` = ?");
$stmt_images->bind_param("i", $row['ID']);
$stmt_images->execute();
$stmt_images_results = $stmt_images->get_result(); // get result
while($row_image = $stmt_images_results->fetch_assoc()){
echo '<img src="'.$row_image['img_url'].'">';
echo '<button id="delete_button" type="button" data-id="'.$row_image["ID"].'">Delete</button>';
}
?>
Ajax:
<script>
$(document).ready(function() {
$('#delete_button').on('click', function() {
var deleteId = $(this).data('id');
var x = confirm("Are you sure you want to delete?");
if(x){
$.ajax({
url: "inc/ajax/delete_images.php",
type: "POST",
data: {
iid: deleteId,
},
cache: false,
success: function(result){
$("#info").html(result);
}
});
}
});
});
</script>
Then in inc/ajax/delete_images.php:
I run a simple delete query based on the iid, "iid = ID of the image row"
Now a frontend user can change that data-id I am blocking that by checking in delete_images.php that only the owner of the post can delete the images related to the post that was submitted but i thought of checking some websites and i see OLX does not have any id or hidden input or data variables how can I delete and images without displaying ids frontend as i dont like using data-id
I know I am doing backend checking but i want to make it even more secure by not showing the images ID
OLX Code image:

Related

display data on selecting values from Dropdown without page refresh or button click using php mysql

I have a drop down on php form, I want to populate guest names in it, which has been done, now I want to load data from table on the basis of value from this drop down and without page refresh/ button submission. How do I do this? I want simple code that could achieve it in one page rather than doing it through multiple pages. The examples I have seen so far are too complicated and when I merge them in my code, they no longer work.
Here is the function which im using onchange of dropdown:
$("#guestname").change(function()
{
var id = $(this).find(":selected").val();
var dataString = 'action='+ id;
$.ajax
({
url: 'billing.php',
data: dataString,
cache: false,
success: function(r)
{
$("#display").html(r);
}
});
})
Here is my billing.php code, it loads values on the basis of first selected value, if I again select value from drop down,it doesn't show the updated record.
<?php
include('config.php');
$action = $_REQUEST['action'];
$stmt=$dbcon->prepare('SELECT discount FROM table_name WHERE name=:name
ORDER BY name');
$stmt->execute(array(':name'=>$action));
}
?>
<div class="row">
<?php
if($stmt->rowCount() > 0){
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
?>
You can do it without page refresh but you gonna need another php file, let's name it query.php to get you the result.
On your current page
<select>
<option value='bob'>Bob
<option value='jane'>Jane
</select>
<div id='output'></div>
<script>
$('select').change(function(){
let name = $(this).val();
$.ajax({
url: 'query.php',
method: 'POST',
data: {username: name},
success: function(response){
$('#output').html(response);
}
});
})
In query.php
$username = $_POST['username'];
//query database to get your result
echo json_encode($result);

Getting ID value of clicked row - jquery

ok .. quick overview.
I have a table and each row has the ID attribute value assigned based on the ID form the db record
I have a click event that shows / hides a div and I want the data it displays
in that div to be based on the results from the DB for that corresponding ID value
Here is my table code showing the ID
<td class="hidden-xs">
<a data-toggle="toggle" href="#comp_info" class="showcompinfo" id="<?php echo $complistr['company_id'];?>">
<?php echo $complistr['registered_office_address'];?>
</a>
</td>
This is where I want the data displayed
<div class="panel-heading">
<i class="fa fa-building" style="color:orangered"></i>
<span id="showcompname"></span>
</div>
Here is the jquery code
$(document).ready(function () {
$('#comp_info').hide();
$('.showcompinfo').click(function () {
var id = $('.showcompinfo').attr('id');
$('#comp_info').toggle();
var companyid = id;
var dataString = 'companyid=' + companyid;
$.ajax({
type: 'POST',
url: '../inc/dataforms/complist.php',
data: dataString,
success: function (result) {
$('#showcompname').html(result);
}
});
});
});
Here is the PHP code for the query
include('../config.php');
if (!empty($_POST['companyid'])) {
$companyid = $_POST['companyid'];
$query = mysqli_query($dbc, "SELECT * FROM `comp_companies` WHERE `company_id` = '$companyid'");
$result = mysqli_fetch_assoc($query);
if ($result) {
echo $result['name'];
}
}
Please dont say .. your code is open to SQL injection .. i've said before its on a closed system with no external access and the people using it can barly use a PC
All I want it to do is to display the company name in the showcompname span box
If possible am I also able to display different result data in different divs ?
To retrieve the ID from the clicked <a> tag, in your jQuery code change this line:
var id = $('.showcompinfo').attr('id');
For this one:
var id = $(this).attr('id');
That way you are retrieving the id attribute from the element that was the target of the click event.
Your original code was retrieving an array of ids of all elements with the class showcompinfo.
Your problem lies in the selector for the id, var id = $('.showcompinfo').attr('id');.
You are selecting every showcompinfo in the page.
You would be better off by using this in that selector.
This would correctly select your id, based off the clicked td.
var id = $(this).attr('id');

Getting jQuery variable to PHP in the same file without refreshing the page

Thanks for reading. I have tried the answers in other similar questions but none have worked so far. I'm trying to UPDATE values inside a Table that is inside a form, so in the first part of the file is the isset "saveImport" which is the name of the a tag that I'm using to send the id thru the URL:
if (isset($_POST['saveImport'])) {
$id = $_POST['id'];
}
a tag:
<a name="saveImport" href="./?id=<?= $row['id']; ?>" class="saveImport btn btn-success col-xs">Save</a>
I do get the ID value in the URL but since is been updated in the same file I'm assuming it refreshes the page before the variable gets post thru AJAX:
<script type="text/javascript">
$(document).ready(function() {
$('.saveImport').click(function() {
var imp_id = id.id;
var imp_href = $(id).attr('href');
alert(imp_href);
$.ajax({
url: "./",
data: {id : imp_id},
type: "POST",
success: function(data){
alert(id);//send this ID to the PHP variable without
//refreshing the file
}
});
});
});
I'm getting the class .saveImport because it's inside a Table which is displaying values from a Database so it's inside a while loop and if I use an ID it will cause conflict as the ID will repeat itself.
I already created the PHP function to UPDATE the Values which executes if the ISSET is true, so my real problem will be to execute the ISSET and inside grab the ID that was posted with AJAX so I can use the ID in the UPDATE function. Right now, if I click on the a tag, it sends the ID thru URL, the page refreshes but the value of the ID is not getting in the $id = $_POST['id];
I appreciate your time.
This should work.
Change the ajax url to your php file name (mine was t.php).
Replace 99 with your data from php. (id is used. href is not used)
<?php
if (isset($_POST['saveImport'])) {
print_r( $_POST );
exit;
}
?>
<a name="saveImport" id='99' href="./?id=99" class="saveImport btn btn-success col-xs"'>Save</a>
<script type="text/javascript">
$('.saveImport').click(function(event) {
var imp_id = this.id;
$.ajax({
url: "t.php",
data: {id : imp_id, saveImport: 1},
type: "POST",
success: function(data){
alert( data );//send this ID to the PHP variable without
//refreshing the file
}
});
event.preventDefault();
});
</script>
.preventDefault() will prevent the link from loading the target page... And then, ajax will proceed.
$('.saveImport').click(function(event) {
event.preventDefault();

tooltipster dynamic content does not work

i'm very frustrated, because i can't find the answer how can i show tooltip with ajax dinamic content. If i use static contet it works, but it doesn7t work with dinamic contect. Can you please help me?
Here is my tooltipster script:
<script type="text/javascript">
$(document).ready(function () {
$('.pomoctools').tooltipster({
multiple: true,
content: 'Loading...',
functionBefore: function (origin, continueTooltip) {
continueTooltip();
// next, we want to check if our data has already been cached
//if (origin.data('ajax') !== 'cached') {
$.ajax({
type: 'POST',
url: 'person/index_person_test.php',
success: function (data) {
// update our tooltip content with our returned data and cache it
origin.tooltipster('content', $(data)).data('ajax', 'cached');
}
});
}
});
});
Then I have this php page (here is just the code for displaying the data of person: person/index_person_test.php) :
$person_id = $_GET['person_id'];//person id
//find person's data
$sql = mysql_query("SELECT * FROM person WHERE person_id='$person_id'");
$row = mysql_fetch_array($sql);
$titleText = stripslashes($row[person_name]);
$sql = mysql_query("SELECT user FROM `users` WHERE id='$row[user_id_updt]'");
$user_name_updt = mysql_fetch_array($sql);
echo $person_id;
I want to show the tooltipster on my other php page, let's call it cast.php, here is just the part for tooltipster:
echo '</td>
<td width="100%" valign="top">'.$row[person_name].'</div>';
So my problem is that the toltipster does not show data when hovering on the link in cast.php.
If i change in person_index_test.php to:
$person_id = $_POST['person_id']; //person id
it does not help.
If i go to "person/index_person_test&person_id=56774"
the data is displayed correctly and if i echo the person id, i get the correct value:
echo $person_id;
I know i need to change things in javacript to fetch my mysql data correctly, but i can't find the answer. I have tried also adding to my jaascript after
url: 'person/index_person_test.php',
data: person_id,
or
data: 'person_id'
or
data: { 'person_id': person_id},
I have tried everything but it doesn't work.
If i cange in my javascript to static person_id then the tooltipster shows the content, but of course on hover the data is always the same. So for static i have changed this:
$.ajax({
type: 'POST',
url: '**?main=person/index_person_test&person_id=56774**',
success: function(data) {
would you please help me? I have read all the tooltipster questions here on ostackoverflow, but i can't find the answer...
Thanks! Misko

PHP/MySQL How to send result back to form?

So I got this form which lets the user enter up to 10 item IDs and then check for the status or apply changes right away. If the user clicks check status the item IDs will be sent to another PHP.
What is the easiest way to send the result back to the form and display it in the red area?
if ($_POST['action'] == 'Check Status/Shelf') {
$itemids = "$itemID1, $itemID2, $itemID3, $itemID4, $itemID5, $itemID6, $itemID7, $itemID8, $itemID9, $itemID10";
while ($row = mysql_fetch_array($all)) {
$hyllplacering = $row['Hyllplacering'] . "";
$all = mysql_query("SELECT Hyllplacering, itemID FROM booking WHERE itemID IN ('$itemids')");
}
}
If you don't want to use Ajax, then save the details into a $_SESSION[] and upon submission the form is posted and then the details can be populated into SESSION and displayed back out to the form upon reloading, but it's a bit of a fiddle for not much return.
And use MySqli or PDO.
For that you have to use ajax
Give id to Apply Changes & Check Status/Self button
<script type="text/javascript">
$(document).ready(function(){
$('#apply').click(function() {
var id1 = $('#id1').val();
var id2 = $('#id2').val();
//same for all your 10 field
var cstring = "id1="+id1+"&id2="+id2 // etc;
$.ajax({
type: "POST",
url: "your file url to do database opration & fetch detail back to here",
data: cstring,
cache: false,
success: function(result) {
$('.resultdata').html(result)
}
});
});
});
</script>
<div class="resultdata"></div>

Categories