PHP compare is not working - php

$snumber = trim($_POST['sn']);
$site_name =strtoupper(trim($_POST['mn']));
$physical = trim($_POST['pp']);
$logical = trim($_POST['lp']);
$port_info =strtoupper(trim($_POST['ti']));
$srlt = mysql_query("select subscriber,terminationid from portinfo") or die(mysql_error());
while($wow = mysql_fetch_array($srlt)){
$suesno =trim($wow["subscriber"]);
$porting = trim($wow["terminationid"]);
if(($suesno == $snumber and $porting == $port_info )){
}
}
the comparison for $suesno and $snumber is working. $suesno is stored in the database as 6661235. but the comparison of $porting and $port_info is not working. and $porting is stored in the database as USER00301500030

try to use && instead of and in if condition

Related

php/mysql dynamic form fields are not inserted in my table

I have a form that has a variable number of input fields, and i now try to get these values in my database. I got this code from another question here and all the replies where implying that they got it working..so i think i'm doing something wrong here.
I get no error, it just enters one empty entry/row in my database every time i submit the form. The $_POST array is filled with all the data i need, it shows when i print_r it.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (
!empty($_POST['homeTeam']) && !empty($_POST['awayTeam']) && !empty($_POST['homeWin']) && !empty($_POST['awayWin']) &&
is_array($_POST['homeTeam']) && is_array($_POST['awayTeam']) && is_array($_POST['homeWin']) && is_array($_POST['awayWin']) &&
count($_POST['homeWin']) === count($_POST['awayWin'])
) {
$homeTeam_array = $_POST['homeTeam'];
$awayTeam_array = $_POST['awayTeam'];
$homeWin_array = $_POST['homeWin'];
$awayWin_array = $_POST['awayWin'];
for ($i = 0; $i < count($homeTeam_array); $i++) {
$homeTeam = mysql_real_escape_string($homeTeam_array[$i]);
$awayTeam = mysql_real_escape_string($awayTeam_array[$i]);
$homeWin = mysql_real_escape_string($homeWin_array[$i]);
$awayWin = mysql_real_escape_string($awayWin_array[$i]);
$sql = "INSERT IGNORE INTO CalcOdds (homeTeam, awayTeam, homeWin, awayWin) VALUES ('$homeTeam', '$awayTeam', '$homeWin', '$awayWin')";
$conn->query($sql);
$conn->close();
}
}
echo "<pre>";
print_r($_POST);
echo "</pre>";
echo 'Done!';
}
?>
I think the problem is because you have $conn->close(); inside the for loop try to add it after the loop like this:
for ($i = 0; $i < count($homeTeam_array); $i++) {
$homeTeam = mysql_real_escape_string($homeTeam_array[$i]);
$awayTeam = mysql_real_escape_string($awayTeam_array[$i]);
$homeWin = mysql_real_escape_string($homeWin_array[$i]);
$awayWin = mysql_real_escape_string($awayWin_array[$i]);
$sql = "INSERT IGNORE INTO CalcOdds (homeTeam, awayTeam, homeWin, awayWin) VALUES ('$homeTeam', '$awayTeam', '$homeWin', '$awayWin')";
$conn->query($sql);
}
$conn->close();
Instead of doing !empty() I'd do isset()
By the looks of things you haven't actually established a connection to your database.
Make sure that the data actually gets through the if() statement by using a echo() for example.

multiple and or logic in php if statement not working

I have gone round and round with this and I can't seem to get the logic correct for what I want.
Here is the code:
function fetchFile() {
global $directory;
if (isset($_GET['id'])) {
global $sampleIssue,$freeIssue;
$id = mysql_real_escape_string($_GET['id']);
$result = mysql_query("SELECT `volume`,`number`,`EditorMessage` FROM `articles` WHERE `id`='$id';");
$article = mysql_fetch_assoc($result);
if
(($article['volume']!=$sampleIssue['vol'] || $article['number']!=$sampleIssue['no']) || ($article['EditorMessage']==='N')
&& ($article['volume']!=$freeIssue['vol'] || $article['number']!=$freeIssue['no'])){
global $needsLogin;
$needsLogin = TRUE;
}
Basically I need for the login to be true or if any of the three conditions are met:
$article['volume']!=$sampleIssue['vol'] || $article['number']!=$sampleIssue['no']
OR
$article['EditorMessage']==='N'
OR
$article['volume']!=$freeIssue['vol'] || $article['number']!=$freeIssue['no']
I have tried switch cases too and I am just not getting it.

If variable is set, show $_SESSION data

I have a hard time with isset() function. Exactly how do I tell php that only if the data was assigned to my, lets say $_SESSION['msg']; variable, only then activate it and use it... So far I am getting a lot of notices: undefined index and this code doesn't do it also:
if (isset($_SESSION['monster']) &
isset($_SESSION['spol']) &
isset($_SESSION['combat']) &
isset($_SESSION['turns']) &
isset($_SESSION['zmaga']) &
isset($_SESSION['zguba']) &
isset($_SESSION['cekini']) &
isset($_SESSION['post']) )
{
$monster = $_SESSION['monster'];
$spol = $_SESSION['spol'];
$combat = $_SESSION['combat'];
$turns = $_SESSION['turns'];
$zmaga = $_SESSION['zmaga'];
$zguba = $_SESSION['zguba'];
$cekini = $_SESSION['cekini'];
$_POST = $_SESSION['post'];
}
Data was supposed to be asigned to it from a redirecting previous site but that site doesn't always activate so I need an alternative. That's why I am asking this.
EDIT with full code:
session_start();
include 'razno.php';
include 'save.php';
include 'stats.php';
$igralec_ime = $_SESSION['username'];
$monster = prikazi_borba($igralec_ime);
$poskodbe = prikazi_stat('curhp', $igralec_ime);
if (prikazi_stat('curhp', $igralec_ime) == 0)
{
$moznost = 'Tvoje zdravje je resno ogroženo, vrni se domov!';
}
else if (isset($_SESSION['monster']) &&
isset($_SESSION['spol']) &&
isset($_SESSION['combat']) &&
isset($_SESSION['turns']) &&
isset($_SESSION['zmaga']) &&
isset($_SESSION['zguba']) &&
isset($_SESSION['cekini']) &&
isset($_SESSION['post']) )
{
$monster = $_SESSION['monster'];
$spol = $_SESSION['spol'];
$combat = $_SESSION['combat'];
$turns = $_SESSION['turns'];
$zmaga = $_SESSION['zmaga'];
$zguba = $_SESSION['zguba'];
$cekini = $_SESSION['cekini'];
$_POST = $_SESSION['post'];
}
update_save($igralec_ime, 'gozd', $monster);
include '../html/gozd.html';
?>
You'll want to replace & with and:
if (isset($_SESSION['monster']) and isset($_SESSION['spol']) and isset($_SESSION['combat']) and isset($_SESSION['turns']) and isset($_SESSION['zmaga']) and isset($_SESSION['zguba']) &
isset($_SESSION['cekini']) and isset($_SESSION['post']) )
{
$monster = $_SESSION['monster'];
$spol = $_SESSION['spol'];
$combat = $_SESSION['combat'];
$turns = $_SESSION['turns'];
$zmaga = $_SESSION['zmaga'];
$zguba = $_SESSION['zguba'];
$cekini = $_SESSION['cekini'];
$_POST = $_SESSION['post'];
}
You should take a look at logical operators in PHP. In many programming languages (PHP included) & is 'bitwise and' operator (not logical). Since you want logical operator you need to use && or and. Also take a look at other PHP operators, just to get familiar with them and to avoid problems like this.
Also if you are using those values outside the if/then block you should think about cases when your statement will be false and assign some default values to those variables.

Boolean PHP MySQL And OR Not Search

Can anybody help, i'm trying to build a search using php that searches a text field in mysql.
I would like users to be able to enter a must have criteria, an or criteria and a not criteria, as well as being able to search for strings, so for example:
("This phrase" OR "That phrase") AND word
At present i'm using the example below to generate a search string:
$all = $row['and_search'] ;
$any = $row['or_search'] ;
$none = $row['not_search'];
if((!$all) || ($all == "")) { $all = ""; } else { $all = "$all"; }
if((!$any) || ($any == "")) { $any = ""; }
if((!$none) || ($none == "")) { $none = ""; } else { $none = "$none"; }
The above works brilliantly for only single words, but not searches such as the example above.
Any ideas how I can change achieve this?
Strip $all, $any and $none by whitespace into arrays. Than join them like
$newAll = '("' . join('" OR "', split(' ', $all)) . '")';

php if condition

I can't find anything wrong with this... This should work right?
function ConfirmedNumber()
{
$rs = mysql_query("CALL ConfirmedNumber('" , $_SESSION['UserID'] . "',#Active)");
while ($row = mysql_fetch_assoc($rs))
{
if ($row['Active'] = 1)
{
return true;
}
}
return false;
}
Assuming the Stored procedure returns a single row with the value '1' in it then I can call the function like this right?
if (ConfirmedNumber())
{
//do some stuff.
}
To expand on my comment:
if ($row['Active'] = 1) should be if ($row['Active'] == 1) to work correctly.
If you want to avoid accidentally doing this in future, you could write your if statements like this:
if (1 == $row['Active'])
This way, you can't accidentally use = as PHP will throw a Fatal Error. You can read more about Comparison Operators at PHP.net
Comment below with the full answer:
The call to the stored proc... line $rs = mysql_query("CALL ConfirmedNumber('" . $_SESSION['UserID'] . "',#Active)"); had a comma instead of the period in the initial post.
you forgot your operator in your IF statement. Change it to this:
if ($row['Active'] == 1)
or even shorter
if ($row['Active'])
it can be something like this
function ConfirmedNumber($id)
{
$rs = mysql_query("CALL ConfirmedNumber('" . $id . "',#Active)");
while ($row = mysql_fetch_assoc($rs))
{
if ($row['Active'] == 1)
{
return true;
}
}
return false;
}
ConfirmedNumber($_SESSION['UserID']);

Categories