Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
this is my code and i am using in edit.php for editing my database but when i open this page directly in browser i am getting error Notice: Undefined index: OPRID
but now i have open page this path
Optr_Edit.php?OPRID=<?=$objResult["OPRID"];?>
its right yaa wrong... tell mee
<?
include ('connection.php');
$strSQL = "SELECT * FROM OPERATOR WHERE OPRID = '".$_GET["OPRID"]."' ";
$objParse = oci_parse ($ora_conn, $strSQL);
oci_execute ($objParse,OCI_DEFAULT);
$objResult = oci_fetch_array($objParse);
if(!$objResult)
{
echo "Not found OPRID=".$_GET["OPRID"];
} else {
....
?>
http://www.yourdomain.com/path/edit.php?OPRID=hello
Using this URL, $_GET["OPRID"] would contain "hello". If you don't pass anything in via the URL like that i.e. http://www.yourdomain.com/path/edit.php then $_GET["OPRID"] will not be set.
$GET contains the CGI parameters when the php module is called from another html form. If you just open the php file directly, there won't be any parameters unless you supply them by using mypage.php?OPRID=XX
GET parameters are parameters from the URL.
For example:
http://www.example.com?OPRID=test
You can now get the value "test" via the $_GET dictionary:
print $_GET["OPRID"];
The output will be
test
You have to call it like:
edit.php?OPRID=<your value here>
then the $_GET array looks like:
array('OPRID',<your value here>);
You can check if an index exists with
if(isset($_GET['OPRID']))
{
//DO STUFF
}
else
{
//ECHO: NO OPRID FOUND
}
Please make sure you escape the values from $_GET, because you an attacker could easily type in:
edit.php?OPRID=1"or"1"="1
or worse. Read more about that: SQL Injection
You can access your GET, POST, COOKIE SERVER and ENV arrays by utilising build in function filter_input()
php.net Link
Using this function you will not get an errors or notices logged and your variables will always have a value and be sanitized.
EDIT:
Instead of using
$_GET["OPRID"]
several times you can use
$oprid = filter_input(INPUT_GET, 'OPRID');
once in your script. After this line you have a variable $oprid with a defined value which will be null if the index OPRID in $_GET was empty not set.
Excerpt from php.net:
Return Values:
Value of the requested variable on success, FALSE if the filter fails, or NULL if the variable_name variable is not set. If the flag FILTER_NULL_ON_FAILURE is used, it returns FALSE if the variable is not set and NULL if the filter fails.
Related
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 3 years ago.
Improve this question
Let's say I have a php variable $post_index = 'myInput'.
Assuming that <input name="myInput"> exist, I then use this php variable on $_POST[$post_index].
Whenever I submit the form and get the value from <input name="myInput"> with the same name as the string in $post_index it works but when I use isset() to check if the form has an input with that name, isset($_POST[$post_index]), isset() returns nothing compared to isset($_POST['myInput']) which returns a 1.
The reason why I am trying to get this to work is because I have a while loop that checks if an input field exist on a dynamically increasing number of fields monitored by an incrementing variable.
<?php
if(isset($_POST['submit-data'])){
$i = 1;
$varcheck = 'part_number'.$i;
echo "<script>alert('".isset($_POST[$varcheck])."');</script>"; //this shows nothing
echo "<script>alert('".isset($_POST['part_number1'])."');</script>"; //this shows "1"
}
?>
<form action="" method="post">
<input name = "part-number1">
<button type="submit" name="submit-data" id="submit-data">Submit</button>
</form>
Why is this the case and how do I work around this using only php?
Edit: Added the code
Edit: Answered - Typographical Error
If I understand your question correctly, you are saying that when you check for the existence of an array key, it only works when you explicitly type in the key name. It does not work when you assign that key to a variable and then use that variable in the check, right?
If so, then the variable is not holding what you think it is. Here is a hard coded example showing that it would work if the variable holds the value you expect.
<?php
$array = [
'key' => 'value'
];
var_dump(isset($array['key']));
$keyStoredInAVariable = 'key';
var_dump(isset($array[$keyStoredInAVariable]));
Output
bool(true)
bool(true)
Why is this the case and how do I work around this using only php?
You don't need a work around. You need to figure out why that variable isn't holding what you think it is. But you haven't given us enough info to help you figure out that reason.
You are sending this data from html (within a form or an xmlhttrequest)?
or are you trying to set the $_POST[] variable manually as $_POST[$post_index]?
If I'm not wrong, your answer is the second one, $_POST only receive request sent by the $_POST protocol, you can't assign it as a common variable in the same php file
This question already has answers here:
How to pass variables received in GET string through a php header redirect?
(8 answers)
Closed 7 years ago.
I have two pages in php ,one of them is exam page(exam.php) and another is result page(result.php), the result is calculated in exam page and must be sent to result page to display.(I don't have a form)
to send the result, Inside exam.php ,I write, header("location:result.php?result");
and to get the result inside result.php ,I write, $newresult=$_GET['result'];
but I receive error,and result didn't sent to the result page.
would you please guide me?
Using a URL to pass parameters can be done like so.
HTML
<a href='yourPage.php?name=Script47'>Send Variable</a>
PHP
<?php
if (isset($_GET['name') && !empty(trim($_GET['name'])) {
$name = htmlspecialchars(trim($_POST['name']), ENT_QUOTES);
}
?>
Explanation
The HTML is fairly simple, we create a link which holds a parameter specified after the page extension (?name=[...]).
The PHP first checks if the name parameter which was passed isset to prevent an undefined index error, and we check if it isn't empty. The trim function removes white spaces so an string with a space isn't outputted (" "). When we know that the string has a value in it we sanitize it (never trust user input) and then we output it.
Reading Material
htmlspecialchars();
trim();
empty();
isset();
Try use session
exam.php
<?php
session_start();
$_SESSION['result'] = $result;
?>
result.php
<?php
session_start();
echo $_SESSION['result'];
?>
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 8 years ago.
Improve this question
I am trying to pass an action parameter and a variable parameter in the same header function.
Here is the code that I have written so far:
header('Location: .?action=show_edit_form, word=$word');
When I use the action parameter alone, it goes to the next page, but when I try to pass a variable along with the action parameter, it does not work.
Please advise.
Query string parameters in URLs are separated by & not by ,
PHP doesn't interpolate variables inside strings quoted with ', you need "
Although most browsers will silently error correct, the Location header requires an absolute URI
Thus:
header("Location: http://example.com/foo.php?action=show_edit_form&word=$word");
Unless you are certain your variable won't have special characters in it, you should make sure it is properly encoded for putting in a URL too.
header("Location: http://example.com/foo.php?action=show_edit_form&word=" . urlencode($word));
What you are doing with the header is just referring the user to another page that conforms to a normal url.
Header('Location: .?action=show_edit_form&
word='.$word);
On that page you can call $_GET['word']. You may also want to urlencode your values to prevent any unforseen problems with invalid characters. Look carefully at you quotation marks inside the header function.
For Example,
Here i am trying to send the parameter id & message for the purpose of deleting record,So you
use the following code,
$Params="?action=delete&id=".$id."&mess=Deleted Sucessfully";
header("Location:company.php".$Params);
So , in the next page you will get the parameter variable as,
$action=$_GET['action'];
$id=$_GET['id'];
$message=$_GET['mess'];
Above is the format of passing multiple parameters.Hope it works...
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
function myCheck($in)
{ return isset($in); }
$var1='Something';
$var2='$var1';
$var3='$varNonExitant';
What I'm trying to achive is to use myCheck to evaluate the existance of the content like this:
myCheck($var2) return true;
myCheck($var3) return false;
isset() is not really a function: it's a language construct. As such, it's allowed to do some magic that's not available to regular functions, such as being fed with non-existing variables.
To sum up: you cannot replicate it with a custom function.
Edit:
As DaveRandom pointed out in a comment below, all you can do is come close by checking if a variable isset for example:
function variable_isset(&$variable = NULL) {
return isset($variable);
}
This approach offers two drawbacks though:
This works by passing the unset variable by reference, thus creating it when called. As it's NULL it is still not set.
It'll trigger an Undefined variable notice if the variable does not exist, ruining the whole concept of gracefully handling optional variables.
Most likely this should not be needed. So question remains why you can not use isset in the first place which would be much more needed to give you better guidance.
When you cyall myCheck($abc), with set $abc = 123, it gets myCheck(123). It isn't any valid argument for isset.
You have to give the function a string with variable name:
function isset_global($variable_name)
{
return isset($GLOBALS[$variable_name]);
}
Of course, I am also wondering, why to do this, but it answers the question as long as you check a global variable.
This question already has answers here:
PHP $_GET and $_POST undefined problem
(5 answers)
Closed 2 years ago.
I continue to get undefined printed out when I use print($_GET['user_username']); from the previous page. The URL of the page is page.php?user_username=Pete. Why is this happening?
$_GET manual says
An associative array of variables
passed to the current script via the
URL parameters.
First be sure that element exists
<?php
echo !isset($_GET["user_username"]) ? "undefined" : $_GET["user_username"];
?>
Or try var_dump against $_GET array to see if element with user_username key exists.
var_dump($_GET);
Is your request like this one?
http://www.mydomain.com/something.php?user_username=something
Try this code:
print_r($_GET);
You will get all the elements passed using get in array format. Then you can check it.. It also helps better in debugging many times.