简体   繁体   中英

Error array, i cant get out the information - in mysql query

Hello i got a big trouble which i couldnt solve, this is a very weird problem :S i use the next code:

for( $m = 0; $m < sizeof( $jugadores ) ; $m++ )
{   
            $juga_q = "INSERT INTO jugadores VALUES($jugadores[$m][1],$nom_equipo,'',$jugadores[$m][4]);";
            $result = mysql_query( $juga_q );
            echo $juga_q."<br>";
}

i got information in the array which i want to insert into my data base, for that i dump my query in $juga_q variable which i use in the insert into, but the query doesnt execute and i make an echo to check whats failing and got the next:

 INSERT INTO jugadores VALUES(Array[1],Aston Villa,'',Array[4]);

when i make a echo of the array out of this query i got no problems, thats why im getting crazy with that and hope u could help me.

Thanks forward!!

try to concatenate it,

$juga_q = "INSERT INTO jugadores VALUES(". $jugadores[$m][1].",$nom_equipo,'',".$jugadores[$m][4].");";

As a sidenote, the query is vulnerable with SQL Injection if the value( s ) of the variables came from the outside. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.

When using array accessors in strings, you need to wrap them in brackets.

Instead of "VALUES ($jugadores[$m][1],..." , try using "VALUES ({$jugadores[$m][1]},..." .

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