I'm trying to make a POST method that will receive a value from a table (that is dynamically generated). This value will be equal to a company name, and a hidden field will be there that is equal to company name + "id" appended to it.
Here's my code:
if(isset($_POST))
{
foreach ( $users as $balance_user ) {
if(isset($_POST[$balance_user]))
{
//update user meta with new balance
$newBalance = $_POST[$balance_user];
$postedID = $_POST[$balance_user.'id'];
update_user_meta($postedID, 'balance', $newBalance);
}
}
}
I keep getting the error Illegal offset type in isset or empty. Can I not pass variables in that way? For example if a company is called Acme, and that particularly named input has a value in it, I want to loop through all of the companies in the POST method, and if that part of the loop equals the company passed in the variable, it should do something.
Add these three lines to see the data, as others have indicated, clearly you are assuming some value is in $balance_user which is not there, or is different.
echo '<pre>:';
var_dump($balance_user);
echo ':</pre>';
if(isset($_POST[$balance_user]))
The pre makes it easier to read the debugging output. the :..: will show null values.
Once you run that, you will probably discover that one of your entries in $users is empty.
The output order will show you where that empty user value is.
However:
$postedID = $_POST[$balance_user.'id'];
That could be the error source as well, is there a post value that is. say, $balance_user == fred
fredid
if there isn't, of course you will instantly get that error. You aren't giving the line number of the error so I can't tell which it is, the line number will show it instantly.
Related
I am trying to compare a value in a table against a new value given in a form then giving a response of what was changed but it's giving the value if true or not
$var1 (existing value in table pulled from a search to display currently value in the form)
$var2 (new value provided in form)
$comment1 (the response)
This is what it looks like now:
if ($data[var1]!='$_POST[var2]')
{
$comment1="$data[var1] changed to $_POST[$var2]";
// (this will be scrubbed but I have the simple form here)
}
else { $comment1=""; }
I have tried: if($data[var1]<>'$_POST[var2]')
I have also tried adding $old_var1=$data[var1];
Then changing to if($old_var1]!='$_POST[var2]') and if($old_var1]<>'$_POST[var2]')
But the comment still shows if they are the same or not, I only want the comment to apply if they are not the same.
You are comparing the data value to a string that is '$_POST[var2]' but not the data inside $_POST[var2]
Try: if ($data['var1']!=$_POST['var2']) if your keys are var1 and var2, or
if ($data[$var1]!=$_POST[$var2]) if the keys are stored in repective variables
So I've got a form with a modal, that modal has 3 rows with 2 text fields each, if the user (me in this prod case) fills out only 2 rows, and leave the other row empty, that 3rd value should be NULL.
In my script I've got:
if (!is_null($_POST['packageDependencies']['bundle'][2])) {
$packageDependency3 = $_POST['packageDependencies']['bundle'][2] . "|" . $_POST['packageDependencies']['version'][2] . "|" . $_POST['packageDependencies']['repository'][2];
$depends = "<key>dependencies</key>
<array>
<string>$packageDependency1</string>
<string>$packageDependency2</string>
<string>$packageDependency3</string>
</array>
";
}
So I'm checking if (!is_null($3rdRow)) { //Do this }, but the variable $_POST['packageDependencies']['bundle'][2] is in fact NULL, as I use var_dump($_POST['packageDependencies']['bundle'][2]); and I get NULL printed to the page, but the if statement is still processing as if it isn't NULL.
$depends gets fwrite() to an XML file, and when I open it, I only see || and but that shouldn't be there as the variable is NULL as I entered no values into those input fields.
Given my advice, a more complete solution would be:
if (!empty(trim($_POST['packageDependencies']['bundle'][2]))) {
NULL is a specific state of a variable that involves the way PHP associates the name of a variable with a variable location. You can think of it like a flag, that indicates a variable name exists, but there is no storage location associated with it. There are a number of situations that empty with trim will catch that will bypass a check against null.
Even though !empty() did the trick, I've decided to use == to be less ambiguous. The answers found here are quite intuitive.
EDIT: As per #gview, adding (!empty(trim($var))) is the best bet as if a user accidentally presses the space key after a tab, it will avoid any errors.
While retrieving values from array without index in PHP I am getting nothing.
From my database value is stored like ["SCHOOL"] and I want to get just SCHOOL from this.
It's not treated as array instead it's treated as a string.
What will be the solution if want to treat this as array and get value of that array.
My Code is as follows :
$stream_arr = $res_tbl['streams'];
which gives result as ["SCHOOL"]
and I want just SCHOOL from this.
I am using code as $stream = $stream_arr[0];
and I get '[' this as result.
If I assign manual value to $stream_arr = ["SCHOOL"],
above code works and returns SCHOOL as expected.
But for results from my database is not working where result is same as the one I manually assigned.
To eliminate braces from the string you can use below code.
$stream_arr = '["SCHOOL"]';
$temp=explode('"',$stream_arr);
$val=$temp[1];
echo $val; //it will dispaly 'SCHOOL'.
But check the insert query(from the place you store data into db) that why it is storing with braces? if it is done by you as required then proceed else first fix that issue instead of eliminating braces in php code.
Here's the link i want to decode:
https://api.instagram.com/v1/users/28855276/media/recent/?client_id=775829274b6f48c1ac2bf218dda60e16
Actually i've tried many methods to get the result i want but i failed to do so with PHP.
i need to extract a value from this json which should equal a certain variable
Example:
$variable = json_value
The Hard thing here that i want a duplicated value.
the value i need is:
profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_28855276_75sq_1348344197.jpg"
Do not know how to reach this value.
Here's it's place in the json file
{"username":"zedd","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_28855276_75sq_1348344197.jpg","id":"28855276","full_name":"Zedd"}
Please note that i've the username and id values already known.
Need to extract the profile_picture link to a PHP variable.
Please note: profile_picture is duplicated.
There is no duplication by me.
<?php
$array = json_decode('{"username":"zedd","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_28855276_75sq_1348344197.jpg","id":"28855276","full_name":"Zedd"}',true);
echo $array['profile_picture'];
?>
I am working on a system to transfer a table into an array over PHP and html forms. In doing so I have to echo the users ID into a hidden element on the table. Here is where I am stumped:
$sqlb = mysql_query('SELECT * FROM table_row WHERE tid = 1');
$numb = 0;
while($rowsres = mysql_fetch_array($sqlb))
{
....
echo('<td style="text-align: center;"><input type="checkbox" value="'.$rowres['rid'].'" name="list[]" /></td>');
echo $rowres['rid'];
echo('<td>'.getData(1, $rowsres['rid'], 1).' '.getData(1, $rowsres['rid'], 2).'</td>');
....
}
To explain the code a little and what is happening:
I start by looping through my database.
The first .... is me doing some simple filtering of the results
Then the issues start. The first echo is me trying to put a hidden value of the row ID.
The value will NOT write. I've tried print, echo, I've tried assigning it to other variables but for whatever reason it will not write.
To make things more confusing: When I use that SAME variable in the third echo statement, it works! That function getData just accesses a database and returns the first and last name of the user, which it IS doing.
I'm extremely confused and have never come across an issue like this before.
Other things:
When I dump the $rowres it returns null, same with $rowres['rid']
I've tried echoing the variable in multiple places and none will echo but again, it works with the function because the function is returning the correct first and last name to each user.
You're using two different variable names:
$rowsres is what your assigning things to in the loop and what your passing into getData.
$rowres (note only one 's') is what you're trying to echo. This is a completely separate variable which has nothing assigned to it.