0

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!!

John Conde
  • 217,595
  • 99
  • 455
  • 496

2 Answers2

1

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.

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492
1

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]},...".

helmbert
  • 35,797
  • 13
  • 82
  • 95