database query fails randomly, cannot replicate on my machine - javascript

edit #1:As you can see below I have multiple $db->query 's and I am wondering if this issue could be cause by too many calls to the database on slower connections? cause the page itself works the database calls just don't go through sometimes. IF this is the issue what would be a good solution for that?
I've been having a really hard time with this one. Some reason the database code only works 2/3rds of the time for customers. I have been unable to find a reason why it would not work for them either.
When some customers use my sight everything works fine up until the final page where the payment gateway sends me a transaction ID and a confirmation on whether or not the payment was successful(1 for yes 0 for no). But for some reason my database line that adds the transaction is not working
$db->query("UPDATE transactions
SET charge_id = '{$trans_id}'
WHERE cart_id = '{$cart_id}'");
$cart_id is from a cookie (issue does not lie with users having cookies blocked.) Now I had previously thought that the issue lied within safari and IE (since customers with issue had these browsers) but after some testing both browsers work on my computer and a friends (just in case). So now I really do not know what the issue could be. Without the transaction ID being set the transaction will not be marked as complete which means it doesn't get registered in an admin panel inventory systems and the quantity of the item does not get updated. The ordered and items are going through and processing So the issue has to be on this page.
Current possible ideas(not sure how to fix either):
Too many db query causing issues for people with slower interenet.
Auto cycle side bar causing an issue (2nd code block)
Code:
<?php require_once 'system/init.php'; include 'includes/head.php'; include 'includes/navigation.php'; include 'includes/headerpartial.php'; include 'includes/leftbar.php'; ?>
<div id="maincontent" class="col-md-8">
<?php
if ($_GET['response_code'] == 1) { $trans_id = $_GET['transaction_id'];
$db->query("UPDATE transactions SET charge_id = '{$trans_id}' WHERE cart_id = '{$cart_id}'");
$db->query("UPDATE cart SET paid = 1 WHERE id = '{$cart_id}'");
$tsql = $db->query("SELECT * FROM transactions WHERE charge_id = '$trans_id' ");
$tran = mysqli_fetch_assoc($tsql);
$domain = '.'.$_SERVER['HTTP_HOST'];
setcookie(CART_COOKIE,'',1,"/",$domain,false);
?> <h1 id="reciept">Thank you for your support!</h1><hr> <p id="reciept"> On behalf of LettuceHeadsFarm <?=$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 charge_id = '$trans_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 }else { echo "Sorry, an error occurred: ".htmlentities($_GET['response_reason_text']); } ?> </div>
<?php include 'includes/rightbar.php'; include 'includes/footer.php'; ?>
Sidebar Code:
<!-- right side bar-->
<div id="sidebar" class="col-md-2" >
<div class="col-md-12" style="font-size: 75%;">
<ul id="tabs" class="nav nav-pills" role="toolbar">
<li role="presentation">
</li>
<li role="presentation">
</li>
<li role="presentation">
</li>
<li role="presentation">
</li>
</ul>
</div>
<br />
<br />
<div class="tabContent" id="insta">
<div class="contentText" id="aboutContent">
<!-- LightWidget WIDGET --> -info removed instagram widget-
</div>
</div>
<div class="tabContent" id="WHoF">
<div id="whoof">
<?php
$sql = "SELECT * from happening ORDER BY post_date desc limit 3 offset 0;";
$result = $db->query($sql);
?>
<?php while($post = mysqli_fetch_assoc($result)) : ?>
<p><b><?=$post['title'];?></b></p>
<hr>
<p ><?= $post['entry']; ?></p>
<hr>
<?php endwhile; ?>
</div>
</div>
<div class="tabContent" id="veggie">
<div>
<p><a href="veggie.php">
<img border="0" alt="Veggie_crate" src="../images/header/veg.png" style="width: 100%; height: 100%;" >
</a></p>
</div>
</div>
<div class="tabContent" id="social">
<div>
-info removed. facebook widget-
</div>
</div>
<script>
$(document).ready(function () {
var timeInterval, tabCount = 0, currnetIndex = 1;
tabCount = $('ul#tabs').find('li a').length;
var tabContentObj = $('.tabContent');
changeTabIndex();
timeInterval = setInterval(function () { changeTabIndex(); }, 6 * 1000);
function changeTabIndex() {
if (currnetIndex > tabCount) {
currnetIndex = 1;
}
tabContentObj.hide();
$('ul#tabs').find('li.selected').removeClass('active');
$('ul#tabs').find('li.selected').removeClass('selected');
var currentAncorObj = $('ul#tabs').find('li a').eq(currnetIndex - 1);
currentAncorObj.parent().addClass('selected');
currentAncorObj.parent().addClass('active');
$(currentAncorObj.attr('href')).show();
currnetIndex++;
};
$('#tabs li').mouseenter(function () {
clearInterval(timeInterval);
}).mouseleave(function () {
timeInterval = setInterval(function () { changeTabIndex(); }, 4 * 1000);
});
$('#tabs li a').click(function () {
tabContentObj.hide();
$('ul#tabs').find('li.selected').removeClass('active');
$('ul#tabs').find('li.selected').removeClass('selected');
var currentAncorObj = $(this);
currnetIndex = $('ul#tabs').find('li a').index($(this)) + 1;
currentAncorObj.parent().addClass('active');
currentAncorObj.parent().addClass('selected');
$(currentAncorObj.attr('href')).show();
currnetIndex++;
//return false;
});
});
</script>
</div>

Try to generate a log of the user request and see what's going on.
$error_code = uniqid(mt_rand(), true);
file_put_contents(__DIR__ . '/cookie_' . $error_code . '.log', print_r($_REQUEST, 1), FILE_APPEND);
This will let you see what information is being supplied by the user during cart submission in order to troubleshoot. $_REQUEST includes $_GET, $_POST, and $_COOKIE data.
If you haven't already, you should also test for the existence of the $cart_id instead of expecting it to have been submitted with the user's request.
if (!empty($cart_id) && $_GET['response_code'] == 1) {
$trans_id = (int) $_GET['transaction_id'];
...//
}else{
echo "Sorry, an error occurred (Error Code: " . $error_code . "): ".htmlentities($_GET['response_reason_text']);
}

Related

Results in words with space don't show in php gallery

I'm creating a gallery in php, where it has a filter divided into categories. But when a word has a space it doesn't show the associated images.
I'm working on two tables, one that creates the categories and the other that is from the gallery.
This is my code and the sql queries:
Querys:
$sql = "selec categoria AS categoria_formatted from cat_lar";
$galerialar = $connect->query($sql);
$sql = "select foto,categoria_lar from galeria_lar,cat_lar Where categoria_lar=categoria";
$galerialarconde = $connect->query($sql);
Code:
<div class="popular page_section">
<div class="container">
<div class="row">
<div align="center">
<button class="filter-button" data-filter="all">All</button>
<?php mysqli_data_seek($galerialar, 0);
while( $galerialarc = $galerialar -> fetch_assoc()){ ?>
<button class="filter-button" data-filter="<?php echo $galerialarc['categoria_formatted']?>"><?php echo $galerialarc['categoria_formatted']?></button>
<?php } ?>
</div>
<br/>
<?php
while( $galerialarc = $galerialarconde -> fetch_assoc()){ ?>
<div class="gallery_product col-sm-3 col-xs-6 filter <?php echo $galerialarc['categoria_lar']?>">
<a class="fancybox" rel="ligthbox" href="admin/galeria/uploads/<?php echo $galerialarc['foto']?>">
<img class="img-responsive" alt="" src="admin/galeria/uploads/<?php echo $galerialarc['foto']?>" width="150px" />
</a>
</div>
<?php }?>
</div>
</div>
</div>
SCRIPT:
<script>
$(document).ready(function(){
$(".filter-button").click(function(){
var value = $(this).attr('data-filter');
if(value == "all")
{
$('.filter').show('1000');
}
else
{
$(".filter").not('.'+value).hide('3000');
$('.filter').filter('.'+value).show('3000');
}
if ($(".filter-button").removeClass("active")) {
$(this).removeClass("active");
}
$(this).addClass("active");
});
});
/* end gallery */
$(document).ready(function(){
$(".fancybox").fancybox({
openEffect: "none",
closeEffect: "none"
});
});
</script>
Can they help me? Because I've already tried to put the words with "_" and replace and it remained the same
As you suggest yourself, the image URLs can't contain whitespaces. If you already tried to replace whitespaces with underscores (_) , remember to upload the image files with that name and replace the foto url in the database with the new images.
If it doesn't work, try to investigate where exactly the problem occurs. Is the queries returning the right results (try to var_dump() the result and see if they are missing. If you think the problem happends in the HTML, try using your "inspect" feature in your browser and see if a source URL is provided for the image element and check if this URL really has an image.

Trying to use Jquery .load() with multiple Id Selectors with no success

In essence a user clicks on a movie title and both plot and cast is populated in my collapsible. It works well if I only use 1 selector for plot or cast, but not both. any ideas or examples? thanks
HTML...
<div data-role="main" class="ui-content">
<div data-role="collapsibleset">
<div data-role="collapsible">
<h1>Plot</h1>
<p id="dvdinfo"></p>
</div>
<div data-role="collapsible">
<h1>Cast</h1>
<p id="dvdcast"></p>
</div>
</div>
</div>
Javascript...
function getmoviepic_mobile(movietitle) {
var Movie = $(movietitle).text();
$('#dvdinfo','#dvdcast').load('getuser2_mobile.php', {input:Movie});
}
PHP...
<?php
include("includes/connection.php");
$q1 = trim($_POST['input']);
$result = mysql_query("
SELECT
id, title, actors, plot, catagory, release_date, rated
FROM ".TBL_DATA."
WHERE title = '".$q1."'");
while($row = mysql_fetch_array($result)) {
echo $row['plot'];
echo $row['actors'];
}
PHP:
while($row = mysql_fetch_array($result)) {
$x=[
'plot'=>$row['plot'],
'actors'=>$row['actors']
];
}
echo json_encode($x);
JS:
function getmoviepic_mobile(movietitle) {
var Movie = $(movietitle).text();
$.getJSON('getuser2_mobile.php', {input: Movie}, function(json, textStatus) {
$('#dvdinfo').html(json.plot);
$('#dvdcast').html(json.actors);
});
}
replace this line
$('#dvdinfo','#dvdcast').load('getuser2_mobile.php', {input:Movie});
with this one
$('#dvdinfo,#dvdcast').load('getuser2_mobile.php', {input:Movie});

Hide menu item according to user account

On my login mysql table, i have an account type column. If the user has a Manager account type, i want to show a management menu item and if not hide it.
This is pretty simple but its not working:
In my header, i have the following:
<script>
var logged_in_account_type = "<?php echo $_SESSION['account_type'] ; ?>";
if(logged_in_account_type === "Manager") {
document.getElementById('management_menu').style.display = 'block';
} else {
document.getElementById('management_menu').style.display = 'none';
}
</script>
I tried without the echo has well.
<div id="management_menu" style="display:none">
<li>
<i class="menu-icon fa fa-dashboard"></i><span class="mm-text">Dashboard</span>
</li>
</div>
<div class="text-bg"><span class="text-slim">Welcome,</span> <span class="text-semibold"><?php echo $_SESSION['account_type'] ?></span></div>
Have you tried using php if while rendering your menu? Something like this:
<?php if($_SESSION['account_type'] == 'Manager'): ?>
<div id="management_menu">
<ul>
<li>
Dashboard
</li>
<li>
Users
</li>
</ul>
</div>
<?php endif; ?>
Where is your javascript code in the document? If your javascript code is on the top of the document, it possibly shows or hides the <div> element properly, but, because the <div> element is at the bottom of the document, it is hidden because its "style" says so.
The solution is to move the javascript code below the <div> element, this will solve the "execution order" problem you have. Example :
<div id="management_menu" style="display:none">
<li>
<i class="menu-icon fa fa-dashboard"></i><span class="mm-text">Dashboard</span>
</li>
</div>
<?php
session_start();
$_SESSION['account_type'] = "Manager";
?>
<script>
var logged_in_account_type = "<?php echo $_SESSION['account_type'] ; ?>";
if(logged_in_account_type === "Manager") {
document.getElementById('management_menu').style.display = 'block';
} else {
document.getElementById('management_menu').style.display = 'none';
}
</script>

when i scroll down load div in php page

Am creating a PHP page for E-com. I have 100 products in database, but am showing only 12 products in page using following code
product.php
<?php
$sql = mysql_query("select * from products limit 12");
while($row = mysql_fetch_array($mysql))
{
$p_img = $row['product_image'];
$p_name = $row['product_name'];
$p_desc = $row['product_desc'];
$p_price = $row['price'];
?>
<div class="col-xs-18 col-sm-6 col-md-3 ">
<div class="thumbnail">
<img src="images/product/<?php echo $p_img; ?>" alt="products" />
<div class="caption">
<h4><?php echo $p_name;?></h4>
<p><?php echo $p_desc;?></p>
<p style="font-size:17px;"><b><i class="fa fa-inr"></i> <?php echo $p_price;?></p>
<p>Button
Button</p>
</div>
</div><!--thumbnail end-->
My question is when i scroll the muose add more 8 products in same page. How can i add that ?.
Thank you for advance.
When you're page is scrolled for example the window is scrolled for 70%.
if ($(window).scrollTop() >= ($(document).height() - $(window).height())*0.7){
$.ajax({url: "phpfile.php", success: function(result){
$("body").html(result);
}});
}
In you're php file you request too, you print the next 8. This will be in the variable result. So results contains the next 8, add this with Jquery or javascript too you're page. Here is documentation of AJAX if you want to do some more with it.
Write an ajax function after page is scrolled to 70% example:
if ($(window).scrollTop() >= ($(document).height() - $(window).height())*0.7) {
callajaxhere();
}
function callajaxhere(){
// ajax call to load products
}

Jquery infinity scroll loops over and over using Mysql/PHP

I'm trying to implement Jquery infinity scroll found at (http://infiniteajaxscroll.com/) together with my MYSQL and PHP. And it works, sort of.
I found similar example here but my was quite different anyway (http://www.w3bees.com/2013/09/jquery-infinite-scroll-with-php-mysql.html)
The loading more works fine however my results are being looped over and over again.
I mean i display all the results in the first page and when i scroll down at the bottom and infinity scroll fires up then same results are being showed instead of splitting those first results in different pages.
Here is my code, quite long :)
<?php
$page = (int) (!isset($_GET['s'])) ? 1 : $_GET['s'];
// GET ALL THE SHOUTBOX
$sql = "SELECT * FROM shoutbox
ORDER BY id DESC
LIMIT 20";
//prepare the statement
$statement = $dbConn->prepare($sql);
//execute the statement
$statement->execute();
//Count the shouboxes
$number_of_shoutbox = $statement->rowCount();
$number_of_posts_per_page = '10';
$total_pages = $number_of_shoutbox / $number_of_posts_per_page;
?>
<div class="background_spacing"></div> <!-- end of background spacing -->
<div id="header_background">
<div class="shoutbox-background text-center">
<div id="shoutbox">
<!-- SHOUTBOX COMPLETE STARTS -->
<div class="shoutbox_complete">
<div class="shoutbox_left">
<img src="images/girl-shoutbox.jpg">
</div>
<div class="shoutbox_right background-white">
<div class="no-padding padding10">
<span class="font24 font-color-000000">NAPISI SVOJ</span>
<span class="font24 username_male">SHOUTBOX</span>
<textarea class="shoutbox-form no-padding" placeholder="Ostavite i vi jedan shoutbox..."></textarea>
<input type="submit" class="button_standard_pink float-right">
</div>
</div>
</div>
<!-- SHOUTBOX COMPLETE STARTS -->
</div> <!-- end of id="shoutbox -->
</div>
</div> <!-- END OF HEADER_BACKGROUND -->
<div class="margin30"></div><!-- Spacing -->
<div class="shoutbox-total">
<div id="shoutbox">
<div class="shoutbox-counter no-padding padding-10 background-shoutbox-counter">
<div class="font48 text-center"><?php count_shoutbox();?></div>
<div class="text-center">shoutbox has been shouted!</div>
<div class="text-center padding-10"><img src="images/default/default-shoutbox-shout.png"></div>
<div class="text-center font10">INVITE YOUR FRIENDS TO SHOUT!</div>
</div>
</div>
<?
//LOOP THROUGH THE SHOUBOXES
while($show_shoutbox = $statement->fetch(PDO::FETCH_BOTH)) {
?>
<div id="shoutbox">
<!-- SHOUTBOX COMPLETE STARTS -->
<div class="shoutbox_complete_display">
<div class="shoutbox_left">
<img src="images/man-shoutbox.jpg">
</div>
<div class="shoutbox_right-display background-box-light-gray">
<div class="no-padding padding-10">
<div class="font24 username_male" style="line-height:1;"><?php echo strtoupper($show_shoutbox['user_name']);?> <?php echo $show_shoutbox['user_age'];?> GODINE</div>
<span class="font-color-898a8b">IZ <?php echo strtoupper($show_shoutbox['country']);?> - TRAŽI <?php echo strtoupper($show_shoutbox['gender_search']);?> IZMEĐU <?php echo strtoupper($show_shoutbox['age_from']);?> I <?php echo strtoupper($show_shoutbox['age_to']);?> GODINA</span>
<div class="margin15"></div>
<div class="font20"><?php echo $show_shoutbox['text'];?></div>
<div style="margin-top:52px;">
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><div class="font18 font-bold">32,222</div>
<span class="font-color-898a8b">ACHIVEMENTS</span>
</td>
<td><div class="font18 font-bold">32,222</div>
<span class="font-color-898a8b">ACHIVEMENTS</span>
</td><td><div class="font18 font-bold">32,222</div>
<span class="font-color-898a8b">ACHIVEMENTS</span>
</td><td><div class="font18 font-bold">32,222</div>
<span class="font-color-898a8b">ACHIVEMENTS</span>
</td>
</tr>
</table>
</div> <!-- end of margin-top:60 -->
</div> <!-- end of no-padding padding-10 -->
<!-- COMMENT START HERE -->
<?php
// d($detail);
$type='question';
// include('like.php');
include('includes/comment.php');
?>
<!-- COMMENT END HERE -->
</div> <!-- end of shoutbox_right-display -->
</div> <!-- end of shoutbox_complete -->
</div> <!-- end of id="shoutbox -->
<?php } // end of the shoubox loop
?>
</div>
<!--page navigation-->
<div id="nav">
<?php
for ($x = 2; $x <= $total_pages; $x++) {
?>
<a href='welcome.php?p=shoutbox&s=<?php echo $x;?>' <?php if($x=='2') { echo 'class="next"';}?>></a>
<?php } ?>
</div>
<script type="text/javascript" src="js/jquery.infinity.scroll.js"></script>
<script type="text/javascript">
var ias = $.ias({
container: ".shoutbox-total .shoutbox_complete_display",
item: "#shoutbox",
pagination: "#nav",
next: ".next"
});
ias.extension(new IASSpinnerExtension());
ias.extension(new IASTriggerExtension({offset: 3}));
ias.extension(new IASNoneLeftExtension({text: 'There are no more pages left to load.'}));
========================================================================
UPDATE CODE BY STILL GIVING ERRORS NOW IN MYSQL:
Tried #Rob Schmuecker solution but now getting errors in mysql.
Here is my modified code with PDO and with Rob Schmuecker suggestion
// GET ALL THE SHOUTBOX
$sql = "SELECT
shoutbox.shoutbox_id,
shoutbox.text,
shoutbox.date,
shoutbox.time,
shoutbox.approved,
shoutbox.`new`,
shoutbox.user_id,
users.profile_image,
users.user_name,
users.user_age,
users.country,
users.gender_search,
users.age_from,
users.age_to
FROM shoutbox INNER JOIN users ON shoutbox.user_ID = users.id
WHERE users.profile_image = '2'
AND shoutbox.approved = '1'
AND shoutbox.new = '0'
AND shoutbox.user_id != :admin_id
AND (shoutbox.user_id NOT IN (SELECT user_id FROM users_blocked WHERE blocked_id = :user_id))
ORDER BY shoutbox.shoutbox_id DESC
LIMIT :limit_start :limit_row_amount";
//prepare the statement
$statement = $dbConn->prepare($sql);
//execute the statement
$statement->execute(array(
'admin_id' => $admin_id,
'user_id' => $user_id,
'limit_start' => $limit_start,
'limit_row_amount' =>$limit_row_amount
));
You need to implement the page into your SQL query.
Below we define two new variables, one which is the total amount you want to retrieve each time and the next is the amount of rows to skip in each limit query, starting from 0 if $page = 1
$page = (int) (!isset($_GET['s'])) ? 1 : $_GET['s'];
$limit_row_amount = 20;
if($page > 1){
$limit_start = $page * $limit_row_amount;
} else {
$limit_start = 0;
}
// GET ALL THE SHOUTBOX
$sql = "SELECT * FROM shoutbox
ORDER BY id DESC
LIMIT $limit_start, $limit_row_amount";

Categories

Resources