if else header not redirecting - php

I'm trying to create a redirection but for some reason it is not working and for the life of me I can't figure out why. The if command works well but not the else.
Any suggestions?
$sql="SELECT * FROM $tbl_name WHERE uid='";
$sql=$sql . $xoopsUser->uid("s") . "' ";
// Get a specific result from the "example" table
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result) == 0) {
print "<div class='blockTitle'><strong>DOWNLOADS</strong></div><p><br><p>";
echo "<div align='center'>You currently have no projects pending payment.<p>If you just completed a transaction but missed the link on the PayPal page, click <a href='http://site.co.uk/site/modules/cjaycontent/index.php?id=6'>here</a>.</div>";
}
else{
header('Location: http://site.co.uk/site/myComposerList2.php');
}

You need to use exit, header wont stop the script from executing instantly.
Like this:
header('Location: http://site.co.uk/site/myComposerList2.php');
exit;

Related

Use session variable or copy PHP code from second page to third page of a website

I have three pages in the website. The first is login page,second is profile page and third is main page.
<?php
session_start();
$servername="blah blah blah";
$connectioninfo=array('Database'=>'mbr');
$conn=sqlsrv_connect($servername,$connectioninfo);
if($conn)
{
echo 'connection established';
}
else
{
echo 'connection failure';
die(print_r(sqlsrv_errors(),TRUE));
}
$q1="SELECT * FROM EmployeeTable WHERE EmployeeID = '" . $_SESSION['id'] . "' ";
$stmt=sqlsrv_query($conn,$q1);
if($stmt==false)
{
echo 'error to retrieve info !! <br/>';
die(print_r(sqlsrv_errors(),TRUE));
}
$row=sqlsrv_fetch_array($stmt);
echo $row['EmployeeName'];
$q2="SELECT * FROM pointsBadgeTable WHERE EmployeeID = '" . $_SESSION['id'] . "' ";
$stmt1=sqlsrv_query($conn,$q2);
if($stmt1==false)
{
echo 'error to retrieve info !! <br/>';
die(print_r(sqlsrv_errors(),TRUE));
}
$pbrow=sqlsrv_fetch_array($stmt1);
?>
The above is the php used in the second page of the website. Here I am using two queries $q1 and $q2 to retrieve information from two different tables (EmployeeTable and pointsBadgeTable) after connection to the database "mbr" here.
I then echo the desired Info in my html after retrieving info from the tables.
For instance,
<?php echo "". $row['goldTotal'] .""?>>
Here 'goldtotal' is a column in the pointsBadgeTable in the above php. Also please note that I am using " . $_SESSION['id'] ." here to show info only about the person who logs in the first page of the website.
The issue here is that I want to echo the same value in the third page as in second page. Will I have to write the same php code in third page as I wrote in second page or I need to store it in some session variable. How to use a session variable here?
Also, is it correct to rewrite the same code in third page also as in second page and use the same queries $q1 and $q2? I will copy and paste the same PHP to the third page also.
You Can include the second page in third page , you will get the value .
Example : file3.php
**<?php
include 'file2.php';
?>**

Delete between with a button and $_GET

I'm trying to setup a button that will delete all the rows we loaded on this run but it's not working.
PHP:
if(isset( $_GET['startID'], $_GET['endID']))
{
$sql_query = "DELETE FROM users WHERE id BETWEEN " .$_GET['startID'] . " AND " . $_GET['endID'] . ";";
mysql_query($sql_query);
header("Location: index.php");
}
Button:
<a onClick="javascript: return confirm('Are you sure?');" href='index.php?startID=<?php echo $firstID; ?>&endID=<?php echo $lastID; ?>'>Delete</a>
Button redirects to: index.php?startID=11111&endID=22222
The button is passing the values but they're not getting deleted from the database. Can anyone point me to the mistake?
For this kind of problem you need to start Debugging yourself. What you really need to do is-
Check the database connection is established or not.
Checkout the Condition is correct or not that is applied, You can use a simple echo for this.
echo the query string and run into the phpMyAdmin and see is there anything miss use or others.
These are the suggestion for all the OP who ask this type of
questions. We are always here to help you where you get stuck.
Try for instead:
if(isset( $_GET['startID'], $_GET['endID']))
{
for($i=$_GET['startID'];$i<=$_GET['endID'];$i++){
$sql_query="DELETE * FROM users WHERE id=" .$i. ";";
mysql_query($sql_query);
}
header("Location: index.php");
}

activate more than a user at once

I'm working on my project for my school, I made activating a user account work, if the admin want to activate one user at a time,
but whenever the admin wants to update multiple record at once, it won't work
I tried to include the update SQL inside a foreach loop, but it gave me an error: invalid argument for foreach()
pleas guys help me,
if(isset($_POST['activate'] )){
$impid = implode(',', $_POST['SSU']);
// for each .. to update more than one at once.
foreach($impid AS $i){
$sql = "UPDATE Accounts SET Activated = '".$_POST['activate']."' WHERE SSU IN('" . $i . "') ";
$result = mysqli_query($dbCIE, $sql) or die(mysqli_error($dbCIE));
}
// to test. if anything got effected..
if (mysqli_affected_rows($dbCIE) > 0) {
echo "<script type='text/javascript'> alert('Successfully Updated ')</script>";
}
else {
echo "<script type='text/javascript'> alert('Failed to Update ')</script>";
} // end the test.
}else{echo "<script type='text/javascript'> alert(' Select Whether to Activare Or Deactive ')</script>";}
}
} // end of first if $_post[update]
thank u guys, I fixed it, i just passed $_POST['SSU'] to my foreach and it worked.

PHP - MySQL WHERE not work

When I add this code to my Php file
include "sql_connect.php";
$query_blog="SELECT * FROM messages";
$result_blog=mysql_query($query_blog);
$num_blog=mysql_numrows($result_blog);
mysql_close();
$sql_index_menu="0";
while ($sql_index_menu < $num) {
$msg_subject=mysql_result($result,$sql_index_menu,"subject");
$msg_id=mysql_result($result,$sql_index_menu,"id");
$msg_from=mysql_result($result,$sql_index_menu,"from");
$msg_to=mysql_result($result,$sql_index_menu,"recipient");
$msg_text=mysql_result($result,$sql_index_menu,"text");
$msg_time=mysql_result($result,$sql_index_menu,"time");
$msg_read=mysql_result($result,$sql_index_menu,"readed");
?>
<tr>
<td><?php if($msg_read == "0") {echo "<img src='/images/message.gif' width='32' height='32'>";} else {echo "<img src='/images/message.png' width='32' height='32'>";}?> <?php echo $msg_time; ?></td><td><?php echo $msg_subject; ?></td><td><?php echo $msg_from; ?></td>
</tr>
<?php
$sql_index_menu++;
}
everything work BUT, when i add this to $query_blog
$query_blog="SELECT * FROM messages WHERE recipent='$username'";
so it won't work..
I tryed to change $username with my name but it still not working.
This code is working, so I copyed it and still nothing...
include "sql_connect.php";
$query="UPDATE messages
SET readed='1'
WHERE id='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
include "sql_connect.php";
$query_blog="SELECT * FROM messages WHERE id='$id'";
$result_blog=mysql_query($query_blog);
$num_blog=mysql_numrows($result_blog);
mysql_close();
$msg_text=mysql_result($result_blog,$sql_index_blog,"text");
$msg_from=mysql_result($result_blog,$sql_index_blog,"from");
$msg_subject=mysql_result($result_blog,$sql_index_blog,"subject");
$msg_time=mysql_result($result_blog,$sql_index_blog,"time");
Can you help me?
I disabled login required to page so now you can see the page (sorry for language :D) As you can see, no error
The website
as mentioned there is a typo, you misstyped recipient, anyway - i recommend you to use mysql_error() function to debug you'r code, an example would be:
$result=mysql_query($query) or die("<b>error:</b>".mysql_error()."line:".__LINE__);
The easiest way to debug a code in PHP is using echo or print_r.
In this case, you can include echo on $query_blog after setting it and run the result in your mysql IDE (or mysql command line).
$query_blog="SELECT * FROM messages WHERE recipent='$username'";
echo $query_blog;
Also, it's not a good msyql practice using quotes on where because your code will be vulnerable to injections.
Instead, use this:
$result = sprintf("SELECT * FROM messages WHERE recipent='%s'", mysql_real_escape_string($username));
$result = mysql_query($query);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $result
die($message);
}
If you are querying a mysql database from php and you want to use php variables in your query you have to escape them, otherwise you are passing the string '$username', not the value that is stored in $username.
Does this work for you?
$query_blog="SELECT * FROM messages WHERE recipent='" . $username . "'";
var_dump($query_blog);

pulling through data not working on iPHONE

have created a mobile site for my client www.-leeds-united-fans.co.uk and the problem is when you view the the latest news is not pulling through for the latest news properly on the home page.
It does not show the latest news item, however when you go to the news section it pulls through all together.
I am using the same coding for both just with a limiter on the latest:
$query = "SELECT * from newsfeed_items order by id desc limit 0,6";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
while ($row = mysql_fetch_assoc($result)) {
echo "<div id=\"featureone\">";
echo "<div class=\"comtitle\"><a href=\"news.php?id=".$row['id']."\" >".$row['title']."</a></div>";
echo "</div>";
}
can anyone help please?
Never assume a MySQL query goes through without an error. always do some kind of error handling like this:
if (!($result=#mysql_query($query, $connection)))
die('Error ' . mysql_errno() . ": " . mysql_error());
This will also give you (and us) some information on what the nature of your error is, and you can google the error number or ask us here.

Categories