This question already has answers here:
php include prints 1
(11 answers)
Closed 2 years ago.
I am using PHP and Ajax to get Data. Data get successfully, but after loaded data at the bottom of loaded data showing "1111111". Why?
I try to find solution on different websites, but i can't. please help me. Advanced Thanks.
please see the attached image
Here is my PHP code (load_more.php) :
<?php
include '../../includes/session.php';
$conn = $pdo->open();
$id= $_POST['last_product_id'];
$output = '';
$stmt = $conn->prepare("SELECT *, COUNT(*) AS numrows FROM products WHERE id < :id ORDER BY id DESC");
$stmt->execute(['id'=>$id]);
$row = $stmt->fetch();
$totalRowCount = $row['numrows'];
$showLimit = 6;
try {
$stmt = $conn->prepare("SELECT * FROM products WHERE id < :id ORDER BY id DESC LIMIT $showLimit");
$stmt->execute(['id'=>$id]);
foreach($stmt as $row){
$product_id = $row['id'];
if (!empty($row['old_price'])){
$cart_wish_btn = '
<small> <del class="taka_sign_sm text-secondary">'.number_format($row['old_price'], 0).'
</del> <span class="ml-2 badge border border-success rounded text-success badge-sm">'.number_format($current_price,0).'% OFF</span></small>';
}
$current_price = 100 -(($row['price'] / $row['old_price']) * 100) ;
$image = (!empty($row['photo'])) ? '../../images/products/'.$row['photo'] : '../../images/noimage.jpg';
$output .= ''.include ('../product/item_view_5.php').'';
}
if($totalRowCount > $showLimit){
$output .= '
<div class="show_more_main" id="show_more_main'.$product_id.'">
<div id="'.$product_id.'" class="show_more"></div>
<div class="loding" style="display: none;"><div class="spinner-border spinner-border-sm text-primary"></div></div>
</div>
';
}
}
catch(PDOException $e){
$output .= $e->getMessage();
}
$pdo->close();
echo $output;
?>
Here is my AJax:
$(document).ready(function() {
//$(document).on('click', '.show_more', function() {
$(window).scroll(function(){
var ID = $('.show_more').attr('id');
if(($(window).scrollTop() == $(document).height() - $(window).height()) && (ID != 0)){
$('.show_more').hide();
$('.loding').show();
$.ajax({
type: 'POST',
url: "../product/load_more.php",
data: 'last_product_id=' + ID,
success: function(html) {
$('#show_more_main' + ID).remove();
$('.load_data_table').append(html);
}
})
}
})
});
Here is my HTML:
<div class="row row-cols-2 row-cols-sm-4 row-cols-md-6 px-2 load_data_table"></div>
<div class="show_more_main" id="show_more_main<?php echo $product_id; ?>">
<div id="<?php echo $product_id; ?>" class="show_more"></div>
<div class="loding" style="display: none;"><div class="spinner-border spinner-border-sm text-primary">
</div>
</div>
</div>
include returns 1 which you assign to $output. You probably want to buffer and get the output of the included file:
ob_start();
include('../product/item_view_5.php');
$output .= ob_get_clean();
If you are doing this frequently then build a function with the above code:
function get_template($file) {
ob_start();
include($file);
return ob_get_clean();
}
And use it thusly:
$output .= get_template('../product/item_view_5.php');
Hello I Am Working On A Startup and I Have A Huge Problem In My Project.
In Searching Page User Can Search By Different Item And Ads Showed.
I Wanna Per Each Ad In The Picture Area Shows Picture By Slider.
Let Me Show By Pic .
This Is Searching Page
Searchin_Page
I Wanna Use Slider For Each Ad That Shows But Its Dose not Work True . . .
This Is My Jquery Code
var slideIndex = 1;
showDivs(slideIndex);
function plusDivs(n) {
showDivs(slideIndex += n);
}
function currentDiv(n) {
showDivs(slideIndex = n);
}
function showDivs(n) {
var i;
var x = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("demo");
if (n > x.length) {
slideIndex = 1
}
if (n < 1) {
slideIndex = x.length
}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" w3-white", "");
}
x[slideIndex - 1].style.display = "block";
dots[slideIndex - 1].className += " w3-white";
}
And This Is Html Side . . .
<div class="Image_Area_Panel">
<div class="w3-content w3-display-container" style="max-width:800px">
<img class="mySlides" src="http://localhost/Ajency/Public/456d6ef5aae4e33926a4c592aa5210a5--sexy-men-hot-men.jpg">
<img class="mySlides" src="http://localhost/Ajency/Public/620b1b643b4d40bef759a386764fc630.jpg">
<img class="mySlides" src="http://localhost/Ajency/Public/Brant-daugherty-cuff.jpg">
<div class="w3-center w3-container w3-section w3-large w3-text-white w3-display-bottommiddle Aroos" style="width:100%">
<div class="AreaBox">
<div class="w3-left w3-hover-text-khaki" onclick="plusDivs(-1)">❮</div>
<div class="w3-right w3-hover-text-khaki" onclick="plusDivs(1)">❯</div>
</div>
<span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(1)"></span>
<span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(2)"></span>
<span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(3)"></span>
</div>
</div>
Sorry For My English Ability
Edit: Forgot to mention none of the SQL works at all when it fails.
I seriously need help figuring this out. It has been about a month since the issue has arrived. I have rewrote the page a couple times and have tried removing some unneeded items in case it was a speed issue (had sidebar that auto scrolled and loaded in two social media widgets which was kinda slow on bad internet) and so far nothing. I really do not know why this happens at all.
Here is the kicker. It only happens to random people. Never breaks for me but breaks nearly every time for a customer on certain pc's. Another issue that person is running into is the cart cookie won't clear for that person either(just them).
I am Using Auth.net's DPM method which takes them offsite momentarily then to my Order_receipt page(the one in question). When arriving at that page you are given 2 $_GET properties example (order_receipt.php?response_code=1&transaction_id=136434353) which is coming in properly even when it fails.
Customer that has issue is using win 10, and has tried it with both chrome and edge running kaspersky antivirus (no issues on my end from either browser)
I'm going to include all code loaded and included in that page below, starting with the order_receipt itself.
** = redacted info
Order_receipt.php:
<?php
require_once 'system/init.php';
include 'includes/head.php';
include 'includes/navigation.php';
include 'includes/headerpartial.php';
?>
<div id="maincontent" class="col-md-12">
<?php
ini_set('error_reporting', -1); ini_set('display_errors', 'on');
ini_set('log_errors', 1);
ini_set('error_log', 'system/error_logs.log');
$error_code = uniqid(mt_rand(), true);
if ($_GET['response_code'] == 1)
{
$trans_id = $_GET['transaction_id'];
if (isset($cart_id)){
$db->query("UPDATE transactions SET charge_id = '$trans_id' WHERE cart_id = '$cart_id'");
$tsql = $db->query("SELECT * FROM transactions WHERE cart_id = '$cart_id' ");
$tran = mysqli_fetch_assoc($tsql);
?>
<h1 id="reciept">Thank you for your support!</h1><hr>
<p id="reciept">
On behalf of ** <?=$tran['full_name']?> we thank you for your purchase and hope you enjoy it!
</p>
<p id="reciept">
You have selected <b>"<?=$tran['pickup-location']?>"</b> as your pickup point.
</p>
<table id="nav-button" class="table table-bordered table-auto">
<tbody>
<tr>
<td>Transaction ID : <?=$tran['charge_id']?></td>
</tr>
<?php
$a = 1;
$it = 1;
$string = $tran['items'];
$itemar = explode(',', $string);
$num = 1;
$istr = $tran['inventory'];
$stri = explode(',', $istr);
if ($tran['status'] != "Complete") {
foreach (array_slice($stri, $num) as $inve ){
$exploded = explode('.', $inve);
$itname = $exploded['0'];
$itquan = $exploded['1'];
$db->query("UPDATE products SET `quantity` = `quantity` - '$itquan' WHERE title = '$itname'");
$db->query("UPDATE products SET `Sold` = `Sold` + '$itquan' WHERE title = '$itname'");
$it++;
}
$compl = "Complete";
$db->query("UPDATE transactions SET `status` = '$compl' WHERE cart_id = '$cart_id'");
}
foreach (array_slice($itemar, $num) as $itemr ){
?>
<tr>
<td><?=$itemr?></td>
</tr>
<?php
$a++;
} ?>
<tr>
<td>
Total: <?=money($tran['grand_total']);?>
</td>
</tr>
</tbody>
</table>
<?php
$domain = '.'.$_SERVER['HTTP_HOST'];
setcookie(CART_COOKIE,'',1,"/",$domain,false);
}else{echo "Cart Id not Set";}
}else
{
echo "Sorry, an error occurred: ".htmlentities($_GET['response_reason_text']);
}?>
</div>
<?php
include 'includes/footer.php';
?>
Init.php:
<?php
$db = mysqli_connect("**","**","**","**");
if(mysqli_connect_errno()){
echo 'Database connection failed with following errors: '. mysqli_connect_error();
die();
}
session_start();
require_once $_SERVER['DOCUMENT_ROOT'].'/config.php';
require_once BASEURL.'helpers/helpers.php';
$cart_id = '';
if(isset($_COOKIE[CART_COOKIE])){
$cart_id = sanitize($_COOKIE[CART_COOKIE]);
}
if (isset($_SESSION['LHUser'])) {
$user_id = $_SESSION['LHUser'];
$query = $db->query("SELECT * FROM users WHERE id = '$user_id'");
$user_data = mysqli_fetch_assoc($query);
$fn = explode(' ', $user_data['full_name']);
$user_data['first'] = $fn[0];
$user_data['last'] = $fn[1];
}
if (isset($_SESSION['success_flash'])) {
echo '<div class="bg-success"><p class="text-success text-center">'.$_SESSION['success_flash'].'</p></div>';
unset($_SESSION['success_flash']);
}
if (isset($_SESSION['error_flash'])) {
echo '<div class="bg-danger"><p class="text-danger text-center">'.$_SESSION['error_flash'].'</p></div>';
unset($_SESSION['error_flash']);
}
?>
config.php:
<?php
define('BASEURL', $_SERVER['DOCUMENT_ROOT'].'/');
define('CART_COOKIE','Sd4CqdgRt6J3gd3F7');
define('CART_COOKIE_EXPIRE', time() + (86400 * 30));
?>
helpers.php:
<?php
ob_start();
function display_errors($errors){
$display = '<ul class="bg-danger">';
foreach ($errors as $error) {
$display .= '<li class="text-danger">'.$error.'</li>';
}
$display .= '</ul>';
return $display;
}
function sanitize($dirty){
return htmlentities($dirty,ENT_QUOTES,"UTF-8");
}
function money($number){
return '$'.number_format($number,2);
}
function login($user_id){
$_SESSION['LHUser'] = $user_id;
global $db;
$date = date("Y-m-d H:i:s");
$db->query("UPDATE users SET last_login = '$date' WHERE id = '$user_id'");
$_SESSION['success_flash'] = 'You are now logged in!';
header('Location: index.php');
}
function is_logged_in(){
if (isset($_SESSION['LHUser']) && $_SESSION['LHUser'] > 0) {
return true;
}
return false;
}
function login_error_redirect($url = 'login.php'){
$_SESSION['error_flash'] = 'You must be logged in to access that page';
header('Location:'.$url);
}
function permission_error_redirect($url = 'login.php'){
$_SESSION['error_flash'] = 'You don\'t have permission to access that page';
header('Location:'.$url);
}
function has_permission($permission = 'admin'){
global $user_data;
$permissions = explode(',', $user_data['permissions']);
if (in_array($permission,$permissions,true)) {
return true;
}
return false;
}
function get_category($child_id){
global $db;
$id = sanitize($child_id);
$sql = "SELECT p.id AS 'pid', p.category AS 'parent', c.id AS 'cid', c.category AS 'child'
FROM categories c
INNER JOIN categories p
ON c.parent = p.id
WHERE c.id = '$id'";
$query = $db->query($sql);
$category = mysqli_fetch_assoc($query);
return $category;
}
head.php:
<!DOCTYPE html>
<html>
<head>
<title>LettuceHeads</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/main.css">
<link rel="icon" href="../images/header/logoicon.png">
<meta name="Viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script SRC="js/bootstrap.min.js"></script>
</head>
<body>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.6";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
navigation.php:
<?php
$sql = "SELECT * FROM navigation ORDER BY `navigation`.`sort` ASC";
$pquery = $db->query($sql);
?>
<nav id="navbar" class="navbar navbar-default navbar-fixed-top" role="navigation">
<div id="navtext" class="containter">
<a id="navborder" href="index.php" class="navbar-brand">**</a>
<ul class="nav navbar-nav">
<?php while($parent = mysqli_fetch_assoc($pquery)) : ?>
<li id="navborder"><?=$parent['name'];?></li>
<?php endwhile; ?>
</li>
</ul>
<ul id="navright" class="nav navbar-nav navbar-right" >
<li id="navborder2"><span class = "glyphicon glyphicon-shopping-cart"></span> My Cart</li>
<?php if(has_permission('admin')): ?>
<li id="navborder">Staff</li>
<?php endif; ?>
</ul>
</div>
</nav>
headerpartial.php:
<div id="partialHeaderWrapper">
<div id="partialbackitem"></div>
<div id="partiallogotext"></div>
<div id="partialfore-item"></div>
</div>
<div class="container-fluid">
footer.php:
I need to keep facebook like and share for each individual articles posted by various users. Once i logged in to the site and give share to the particular article it should get shared in facebook as well as it should show the count near to the share button of that individual article.....below is my coding please help me to get rid of this problem.Thanks a lot!
function selfURL() {
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
return
$protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}
function strleft($s1, $s2) {
return substr($s1, 0, strpos($s1, $s2));
}
<div id="fb-root">
</div>
<script src="http://connect.facebook.net/en_US/all.js#appId=204703199589477&xfbml=1">
</script>
<fb:like href="<?php print(selfURL()); ?>" send="false" width="90" show_faces="false" action="like" font="" layout="button_count">
</fb:like>
</div>
Include the JavaScript on your page once, ideally right after the opening <body> tag.
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/nl_NL/all.js#xfbml=1&appId=182934585182826";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
Place the code for your plugin wherever you want the plugin to appear on your page.
<div class="fb-like" data-href="http://www.test.com" data-send="true" data-width="450" data-show-faces="true"></div>
Replace http://test.com with the URL of your Article. If you pase the DIV 3 time
As for the Share button, you'll need this piece of script. Yet again, change the domain to your article url
<div class="fb-send" data-href="http://example.com"></div>
I am trying to delete multiple rows from a table based on checkboxes, however I'm not too sure how best to go about it. Currently, I have a list of films, and I want to be able to delete one or more of these films, based on whether they have been selected.
// Update Watchlist
if ($submit == 'Update Watchlist') {
require_once("db_connect.php");
$watchlist_name = clean_string($_POST['watchlist-name']);
$watchlist_description = clean_string($_POST['watchlist-description']);
$watchlist_category = $_POST['watchlist-category'];
$updateWatchlist_bad_message = '';
if (!empty($watchlist_name)) {
if ($watchlist_name = clean_string($watchlist_name)) {
$update_watchlist_name_query = "UPDATE watchlists SET name = '$watchlist_name' WHERE watchlist_id = " . $watchlist_id;
mysql_query($update_watchlist_name_query) or die("Insert failed. " . mysql_error() . "<br />" . $$update_watchlist_name_query);
}
}
if (!empty($watchlist_description)) {
if ($watchlist_description = clean_string($watchlist_description)) {
$update_watchlist_description_query = "UPDATE watchlists SET description = '$watchlist_description' WHERE watchlist_id = " . $watchlist_id;
mysql_query($update_watchlist_description_query) or die("Insert failed. " . mysql_error() . "<br />" . $$update_watchlist_description_query);
}
}
if ($watchlist_category != "") {
$update_watchlist_category_query = "UPDATE watchlists SET category = '$watchlist_category' WHERE watchlist_id = " . $watchlist_id;
mysql_query($update_watchlist_category_query) or die("Insert failed. " . mysql_error() . "<br />" . $$update_watchlist_category_query);
}
if(isset($_POST['film-name'])) {
$checkbox = $_POST['film-name'];
$count = count($checkbox);
for($i = 0; $i < $count; $i++) {
$id = (int) $checkbox[$i]; // Parse your value to integer
if ($id > 0) { // and check if it's bigger then 0
mysql_query("DELETE FROM watchlist_films WHERE film_id = $rt_id");
}
}
} else {
$updateWatchlist_bad_message = '<div class="alert alert-error">Sorry, but we can\'t do that at the minute. Please try again later.</div>';
}
require_once("db_close.php");?>
<script type="text/javascript">
window.location = "watchlist.php?id=<?php echo $watchlist_id; ?>"
</script><?php
}
The appropriate string is the film-name, and I have attempted to use this solution - PHP to delete SQL row with multiple checkboxes - however it is not working, insofar as the films are not being deleted from their containing Watchlist.
Basically, the logic behind my query is as follows:
check if one checkbox is ticked
if one checkbox is ticked, check if any others are ticked, too
delete all films from the Watchlist which have been ticked
I'm not sure if the above is the easiest way to do it, for example, it may be simpler and cleaner to just check if any checkboxes are ticked in one big go, rather than checking first if any one has been ticked before checking if others have been, too.
UPDATE
Just thought I'd clarify with more info - my actual foreach showing all the films in the Watchlist, is below (apologies for the formatting):
foreach ($films as $key => $film_item) {
include ("watchlist-film-controller.php");?>
<label class="checkbox input-block-level">
<p class="pull-right"><?php echo $title; ?></p>
<input type="checkbox" class="input-block-level" name="film-name[]"
value="<?php echo $title; ?>">
</label><?php
}
UPDATE 2
In answer to the two (gratefully received!) comments on this post, here's a little more information about what's now happening. I've tried both solutions, however neither are working. As it stands, I have implemented the solution given by didierc and my code currently looks like this:
<?php
/*
ini_set('display_errors', 1);
error_reporting(E_ALL);
*/
$rt_id = $film_item['film_id'];
$watchlist_id = $_GET['id'];
include_once('/api/RottenTomatoes.php');
$rottenTomatoes = new RottenTomatoes('2b2cqfxyazbbmj55bq4uhebs', 10, 'us');
/* echo "<pre>"; */
try {
$result = $rottenTomatoes->getMovieInfo($rt_id);
//print_r($result);
} catch (Exception $e) {
//print_r($e);
}
/* echo "</pre>"; */
$title = $result['title'];
$year = $result['year'];
$critics_consensus = $result['critics_consensus'];
$poster_thumb = $result['posters']['thumbnail'];
// Update Watchlist
if ($submit == 'Update Watchlist') {
require_once("db_connect.php");
$watchlist_name = clean_string($_POST['watchlist-name']);
$watchlist_description = clean_string($_POST['watchlist-description']);
$watchlist_category = $_POST['watchlist-category'];
$updateWatchlist_bad_message = '';
if (!empty($watchlist_name)) {
if ($watchlist_name = clean_string($watchlist_name)) {
$update_watchlist_name_query = "UPDATE watchlists SET name = '$watchlist_name' WHERE watchlist_id = " . $watchlist_id;
mysql_query($update_watchlist_name_query) or die("Insert failed. " . mysql_error() . "<br />" . $$update_watchlist_name_query);
}
}
if (!empty($watchlist_description)) {
if ($watchlist_description = clean_string($watchlist_description)) {
$update_watchlist_description_query = "UPDATE watchlists SET description = '$watchlist_description' WHERE watchlist_id = " . $watchlist_id;
mysql_query($update_watchlist_description_query) or die("Insert failed. " . mysql_error() . "<br />" . $$update_watchlist_description_query);
}
}
if ($watchlist_category != "") {
$update_watchlist_category_query = "UPDATE watchlists SET category = '$watchlist_category' WHERE watchlist_id = " . $watchlist_id;
mysql_query($update_watchlist_category_query) or die("Insert failed. " . mysql_error() . "<br />" . $$update_watchlist_category_query);
}
if(isset($_POST['film-name'])) {
$films = array_map('intval', $_POST['film-name']); // make sure that every film id is an integer
mysql_query("DELETE FROM watchlist_films WHERE film_id IN (" . implode(',', $films) . ") AND watchlist_id = " . $watchlist_id);
} else {
$updateWatchlist_bad_message = '<div class="alert alert-error">Sorry, but we can\'t do that at the minute. Please try again later.</div>';
}
require_once("db_close.php");?>
<script type="text/javascript">
window.location = "watchlist.php?id=<?php echo $watchlist_id; ?>"
</script><?php
}
$rt_id is each film's unique ID and is being passed to the form, so the query knows which film or films, in this case, should be deleted. The name of the film is only being used to make the actual delete form more human-readable, rather than printing out a list of ID numbers, as the user would have no way of knowing which ID matched which film. After trying out both solutions given, neither appears to be working, however no errors are being returned - the form submits, but the selected films do not delete from the Watchlist.
Update 3
In response to didierc's comment, here's a full breakdown of what's going on:
Watchlists are broken down into two tables - watchlists and watchlist_films. watchlists holds simple information such as the Watchlist ID, name and description, as well as the user ID of the user who created it. watchlist_films only contains the Watchlist ID and the film IDs the Watchlist contains ($rt_id). A Watchlist takes up a single row in the watchlists table and multiple rows in the watchlist_films table (as one Watchlist can have multiple films).
Film information is being brought back from the Rotten Tomatoes and TMDb APIs, and this is where the film ID ($rt_id) is from - each film has a completely unique $rt_id.
The full 'processing code' for Watchlists is in Update 2, however the HTML rendering is as follows:
<?php
include("checklog.php");
require_once("watchlist-controller.php");
?>
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="shortcut icon" href="img/fav.ico">
<link rel="apple-touch-icon" href="img/apple-touch-icon.png">
<title>Screening - Your ticket to your movies - <?php echo $watchlist_name; ?></title>
<meta name="description" content="Screening is a brand new take on the traditional movie database, fusing social networking and multimedia to provide a clear, concise experience allowing you to share your favourite movies, and discover new classics.">
<meta name="keywords" content="Movies, Films, Screening, Discover, Watch, Share, experience, database, movie database, film database, share film, share films, discover film, discover films, share movie, share movies, discover movie, discover movies">
<!-- Bootstrap -->
<link href="css/bootstrap.css" rel="stylesheet" media="screen">
<link href="css/bootstrap-responsive.css" rel="stylesheet">
<link href="css/custom-bootstrap.css" rel="stylesheet">
<link rel="stylesheet" href="fonts.css" type="text/css" />
<link rel="stylesheet/less" type="text/css" href="css/stylesheet.less" />
<script src="js/less-1.3.3.min.js" type="text/javascript"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<!-- Start Google Analytics -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-36943512-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<!-- End Google Analytics -->
<!-- Start Google Analytics -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-36943512-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<!-- End Google Analytics -->
</head>
<body>
<div class="container"><?php
require_once ("header.php");?>
<div class="well main-content">
<p class="page-title"><?php echo $watchlist_name; ?></p>
<div class="row-fluid">
<section class="span3 sidebar pull-left">
<p class="sidebar-text"><span class="bold">NAME: </span><?php echo $watchlist_name; ?></p>
<p class="sidebar-text"><span class="bold">CATEGORY: </span><?php echo $watchlist_category; ?></p>
<div class="alert alert-info"><?php echo $watchlist_description; ?></div>
Update Watchlist
Delete Watchlist
Your Profile
</section>
<section class="span9 watchlist-holder">
<!-- Loading bar -->
<!--
<div class="progress progress-striped active">
<div class="bar" style="width: 100%;"></div>
</div>
-->
<ul class="unstyled"><?php
foreach($films as $key => $film_item) {
include ("watchlist-film-controller.php");?>
<li class="well list-item clearfix">
<div class="row-fluid">
<img src="<?php echo $poster_thumb; ?>" alt="<?php echo $title; ?> poster" title="<?php echo $title; ?> poster" />
<div class="span11 movie-info">
<p class="search-title"><?php echo $title; ?> <small>(<?php echo $year; ?>)</small></p><?php
if ($critics_consensus == "") {?>
<p class="watchlist-synopsis">No overview available</p><?php
} else {?>
<p class="watchlist-synopsis"><?php echo $critics_consensus; ?></p><?php
}?>
</div>
</div>
</li><?php
}?>
</ul>
</section>
<div id="watchlistUpdate" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="watchlistUpdateLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="watchlistUpdateLabel" class="modal-title">Update Watchlist</h3>
</div>
<form name="watchlist-updater" class="watchlist-updater" action="watchlist.php?id=<?php echo $watchlist_id; ?>" method='POST'>
<div class="modal-body">
<?php echo $updateWatchlist_bad_message; ?>
<div class="alert alert-info">Use the boxes below to change the Watchlist name and description</div>
<input type="text" class="input-block-level" name="watchlist-name" alt="watchlist-name" placeholder="<?php echo $watchlist_name; ?>">
<textarea rows="3" class="input-block-level" name="watchlist-description" title="Watchlist Description" placeholder="<?php echo $watchlist_description; ?>"></textarea>
<label for="Watchlist Category" class="pull-left inline-label" title="Watchlist Category">Watchlist Category</label>
<select class="input-block-level" name="watchlist-category" title="Watchlist Category">
<option value="" title=""></option>
<option value="General" title="General">General</option>
<option value="To watch" title="To watch">To watch</option>
<option value="To share" title="To share">To share</option>
<option value="Favourites" title="Favourites">Favourites</option>
</select>
<div class="alert alert-info">Use the checkbox to the left of each film to remove it from the Watchlist</div><?php
foreach ($films as $key => $film_item) {
include ("watchlist-film-controller.php");?>
<label class="checkbox input-block-level">
<p class="pull-right"><?php echo $title; ?></p>
<input type="checkbox" class="input-block-level" name="film-name" value="<?php echo $title; ?>">
</label><?php
}?>
</div>
<div class="modal-footer">
<button type="button" class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button type="submit" class="btn btn-success" name="submit" value="Update Watchlist">Update Watchlist</button>
</div>
</form>
</div>
<div id="watchlistDelete" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="watchlistDeleteLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="watchlistDeleteLabel" class="modal-title">Delete Watchlist</h3>
</div>
<div class="modal-body">
<?php echo $deleteWatchlist_bad_message; ?>
<div class="alert alert-error alert-block">
<p>Deleting this Watchlist will delete all its containing films from your profile. This information will not be recoverable.</p>
<p>Please only delete this Watchlist if you are absolutely sure you want to purge all the information it contains.</p>
</div>
<p>Are you sure you want to delete this Watchlist?</p>
</div>
<div class="modal-footer">
<form name="watchlist-delete" class="watchlist-delete" action="watchlist.php?id=<?php echo $watchlist_id; ?>" method="POST"><?php
include ("watchlist-film-controller.php");?>
<button type="button" class="btn" data-dismiss="modal" aria-hidden="true">Do not delete this Watchlist</button>
<button type="submit" class="btn btn-danger" name="submit" value="Delete Watchlist">Delete Watchlist</button>
</form>
</div>
</div>
</div>
</div>
<?php include 'footer.html'; ?>
</div>
</body>
</html>
The actual updating of the Watchlist is in the #updateWatchlist modal. Anymore information required, I'm happy to provide!
I assume that (one of) the problem(s?) is that you're deleting by $rt_id. However, in the line before, it is just called id. Other than that, I can't see any obvious problems right now. If that doesn't work, please try to print the SQL query as it is about to be sent to the database by replacing mysql_query with echo and give us the output.
Also, a quick tip: Right now, you are deleting the films one by one. Depending on the number of films selected, this might be a noticeable performance hit. How about you delete them all in one query?
if(isset($_POST['film-name'])) {
$films = array_map('intval', $_POST['film-name']); // make sure that every film id is an integer
mysql_query("DELETE FROM watchlist_films WHERE film_id IN (" . implode(',', $films) . ")");
}
Some odd things:
in your delete loop you check $id from $checkbox, yet you use $rt_id: I think it's the reason why it doesn't work.
for watchlist-description, you call clean_string twice on it, once when you get it from $_POST and another time when you check if it's empty.
the checkbox values are actually the movie titles, not the movie ids, you should probably fix that, or retrieve the corresponding id in the form process script.
you delete all the entries with a given film id, but it should probably be only the ones tied to a specific watch list.
Regarding the delete process, you can make it into one single query:
$range = implode(',', array_filter(
array_map('intval', $checkbox),
function($v){ return $v > 0; }));
$update_query = 'DELETE FROM watchlist_films WHERE film_id IN ('.$range.") AND watchlist_id = '" . $watchlist_id."'";
Following your comments, let me elaborate:
The value you retrieve from the form checkbox is something called $title in the form generation, which I suppose you compute in watchlist-film-controller.php, since it doesn't appear anywhere else. But the value you need to delete the row in your table is $
rt_id. How is $rt_id computed from that $title?
Basically, your checkbox value should be that $rt_id, so that in the form processing, you don't have to look up the value again. I'm pretty sure that for a given movie title, you may get several movie id, so you cannot rely simply on the title to delete an entry in the watchlist. Imagine that someone has all the movies named "True Grit" in her watchlist, how would you handle it, if she choose to delete one of them?
Please think about moving your code to the PDO or mysqli API in the future, to enable safer data sanitization.