php getting variable outside if condition [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hi i have little problem with if statements while checking if $GET values are sended. The problem is the following: If I run this code the variables in the second if condition becomes inserted but I cant get variables from the second if condition outside the second if condition. Why does that happening? I've already tried it with return but it wont work. What is the mistake?
$gameid = $_GET['gameid'];
$id = $_GET['id'];
$questionid = $_GET['questionid'];
if (isset($_GET['gameid'])) {
$answer = "answer_d";
}
if (isset($_GET['id'])) {
// insert values into mysql database
$answer = "answer_m";
}
then I want to echo the variable $answer in a <p> tag.

If I understand your question correctly then the issue is that the $answer variable doesn't exist outside of the scope of the if statements. Try defining $answer alongside your other variables such as $gameid and then updating the value within the if statements.

at first initialize the variable value. then you echo this variable after all condition then you understand which if block your code enter or not
$gameid = $_GET['gameid'];
$answer = '';
$id = $_GET['id'];
$questionid = $_GET['questionid'];
if (isset($_GET['gameid'])) {
$answer = "answer_d";
}
if (isset($_GET['id'])) {
// insert values into mysql database
$answer = "answer_m";
}

Related

Query with WHERE clause not working using mysqli bind_param() [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am creating a webservice where I need to get some data from mysql and retrieve it in a mobile app.
I have been trying all day to do a query with a WHERE statement, but no success.
The normal query, without the WHERE is working perfectly though.
Also I went through all the similar questions on stack overflow and other forums, but none is the same problem as mine.
This is my current code:
<?php
$con=mysqli_connect("Hidden credentials");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if ($stmt = $con->prepare("SELECT
team.id as teamId,
team.team_name as teamName,
user.id as userId,
user.username as username,
team_members.role as role
FROM team_members
inner join user on team_members.user_id = user.id
inner join team on team_members.team_id = team.id
where user.id = ?
")) {
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$resultArray = array();
$tempArray = array();
while($row = $result->fetch_object())
{
$tempArray = $row;
array_push($resultArray, $tempArray);
}
echo json_encode($resultArray);
$stmt->close();
}
?>
If I remove the line where user.id=? it gets the data correctly.
Running the query on phpMyAdmin is everything ok, so it's not any issue on the query.
The variable $id doesn't exist (in your code). If you want to set the variable dynamically you can use $id = $_GET['id'];. This will take the value from the url and put it in the variable!
Your QUERY doesn't work because your variable id doesn't exists (in code what you showing).
Fix: create variable id and put some data to this variable.
For example:
$id = 5;
Or dynamcially:
From URL with GET method:
$id = $_GET['id'];
this allows you to get parameter from URL. But you must set this parameter by link. For example: <a href="index.php?id=5">. By clicking on this a tag you will be redirected to page index.php with parameter id which equals to 5.
From POST method:
for example you have this FORM:
<form method="post">
<input type="number" name="id">
<input type="submit" name="submit">
</form>
after submiting this FORM values will be saved in $_POST. You can access them by $_POST["name"]. In this case $_POST["id"].
$id = $_POST["id"];
From SESSION:
$id = $_SESSION["id"];
but first you have define $_SESSION["id"]. You can access this variable ($_SESSION["id"]) in other pages of your domain.

How to search a string in a whole MySQL database? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to search a specific string from a whole MySQL database not from only a single table of the database and get the data as a JSON object in my android app.
here is my php file
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$con = require("connection.php");
if (isset($_POST['submit']))
{
$username = $_POST['username'];
$password = $_POST['password'];
}
$statement = mysqli_prepare($con, "SELECT * FROM user WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement,"ss",$username,$password);
mysqli_stmt_execute($statement);
mysqli_store_result($statement);
mysqli_stmt_bind_result($statement,$id,$name,$phone,$email,$username, $password);
$user = array();
while(mysqli_stmt_fetch($statement)){
$user[name]= $name;
$user[phone]= $phone;
$user[email]= $email;
$user[username]= $username;
$user[password]= $password;
}
echo json_encode($user);
mysqli_stmt_close($statement);
mysqli_close($con);
?
But I don't want to search only in user table instead of i want to search in whole database. please help.
There are no direct way to SELECT from the whole database. Instead you can get all tables from the information_schema like this.
Then you can loop over all tables and search in all columns with the comlumn types like eg. varchar, text or others depending on your needs.

Update query is not working - need to look [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Can anyone tell where am I wrong in the query. The database in not updating irrespective of the value
<?php
// ================= UPDATE =========================
if ($_POST['SUBMIT']=='SUBMIT')
{
$fixture_id = "$_GET[id]";
$m_date = "$_POST[match_date]";
$m_time = "$_POST[match_time]";
$m_report = "$_POST[match_report]";
$m_a_result = "$_POST[team_a_result]";
$m_b_result = "$_POST[team_b_result]";
$updt = mysql_query("UPDATE `fixture` SET match_date='$m_date', match_time='$m_time', match_report='$m_report', match_a_result='$m_a_result', match_b_result='$m_b_result', status = 1 WHERE id = '$fixture_id'");
header("location:view_fixture.php?msg= You have inserted result successfully...");
}
else
{
header("location:result_update.php?msg= Something went wrong...");
}
// ================================================================================
?>
Before executing this make sure your submit button have a string value of "SUBMIT"
Try this ..
<?php
$fixture_id = $_GET[id];
if ($_POST['SUBMIT']=='SUBMIT')
{
$m_date = $_POST['match_date'];
$m_time = $_POST['match_time'];
$m_report = $_POST['match_report'];
$m_a_result = $_POST['team_a_result'];
$m_b_result = $_POST['team_b_result'];
$updt = mysql_query("UPDATE `fixture` SET match_date= '$m_date', match_time='$m_time', match_report='$m_report', match_a_result='$m_a_result', match_b_result='$m_b_result', status = 1 WHERE id = '$fixture_id'");
header("location:view_fixture.php?msg= You have inserted result successfully...");
}
else
{
header("location:result_update.php?msg= Something went wrong...");
}
?>
Please DO NOT USE mysql_* . It is now deprecated

No response to update query in oracle [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am a beginner . I made a php html combined page(viewemployees.php) to display my table from oracle data base. It worked fine. Then i made a page to edit my data base table(editemployees.php). that page has two functions. It displays the original table and a simple form which should contain the elements to update the table. When we place values in the form it directs us to a new php page(een.php) where it updates table and returns back to editemployees.php showing the updated table. but i am stuck when i fill in the form AND NOTHING happens. Even I have placed a condition that to echo a line if new value field is null. still that line is not echoed. This is some weird thing. I am posting both of my edit and view pages code click the link. kindly help.
In simple my table update is not working and showing undefined variable error at line 4 of een.php. Why is taht variabl undefined?? can anyone help please
https://www.dropbox.com/sh/xtuvotdy7c9wr1v/AADrNSlC_EJ0YkyDDkhe8mKGa?dl=0
<?php
include("connection.php");
$empid = $_POST['EMPLOYEE ID'];
$field = $_POST['EDIT FIELD'];
$nfield = $_POST['NEW VALUE'];
echo $field;
if( empty($_POST['NEW VALUE'] )){
echo "type new field properly";
}
else
{
$e = filter_var($empid, FILTER_SANITIZE_EMAIL);
$f = filter_var($field, FILTER_SANITIZE_EMAIL);
$nf = filter_var($nfield, FILTER_SANITIZE_EMAIL);
}
if( $e==$empid && $f==$field && $nf==$nfield)
{
if ($field=="age" || $field=="sal"){
$sel = "seleect * from employ";
$st = oci_parse(conn, $sel);
oci_execute($st);
$query = " update employ set $field = $nfield where empid = $empid";
$stmt = oci_parse($conn,$query);
oci_execute($stmt, OCI_COMMIT_ON_SUCCESS);
oci_free_statement($updateTitleInserted);
oci_close($conn);
echo oci_error();
header("Location: home.html");
}
else{
$query = "update employ set $field = '$nfield' where empid = $empid";
$stmt = oci_parse($conn,$query);
oci_execute($stmt,OCI_COMMIT_ON_SUCCESS);
oci_free_statement($updateTitleInserted);
oci_close($conn);
echo oci_error();
echo $field;
}
}
else
echo "wrong data entry go back and enter again";
?>
This line does not make sense.
$empid = $_POST["EMPLOYER ID"];
A post variable name cannot contain a space.

Why is my function not working PHP? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hey why is my function not working here is the php code, its written in mysql_connect:
function isUserLoggedIn() {
global $conn;
$sql = "SELECT user_id, password FROM user
WHERE
user_id='" . fixstr($loggedInUser->user_id) . "'
AND
password='" . fixstr($loggedInUser->password) . "'
AND
active = 1
LIMIT 1";
$res = mysql_query($sql);
$rs = mysql_fetch_array($res);
if($loggedInUser($res) == NULL)
{
return false;
}
else
{
//Query the database to ensure they haven't been removed or possibly banned?
if(returns_result($sql) > 0)
{
return true;
}
else
{
//No result returned kill the user session, user banned or deleted
$loggedInUser->userLogOut();
return false;
}
}
}
The connection does return an active window but is not able to connect to any of the functions does anybody know why my code is not working?
$loggedInUser is not in the scope.
You can do one of the following:
inject $loggedInUser through method parameter.
Eg) function isUserLoggedIn($loggedInUser)
locally construct or define $loggedInUser.
Eg) $loggedInUser = (new LoggedInUserFactory)->buildLoggedInUser();
declare global $loggedInUser;
In addition to what Kita wrote, it's also not known whether $conn is valid, and perhaps if($loggedInUser($res) == NULL) should be $rs not $res? I don't know the function definition so I can't say for sure, but it looks like that's more likely

Categories