Reputation: 3
I'm having trouble redirecting after a count hits 13.
I have bunch of images that a user can click on to add to their list in the db, when they click, an on click js script fires and triggers updatelist.php.
That part works fine, within updatelist.php I need it to insert and then check the table to see if its reached a count of 13 yet, if so, it needs to redirect to a new page.
The insert works fine, but its not redirecting. below is my updatelist.php code
<?php
include('connect.php');
session_start();
$username=$_SESSION['username'];
$image = $_GET['image'];
$query = mysql_query("SELECT * FROM tbl_movies cross join tbl_users WHERE image_on = '$image' and username = '$username'");
while ($row = mysql_fetch_assoc($query))
{
$movieid = $row['id'];
$userid = $row['UserID'];
}
$query2 = mysql_query("select * from tbl_movies cross join tbl_users where image_off = '$image' and username = '$username'");
while ($row2 = mysql_fetch_assoc($query2))
{
$mid = $row2['id'];
$uid = $row2['UserID'];
}
$numrows = mysql_num_rows($query);
$numrowz = mysql_num_rows($query2);
if ($numrows!=0)
{
$queryInsert = mysql_query("insert into tbl_user_lists (userid_fk,movie_id_fk) values ('$userid','$movieid')");
$query5=mysql_query("select l.* from tbl_user_lists as l join tbl_users on l.userid_fk=tbl_users.UserID where username='$username'");
$updatecount = mysql_num_rows($query5);
if ($updatecount == 13)
{
header('Location: http://localhost/main.php');
}
}
else if ($numrowz!=0)
{
$queryUpdate = mysql_query("delete from tbl_user_lists where userid_fk = '$uid' and movie_id_fk = '$mid'");
}
?>
Below is the script on the main page that calls the above php page
<script>
$(function(){
$('.img-swap').live('click', function() {
var _src = $(this).attr('src');
if (-1 == _src.indexOf('_on')) {
$(this).attr('src',_src.replace('_off','_on')).removeClass('off');
} else {
$(this).attr('src',_src.replace('_on','_off')).addClass('on');
}
// Update server
$.get( 'updatelist.php?image='+escape($(this).attr('src')));
$(this).toggleClass("on");
});
});
</script>
Upvotes: 0
Views: 5975
Reputation: 5883
Some problems that might have caused the issue:
Location:
you should write a full url;if ($updatecount = 13)
will always return true, use a comparison operator instead (if ($updatecount == 13)
);exit;
after the header line;<?php
opening tag the first thing in the file? Is there no html before?;Also, I don't know if your application will be publicly accessible: in that case, remember that users are not bound to the options in your html as for the values they can post, so escape your query (or use prepared statements) to avoid the risk of sql injection.
Upvotes: 0
Reputation: 21191
So you're calling a remote PHP script via JS? I don't think sending a header response to the JS code will do anything in the page that calls it. You could have your PHP script return a predetermined code depending on its result, then redirect in the JS code if the "max" code was returned.
Something like (assuming jQuery is available)
<script>
$.get( "your_php_script.php" , function (response){
if( response == "max" ) {
// redirect
window.location="other_page.php";
}}
);
</script>
Upvotes: 0
Reputation: 64137
You want:
if ($updatecount == 13)
{
header('Location: main.php');
exit();
}
Basically you were doing an assignment operator rather than a comparison, and you should always call exit() after you redirect.
Upvotes: 4