Discussion:
I am puzzled. Error on one site, no error on the other
Stephen
2013-10-26 00:27:09 UTC
Permalink
Problem Situation

I have two web sites on the same shared host. They share code for the
control panel. When executed for one site I get a warning (reproducible
always), but on the other there is no warning.

One my home server, set up in the same way, I do not get a warning for
either site.

The warning is from this code:

if ( in_array( $keys, $photo_ids ) )

*Warning*: in_array() expects parameter 2 to be array, null given in
*/home/rois3324/include/cpprocessforms.php* on line *203*

Steps

1) Photos are transferred to incoming directory using ftp.
2) Photo data is imported into database and files moved to web site's
file system
3) Photos are linked to a "category" by
i) Specifying photos to consider by entering filespec using wildcards
ii) User presented with photos
iii) User selects photos to be added to category and clicks process
button
iv) Form returns array of photo_ids (key in database table)
v) Form processor creates entry in "link" table that links
category_id to photo_id
vi) A check is made to detect and reject when the link already exists

This is where the error occurs

I have looked at the code, but I am at a total loss to figure out why I
have trouble on one site and not the other, even though they are using
the code. And my home development system has no problems.

I can't play trial and error on the development system.

Anyone have any ideas?

This is the code where the warning is triggered:

function linkphotos( $dbh, $x ) {

global $thumbsdirectory;

$ret_str = "";
$cat_id = $x['category'];
$photos = $x['list'];
$sql0 = "SELECT photo_filename FROM photographs WHERE photo_id = :id";
$sql1 = "SELECT photo_id FROM gallery_photos WHERE photo_category = :id";
$sql2= "INSERT INTO gallery_photos VALUES ( :id, :photo_id, :order )";

$stmt = $dbh->prepare($sql0);
try {
foreach( $photos as $keys=> $on) {
$stmt->bindValue(':id', $keys);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$filenames[$keys] = $thumbsdirectory . "/" . $row['photo_filename'];
}
} catch (PDOException $e) {
return 'Error selecting existing file names: ' . $e->getMessage();
}

$stmt = $dbh->prepare($sql1);
try {
$stmt->bindValue(':id', $cat_id);
$stmt->execute();
while ( list( $id ) = $stmt->fetch(PDO::FETCH_NUM)) {
$photo_ids[] = $id;
}
} catch (PDOException $e) {
return 'Error selecting existing photos: ' . $e->getMessage();
}

$stmt = $dbh->prepare($sql2);
try {
$stmt->bindValue(':id', $cat_id);
foreach( $photos as $keys=> $on) {
$ret_str .= htmlimage($filenames[$keys], $filenames[$keys] ) .
"<br />";
if ( in_array( $keys, $photo_ids ) ) { <<<<<<<<warning raised here
$ret_str .= " Duplicate. Already in Category.<br />";
} else {
$stmt->bindValue(':photo_id', $keys);
$stmt->bindValue(':order', $keys);
$stmt->execute();
$ret_str .= " Added to Category.<br />";
}
}
} catch (PDOException $e) {
return 'Error inserting new photos: ' . $e->getMessage();
}

return $ret_str;
}
--
Stephen
Aziz Saleh
2013-10-26 00:53:28 UTC
Permalink
Post by Stephen
Problem Situation
I have two web sites on the same shared host. They share code for the
control panel. When executed for one site I get a warning (reproducible
always), but on the other there is no warning.
One my home server, set up in the same way, I do not get a warning for
either site.
if ( in_array( $keys, $photo_ids ) )
*Warning*: in_array() expects parameter 2 to be array, null given in
*/home/rois3324/include/**cpprocessforms.php* on line *203*
Steps
1) Photos are transferred to incoming directory using ftp.
2) Photo data is imported into database and files moved to web site's file
system
3) Photos are linked to a "category" by
i) Specifying photos to consider by entering filespec using wildcards
ii) User presented with photos
iii) User selects photos to be added to category and clicks process
button
iv) Form returns array of photo_ids (key in database table)
v) Form processor creates entry in "link" table that links category_id
to photo_id
vi) A check is made to detect and reject when the link already exists
This is where the error occurs
I have looked at the code, but I am at a total loss to figure out why I
have trouble on one site and not the other, even though they are using the
code. And my home development system has no problems.
I can't play trial and error on the development system.
Anyone have any ideas?
function linkphotos( $dbh, $x ) {
global $thumbsdirectory;
$ret_str = "";
$cat_id = $x['category'];
$photos = $x['list'];
$sql0 = "SELECT photo_filename FROM photographs WHERE photo_id = :id";
$sql1 = "SELECT photo_id FROM gallery_photos WHERE photo_category = :id";
$sql2= "INSERT INTO gallery_photos VALUES ( :id, :photo_id, :order )";
$stmt = $dbh->prepare($sql0);
try {
foreach( $photos as $keys=> $on) {
$stmt->bindValue(':id', $keys);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC)**;
$filenames[$keys] = $thumbsdirectory . "/" . $row['photo_filename'];
}
} catch (PDOException $e) {
return 'Error selecting existing file names: ' . $e->getMessage();
}
$stmt = $dbh->prepare($sql1);
try {
$stmt->bindValue(':id', $cat_id);
$stmt->execute();
while ( list( $id ) = $stmt->fetch(PDO::FETCH_NUM)) {
$photo_ids[] = $id;
}
} catch (PDOException $e) {
return 'Error selecting existing photos: ' . $e->getMessage();
}
$stmt = $dbh->prepare($sql2);
try {
$stmt->bindValue(':id', $cat_id);
foreach( $photos as $keys=> $on) {
$ret_str .= htmlimage($filenames[$keys], $filenames[$keys] ) .
"<br />";
if ( in_array( $keys, $photo_ids ) ) { <<<<<<<<warning raised here
$ret_str .= " Duplicate. Already in Category.<br />";
} else {
$stmt->bindValue(':photo_id', $keys);
$stmt->bindValue(':order', $keys);
$stmt->execute();
$ret_str .= " Added to Category.<br />";
}
}
} catch (PDOException $e) {
return 'Error inserting new photos: ' . $e->getMessage();
}
return $ret_str;
}
--
Stephen
Your $photo_ids array is not declared. After
$photos = $x['list'];
add
$photo_ids = array();
David Harkness
2013-10-26 15:39:34 UTC
Permalink
Post by Stephen
if ( in_array( $keys, $photo_ids ) )
*Warning*: in_array() expects parameter 2 to be array, null given in
*/home/rois3324/include/**cpprocessforms.php* on line *203*
This happens when you allow PHP to create the array for you. Before
iterating through another (possibly empty) array and adding photo IDs
to $photo_ids,
you must assign an empty array to the variable. You need to add

$photo_ids = array();

at the top of the function. PHP will happily assign a new array to $photo_ids
if the variable is null in this loop:

while ( list( $id ) = $stmt->fetch(PDO::FETCH_NUM)) {
Post by Stephen
$photo_ids[] = $id;
}
But if that statement produces an empty result set, $photo_ids will remain
null.

Cheers,
David
Stephen
2013-10-27 22:01:00 UTC
Permalink
Post by Stephen
Problem Situation
if ( in_array( $keys, $photo_ids ) )
*Warning*: in_array() expects parameter 2 to be array, null given in
*/home/rois3324/include/cpprocessforms.php* on line *203*
Thank you to those who responded. Declaring the array did the job.
Should have seen that, but because I did not get the error in other
environments I overlooked what should have been obvious!
--
Stephen
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...