GET request variable error in PHP - php

I have a simple PHP code, as below.
When I try the URL localhost/df.php?result1=bharat, I get the result Bharat, exactly as I want it. But when I try the URL localhost/df.php?result2=bharat, I get an error, meaning my result2 variable was not read like my result1 variable did.
Could you please correct my code so that it works?
<?php
if(isset($_GET['Result1']))
{
$file = $_GET['Result1'];
}
else
{
echo "Error"; exit;
}
echo "$result1";
?>
elseif(isset($_GET['Result2']))
{
$file = $_GET['Result2'];
}
else
{
echo "Error"; exit;
}
echo "$result2";
?>

You have way too many errors in your code. The following is the solution to your problem:
<?php
if(isset($_GET['result1']))
{
$result1 = $_GET['result1'];
echo $result1;
}
elseif(isset($_GET['result2']))
{
$result2 = $_GET['result2'];
echo $result2;
}
else
{
echo "Error";
exit();
}
?>
For the future, I would recommend you to learn PHP and be familiar with the basic syntax, at least, before posting questions about it here.

Related

Show url only if value is present

Goal:
Instead of showing simple YES or NO.
If value is found in record, show hyperlink with that value or else show text "No"
How to modify below code for this purpose:
<?php echo $row_RecordsetContacts['propertyFile'] ? '<strong>Yes</strong></br>' : 'No</br>'; ?>
View
</td>
<?php
$file = $row_RecordsetContacts['propertyFile']; # for readability only
if ($file)
{
?>View<?php
}
else
{
?>No<?php
}
I also suggest to avoid mixing echo and HTML markup. In 99% cases it makes the code better for understanding.
Try the following code:
<?php
$prop = $row_RecordsetContacts['propertyFile'];
if(empty($prop)) {
echo "No";
} else {
echo "<a href='propfiles/$prop'>View</a>";
}
?>

If-else condition gives me errors in PHP

I have two websites, each with it's own domain name. On load I have execute a php file that is used on both sites and has the same functionality.
Here is the code of that file:
<?php
echo '1';
if ($_SESSION["template"]=="template1";)
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template1/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template1/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template1/c.php');
}
else if ($_SESSION["template"]=="template2";)
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template2/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template2/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template2/c.php');
}
else {
echo 'NO DATA';
}
echo '4';
?>
On each of the two sites I set a session variable but in the above code it doesn't seem to work as I expect it to.
Am i missing something?
remove semicolon from if() and else if() statement, also add brackets when you using nested if else because it makes someone to understand easier and looks better
<?php
echo '1';
if ($_SESSION["template"]=="template1")
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template1/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template1/b.php');
}
else if { ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template1/c.php');
}
}
else if ($_SESSION["template"]=="template2")
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template2/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template2/b.php');
}
else if ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template2/c.php');
}
}
else {
echo 'NO DATA';
}
echo '4';
?>
After reviewing your code I edited my answer. The below code will do exactly the same as your code but requires alot less code.
<?php
if (isset($_SESSION['template']) && isset($_REQUEST['cmdAction'])) {
echo file_get_contents('http://localhost/images/'.$_SESSION['template'].'/'.strtolower($_REQUEST['cmdAction']).'.php');
} else {
echo 'NO DATA';
}
?>

trouble with the index PHP

for($c=1;$c<=$num;$c++)
{
$row=mysql_fetch_array(mysql_query("SELECT * FROM `$quiztitle` WHERE id=$c"));
if($row['answer']==$_POST['answer'][$c]) // NOT WORKING
{
echo "correct";
echo "<br>";
}
else
{
echo "incorrect";
echo "<br>";
}
}
on the line where it says "NOT WORKING",
the index [$c] does not get the value from the loop.
but when i specify it and change it to $_POST['answer1'], it is working.
what is the correct syntax for this?
Try this.
for($c=1;$c<=$num;$c++)
{
$row=mysql_fetch_array(mysql_query("SELECT * FROM `$quiztitle` WHERE id=$c"));
if($row['answer']==$_POST['answer'.$c]) // NOT WORKING
{
echo "correct";
echo "<br>";
}
else
{
echo "incorrect";
echo "<br>";
}
}
You're treating 'answer' as an array here, looking for an index within.
You want to concatenate the value.
if($row['answer']==$_POST["answer{$c}'])
Based on your note, it looks like you want:
$_POST["answer$c"]
Correct is:
if ($row['answer'] == $_POST['answer' . $c]) {
....
}

if else does not work

i have problem with if else statement in zend framework
<?php if(count($this->result) > 0) {
echo "not found";
}
else{
echo "advertise here";
}
?>
i would like to hide ads if there is no result, somehow, it does not work, please help
I'm not sure if I've understood the question correctly. But try
<?php
if(count($this->result) > 0) {
echo "advertise here";
}else{
// do nothing..effectively hiding.
}
?>
You can also get rid of the else part completely.
Your if else statement seems to be correct.
Did you try to check the content of your variable with a
var_dump($this->result)

Need help in display members name when logged in using PHP?

for some r.eason I cant display a logged in users name when they are logged in? the code is below
<?php
if (isset($_SESSION['user_id'])) {
echo '<?php if (isset($_SESSION[\'first_name\'])) { echo ", {$_SESSION[\'first_name\']}!"; } ?>';
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else { echo 'something';
}
?>
Thanks every one but i solved it.
Ack! Just look at your code. Do you know what this line is doing?
echo '<?php if (isset($_SESSION[\'first_name\'])) { echo ", {$_SESSION[\'first_name\']}!"; } ?>';
That's so wrong I don't even know where to begin. Just try
echo $_SESSION['first_name'];
And see if that gets you closer to what you want ;)
Make sure you're also calling session_start() before trying to access the variables.
Change your code to:
<?php
session_start();
if (isset($_SESSION['user_id'])) {
if (isset($_SESSION['first_name'])) {
echo ", " . $_SESSION['first_name']} . '!';
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else {
echo 'something';
}
?>
This is not a valid PHP code. Single quote "'" are not pair up. The block ('{' and '}') are also not pairing up.
The most importantly, the code to show the first name is in a string so it will not be shown.
I think the code you are trying to write is:
<?php
if (isset($_SESSION['user_id'])) {
if (isset($_SESSION['first_name'])) {
echo ", {$_SESSION['first_name']}!";
}
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else {
echo 'something';
}
?&gtl
Is it?
Here are the list of possibilities of the mistakes and make sure that you have corrected them
1) have you set the cookie "first_name" using setcookie method...?
2) Then have u called the session_start() function so that the session variables can be called in that page??
3) Try echo $_SESSION['first_name']... i don understand why you have put the flower brackets coz i never have used them even once in my 15 php projects..

Categories