简体   繁体   中英

Passing array values in php prepared statement not working

So I have a prepared statement with bind_param that works fine if I assign each piece of data to individual variables and use those variables in the bind_param statement but it doesn't work if I assign each piece of data to an array element and try to use the array elements in the bind_param statement. Examples below:

This works: $insertquery->bind_param("sssssssssssssssssssss", $dbname, $dbmanacost, $dbcmc, $dbcolors, $dbtype, $dbsupertypes, $dbtypes, $dbsubtypes, $dbrarity, $dbcardtext, $dbflavortext, $dbartist, $dbnumber, $dbpower, $dbtoughness, $dblayout, $dbmultiverseid, $dbexpansion, $dbexpansioncode, $dbreleasedate, $dbversions);

But this inserts a blank line into the database: $insertquery->bind_param("sssssssssssssssssssss", $db['name'], $db['manacost'], $db['cmc'], $db['colors'], $db['type'], $db['supertypes'], $db['types'], $db['subtypes'], $db['rarity'], $db['cardtext'], $db['flavortext'], $db['artist'], $db['number'], $db['power'], $db['toughness'], $db['layout'], $db['multiverseid'], $db['expansion'], $db['expansioncode'], $db['releasedate'], $db['versions']);

The content of the variables is exactly the same in both examples. I would like to use a single array for organizational purposes but I'm not sure why the prepared statement won't read the data.

The problem was that each time I looped through I was resetting the array by redeclaring it, as in, $db = array(...) but apparently that breaks the link from the prepared statement. I rewrote the code to blank each array element using a foreach loop and that fixed it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM