if statement print even when the statement is false? [duplicate] - php

This question already has answers here:
The 3 different equals
(5 answers)
Closed 2 years ago.
A seemingly simple question, but i'm not getting what is wrong with this one.
I have a file upload button that passes ?upload=success in the URL.
I want to show a message when upload is completed; however, even when $upload is not set, I still get the message printed.
Here's the code:
$upload = "";
echo "Upload status: ".$upload;
if ($upload = "success") {
echo "<h3>Upload completed <br></h3>";
} else {
echo "";
}
I don't get why:
when $upload is set to "" the if statements go through and prints the string;
when I complete an upload and I have ?upload=success, the echo "Upload status: ".$upload; return nothing ( and obviously the Upload completed message still gets printed)
Thanks to anybody that will spend a minute to help me :)

Your are using assignment = instead of comparison ==
if ($upload == "success") {
echo "<h3>Upload completed <br></h3>";
} else {
echo "";
}

Related

How to add an error handling to read an XML file in php?

I am developing a PHP script that allows me to modify tags in an XML file and move them once done.
My script works correctly but I would like to add error handling: So that if the result of my SQL query does not return anything display an error message or better, send a mail, and not move the file with the error and move to the next.
I did some tests but the code never displays the error and it moves the file anyway.
Can someone help me to understand why? Thanks
<?php
}
}
$xml->formatOutput = true;
$xml->save($source_file);
rename($source_file,$destination_file);
}
}
closedir($dir);
?>
Give this one a try
$result = odbc_fetch_array($exec);
if ($result === false || $result['GEAN'] === null) {
echo "GEAN not found for $SKU_CODE";
// continue;
}
$barcode = (string) $result['GEAN'];
echo $barcode; echo "<br>"; //9353970875729
$node->getElementsByTagName("SKU")->item(0)->nodeValue = "";
$node->getElementsByTagName("SKU")->item(0)->appendChild($xml->createTextNode($result[GEAN]));

Parse error: syntax error, unexpected '{' in /path/to/the/website/file.php on line 18 [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
The issue says unexpected {..... I dont understand why this is hapenning i remove it and i still ahve issues. Please reply with th right code.
Kevin Andrews / Auth
<?php
$shortenedlink = mt_rand(10000,99999);
$longlink = $_POST['longlink'];
if(!isset($longlink) || trim($longlink) == '')
{
echo "The link field is empty. Redirecting you in 3 seconds.";
header ( "refresh:3;url=http://auth.kenygamer.com" );
exit;
$shortenedlinkpath = "$shortenedlink.asp";
if (file_exists($shortenedlinkpath))
{
echo "An error occurred creating the shortened link, because the assigned number already exists. However, you can retry. Copy the link and paste it again on the main page:<br><br>$longlink<br><br>Redirecting you in 15 seconds.";
header( "refresh:15;url=http://auth.kenygamer.com" );
exit;
}
else
{
echo "";
}
$shortenedfilecontent = '<title>Outgoing link</title><meta http-equiv="refresh" content="5; url=$longlink">';
$fp = fopen("$shortenedlink.asp", "w");
fwrite($fp, $shortenedfilecontent).' ';
fclose($fp);
echo ("The shortened URL has been successfully created. The shortened number #$shortenedlink has been assigned to your long URL $longlink. Therefore, it is accessible at https://auth.kenygamer.com/$shortenedlink at any time. Remember that you can always create new shortened URLs.<br><br>Long link: $longlink<br>Shortened link: $shortenedlink<br><br>Redirecting you in 20 seconds.");
header( "refresh:20;url=https://auth.kenygamer.com/$shortenedlink" );
?>
You forgot to add the } after the exit;
This should help you:
<?php
$shortenedlink = mt_rand(10000, 99999);
$longlink = $_POST['longlink'];
if (!isset($longlink) || trim($longlink) == '') {
echo "The link field is empty. Redirecting you in 3 seconds.";
header("refresh:3;url=http://auth.kenygamer.com");
exit;
}
$shortenedlinkpath = "$shortenedlink.asp";
if (file_exists($shortenedlinkpath)) {
echo "An error occurred creating the shortened link, because the assigned number already exists. However, you can retry. Copy the link and paste it again on the main page:<br><br>$longlink<br><br>Redirecting you in 15 seconds.";
header("refresh:15;url=http://auth.kenygamer.com");
exit;
} else {
echo "";
}
$shortenedfilecontent = '<title>Outgoing link</title><meta http-equiv="refresh" content="5; url=$longlink">';
$fp = fopen("$shortenedlink.asp", "w");
fwrite($fp, $shortenedfilecontent) . ' ';
fclose($fp);
echo ("The shortened URL has been successfully created. The shortened number #$shortenedlink has been assigned to your long URL $longlink. Therefore, it is accessible at https://auth.kenygamer.com/$shortenedlink at any time. Remember that you can always create new shortened URLs.<br><br>Long link: $longlink<br>Shortened link: $shortenedlink<br><br>Redirecting you in 20 seconds.");
header("refresh:20;url=https://auth.kenygamer.com/$shortenedlink");
?>

Simple contact form validation with IF statements in PHP [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
I have made a contact form in HTML5 and I would like to validate It with PHP. I used a method what was show by Brad Hussey in a UDEMY course. So I got the data with a POST method. Then comes this code:
if ($_POST) {
if (!$_POST['nev']) {
$error .= "Kérem töltse ki a név mezőt!<br>";
}
if (!$_POST['email']) {
$error .= "Kérem adja meg az email címet!<br>";
}
if ($_POST['email'] && STfilter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {
$error .= "A(z) $emailcim cím nem valós";
}
if ($error != "") {
$error ='<div class="alert alert-danger" role="alert">Hiba lépett fel!<br>' . $error . '</div>';
} else {
$nev = $_POST['nev'];
$emailcim = $_POST['email'];
include 'phpmailer.php';
}
}
The phpmailer.php works fine if I rune it alone. And If I skip the validation and go directly to phpmailer.php everything is okay. So the problem should be inside this 3 IF statements, just I can't figure It out :(
Do you have any idea?
update: Sorry I didn't write down well the problem. It is just simple white page. So when I click to the submit button, It doesn't send the mail, and not going back to the site. It became a totaly empty page. Something like when I miss a ; sign. But I checked It many times (with syntax checker too), and nothing is missing.
I think the problem could be somewhere here:
if ($_POST['email'] && STfilter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {
$error .= "A(z) $emailcim cím nem valós";
}
I don't really understand the working of this part.
Tuğca Eker was right!
The problem was the STfilter_varprefix.
The correct statement is this:
if ($_POST['email'] && filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {
$error .= "A(z) $emailcim cím nem valós";
}

Codeigniter Flash data is not working 1 out of 10 time [duplicate]

This question already has answers here:
Codeigniter flashdata not working in internet explorer and google chrome
(2 answers)
Closed 7 years ago.
I am facing a very strange issue, the Codeigniter's flash data is not available 1 out of 10 time. Most of the time it works but randomly it doesn't. So, someone could help me know what could be the reason for this.
Controller Code
public function set_pagelist(){
$site_id = $this->input->get('site_id');
$use_list = $this->input->get('use_list');
if($use_list=="1"){
$use_list = (int) $use_list;
}
$data = array("site_id" => $site_id,"page_list_option" => $use_list);
$url_send = $this->api_url."set_pagelist_option";
$str_data = json_encode($data);
$request = sendPostData($url_send, $str_data);
$response = json_decode($request, true);
$errors = $response['errors'];
$response_message = $response['response_message'];
if (isset($response_message) && isset($errors)) {
if (trim($response_message) === "Value Set" && trim($errors) === "None") {
$this->session->set_flashdata('upload_message', '<p><div class="alert-box success"><span>success: </span>Value has been set successfully</div></p>');
redirect("manage-domain");
} elseif($response_message === "Error" && $errors === "Invalid Pagelist Option") {
$this->session->set_flashdata('upload_message', '<p><div class="alert-box error"><span>Error: </span>Invalid Pagelist Option</div></p>');
redirect("manage-domain");
}
} else {
$this->session->set_flashdata('upload_message', '<p><div class="alert-box error"><span>Error: </span>OOPS some error occurred</div></p>');
redirect("manage-domain");
}
}
In view:
echo $this->session->flashdata('upload_message');
I have been able to solve this using the following link Codeigniter flashdata not working in internet explorer and google chrome
Thought of sharing as it can be of any help to someone

Php error Working with get varialbes

i have a php code and when i add some gets variables i get error, 500, i tested it with ajax and without ajax(directly writing the url on the adress bar)
When i go with localhost/ajax/test.php?did=1 everything is fine but when i go with localhost/ajax/test.php?did=1&task=1 the problem happens
all the functions are created before on the ../php/core.php
Here is the code
<?php
require '../php/core.php';
$did = floor($_GET['did']);
if (device_exist($did) && amlogedin() && intouch_get($uid, $did) == 2) {
$task = floor($_GET['task']);
$id = safestring($_GET['id']);
switch ($task) {
case 1:
if (feature_removefeature($did, $fid)) {
echo donemsg("Feature Removed");
}
break;
}
design_makefeturelist($did);
}
else {
echo 'Sorry, some thing is wrong';
}
Almost sure that you've got an error in the feature_removefeature or donemsg function. Because after you set task=1 it will enter the case 1: statement. You could replace the if with a simple echo "lala"; to check it out.
ps. Is $fid already set?

Categories