I am posting a form to a script with this code:
foreach($_POST as $k=>$v){
if(!is_array($v)){$$k=dbenc($v);} else{$$k=$v;}
$_SESSION["cat"][$k]=$v;
}
$url=vlookup("url","cat","cat=$cat");
// $cat is an integer which originated from the same field value and has been re-posted
"vlookup" is:
function vlookup($field,$table,$criteria){
global $link;
if($table<>""){$from="FROM $table";}
if($criteria<>""){$criteria="WHERE $criteria";}
$r=mysqli_query($link,"SELECT $field as v $from $criteria") or die("VLOOKUP FAILED: $field $from $criteria".mysqli_error($link));
$n=mysqli_num_rows($r);
if($n>0){
$row=mysqli_fetch_assoc($r) or die(mysqli_error($link));
return $row["v"];
}
}
The problem is that
print($cat);exit;
gives the expected result, but $cat does not appear in function output such as
showerror("$cat not found");
and the "vlookup" relying on it produces an error suggesting there is nothing after the =
Printing the query in the error report confirms the absence of the $cat value in the vlookup function.
This makes no sense!
Any thoughts or advice appreciated.
As per my comment above, the problem seems to be the use of false in
if (strpos($value," ")!==false && ...)
after the vlookup using $cat
If I test for spaces without using !==false in combination with other tests, dynamic variables appear as normal. Not sure if this is to be expected?
Related
There are two columns in the database table "system". I have the systemId and want to get the mobileSystemId. But the variable $mobileSystemIds which I already defined as global is always empty.
EDIT: Now array_map doesn´t work. I always get my Exception output "Arrayfehler ArrayMap"
I have the following code :
$mobileSystemIds=array();
function getMobileSystemId($systemId)
{
global $mysqli;
global $mobileSystemIds;
$query="SELECT mobileSystemId FROM system WHERE systemId ='" .$systemId ."'";
if(!$result=$mysqli->query($query))
{
echo "Datenbankfehler DB-QUery";
exit(0);
}
if (!$mobileSystemId=$result->fetch_assoc())
{
echo "Datenbankfehler DB-Fetch";
exit(0);
}
$mobileSystemId=$mobileSystemId["mobileSystemId"];
echo "mobile System ID: " .$mobileSystemId ."<br />";
return $mobileSystemId;
}
if(!$mobileSystemIds=array_map("getMobileSystemId",$systemList))
{
echo "Arrayfehler ArrayMap";
}
In this case, using a return in your function would be much cleaner.
Nothing to do with your problem, but is your $systemId var trusted ? (To prevent SQL injection).
Update:
if(!$mobileSystemIds=array_map("getMobileSystemId",$systemList))
{
echo "Arrayfehler ArrayMap";
}
ought to read (just checked; it works for me):
$mobileSystemIds = array_map('getMobileSystemId', $systemsList);
if (empty($mobileSystemIds))
{
if (empty($systemsList) || !(is_array($systemsList)))
echo "OK: no mobile IDs, but no systems either";
else
echo "THIS now is strange :-(";
}
else
{
echo "Alles OK";
var_dump($mobileSystemIds);
}
I tried this by returning a dummy value based on input; if it does not work for you, there must be something strange in the database.
(Update: the text below refers to your original code, which did not use array mapping)
Your code ought to be working as it is. You put several $mobileSystemId 's into a single $mobileSystemId.
It works: I tested with a simpler code, removing the DB calls but leaving your code, and spelling, untouched.
So, the error must be elsewhere. I would guess that this code is included into something else, and:
the $mobileSystemIds = array(); declaration gets executed more than once, thereby losing all its data;
the $mobileSystemIds = array(); declaration is itself included in a more local scope and you read it from outside, reading an empty value or a totally different value.
Try replacing the first part of your code with:
GLOBAL $mobileSystemsIds;
if (defined($mobileSystemsIds))
trigger_error("mobileSystemsId defined more than once", E_USER_ERROR);
else
$mobileSystemsIds = array();
and also, in the function body:
if (!defined($mobileSystemsId))
trigger_error("mobileSystemsId should have been defined", E_USER_ERROR);
Why is this warning occuring when I am using IE?
Warning: Invalid argument supplied for foreach()
It works in all other browsers..
The function for the loop:
function wdsearch(PDO $dbh){
if(!isset($_POST['wdsubmit'])) {
} else {
$term = $_POST['wdsearchvalue'];
$stmt = $dbh->prepare("
SELECT *
FROM posts
WHERE category = :designer
AND (full_text LIKE CONCAT('%', :term, '%')
OR heading LIKE CONCAT('%', :term, '%'))
ORDER BY post_date DESC
");
$designer = 'Designer';
$stmt->bindParam(":designer", $designer);
$stmt->bindParam(":term", $term);
$stmt->execute();
return $stmt->fetchAll();
}
}
wdsearch($dbh);
$wdsearch = wdsearch($dbh);
And the loop goes here..
<?php foreach($wdsearch as $wds) : ?>
<!-- HTML here -->
<?php endforeach; ?>
Any possible IE related warnings? Like I said all other browsers can handle it..
The problem is in your wdsearch function; if $_POST['wdsubmit'] is not set, it returns nothing and if anything goes wrong in any of the db operations it will not return an array either.
You need to add some error handling to your db operations and in case of failure or a non-post, return an empty array and / or check if $wdsearch is an array before you use foreach.
The specified error is occurring in your PHP code, not in any specific browser.
The issue is, the datatype of $wdsearch, does not implement an iterator. This could be caused by $wdsearch being null, a string/number/etc. - or simply an unsupporting object.
Try verifying if the data is an array before going into the foreach loop with is_array($wdsearch), or verify the variable is not null with isset($wdsearch). If you believe there is valid data there, check to see if the object in $wdsearch actually supports iterators and, if not, update it to implement!
Example:
<?php
if (isset($wdsearch)):
foreach($wdsearch as $wds) :
?>
<!-- HTML here -->
<?php
endforeach;
endif;
?>
If for the same parameters it works in Chrome and FF, but not IE.
What are exactly are you submitting in IE. Is there some silly thing like a button with picture submit? That would pass your terms as the location of the click, not actual value for example.
Debug what your incoming parameters in $POST are before outputting to foreach. Also how do you handle null result sets?
I use the latest code igniter (2.0.3) and php-active 0.0.1.
All are working fine except save();
Code:
if($_POST)
{
$entry= Customers::find_by_routeid('4');
$entry->routeid=5;
$entry->save();
}
Here's my problem: for some reason that I cannot understand the above code does not work, but if I take the code out of if ($_POST), it works fine.
What I am doing wrong?
EDIT:
Thanks Damien Pirsy $this->input->post() does the trick, but when I uncomment the comments in the code the problems returns.
The code now is:
if($this->input->post())
{
$id = $this->input->post('id');
$oldRoute = $this->input->post('oldRoute');
$newRoute = $this->input->post('newRoute');
$entry= Customers::find_by_routeid($this->input->post('oldRoute'));
$entry->routeid=$this->input->post('newRoute');
$entry->save();
/*
if($oldRoute<$newRoute)
{
for ($i=$newRoute; $i>$oldRoute; $i--)
{
$element = Customers::find_by_routeid($i);
echo $element->routeid -= 1;
$element->save();
}
}
*/
}
The elements new IDs ($element->routeid -= 1;) are echoing right, but I have the same problem as in the beginning and neither of two saves work.
You didn't provide much details or debug info, so I'll just guess: try using the CI's native post handler instead. You should have var_dump()ed the $_POST array, see if isset() or not, also, since you're using it as a condition
if($this->input->post())
{
//...
}
UPDATE:
Since we're talking about Post variables, don't assume they're exactly as you want them. Keep in mind that $this->input->post('field') returns FALSE when the index is not present; that might well brake your if condition.
Assuming you need numbers to do this, you can do a check like
if($this->input->post('newRoute') AND is_numeric($this->input->post('newRoute'))
{
$newRoute = $this->input->post('newRoute');
}
else
{
// give it a default value, or raise an error, for example. If you need this
// variables, and need them to be numbers, you cannot go on in case these
// conditions are not met, right?
}
And the same for $oldRoute.
And yeah, OK, maybe you can write a cleaner code than mine, but you get the picture ;)
I am using the great tutorial provided by Nodstrum. I am attempting to autofill multiple text fields with PHP, MYSQL, and AJAX. I have a PHP script, here is the line of code returning my results:
echo '<li onClick="fill(\''.$result->name.'|'.$result->id.'\');">'.$result->name.'</li>';
Notice that I am seperating my results with a pipestem character.
Here is the function where I am receiving the error 'Undefined or not an object' I am breaking out the values and using the pipestem as splitting the values from mysql.
function fill(thisValue) {
myvalues=thisValue.split('|') {
$('#inputString').val(myvalues[0]);
$('#email').val(myvalues[1]);
}
window.setTimeout("$('#suggestions').hide();", 200);
}
If I 'ok' the error messages, I will eventually see both values displayed in the text fields, so I believe I am retreiving the values properly from MySQL. I appreciate any help anyone can provide to get me steered in the right direction, or a fresh perspective.
Thanks Again,
--Matt
The value you are passing that becomes thisValue is null or undefined. You can test this parameter before blindly trying to .split it (the split function works only on strings).
function fill(thisValue) {
// "value" will always be a string
var value = thisValue ? String(thisValue) : '';
// this line will not generate an error now
var myvalues=value.split('|');
// but these ones might! make sure the length of myvalues is at least 2
if (myvalues.length >= 2) {
$('#inputString').val(myvalues[0]);
$('#email').val(myvalues[1]);
}
// this might need to go inside the above if
window.setTimeout("$('#suggestions').hide();", 200);
}
Try this:
function fill(thisValue) {
myvalues=thisValue.split('|');
$('#inputString').val(myvalues[0]);
$('#email').val(myvalues[1]);
window.setTimeout("$('#suggestions').hide();", 200);
}
I am trying to insert messages to a function
function addMessage($item) {
if ($result) {
$message = '<p class="ok">
<span> Item added </span>
</p>
';
header("Refresh: 2; url=?page=$item");
}
else{
$message = '<p class=not><span>There is an error blah blah</span></p>';
}
return $message;
}
When I use it : addMessage('contents') it only returns to second condition. How can I fix this?
You are checking $result inside the if but its neither been assigned any value before that nor been declared as global . I think you meant to check $item:
if ($item) {
Hi jasmine
Your function always returns the second condition because you haven't assigned a value to $result, eider inside the function or when you call the function (like unicornaddict mentioned by other words).
To get your code working the way you probably want, your function should be like this:
function addMessage($item, $result) {
if ($result) { // It will return this condition, case $result has any value assigned and is different from FALSE (boolean)
$message = '<p class="ok">
<span> Item added </span>
</p>
';
header("Refresh: 2; url=?page=$item");
}
else{ // It will return this condition, case $result doesn't has any value assigned or is equal to FALSE (boolean)
$message = '<p class="not"><span>There is an error blah blah</span></p>';
}
return $message;
}
And then you can call the function like you where already calling it, but don't forget to include a variable or a value that should be handled as the $result variable inside the function
addMessage('contents', $result);
Note:
In your $message variable you have <p class=not> and should be <p class="not">.
Remember that header() must be called before any actual output is sent to the browser.
Hope it Helps.
Is $result defined in your script? Use if ($item) instead.
Be very careful that PHP allows the usage of undefined variables.
what they said :-)
Btw, a decent IDE (like Zend) will analyze your code and warn you about things like that.
Such static code analysis is known as "linting", so google for "PHP lint" or see questions like Is there a static code analyzer [like Lint] for PHP files?
But this code sample is so small that I guess you are a beginner (no offence interned - we all had to start somewhere), so do a lot of reading and gather a lot of tools and experience.
For instance, a decent IDE (like Zend or Eclipse PDT) would let you step through your code, line nby line, and examine the value of each variable and then you ought to have seen the problem.
Welcome to PHP and good luck!