Reputation: 225
i want to insert array to mysql db using php..
and now i getting error with this code :
$insert_sql = sprintf("INSERT INTO detail_paket (id_paket, menu_paket) select
paket.id,\'%s\' from paket where paket.nama_paket='$nama_paket'",%data1[$i]);
^
|
here is the error syntax
data1 is the array..
what is the correct syntax to use \'%s\' ?
when i trying to run the query, the error info show : syntax error, unexpected '%' in or syntax error unexpected "" in*.
i already tried using \"%s\ and \"%s"\ and %s and '%s' and "%s" and \"%s\" but the query still give me error..
and one last question, what is this '%s' syntax called ?
thanks...
original query from tutorial
$insert_sql = sprintf("INSERT INTO student (first_name, last_name) VALUES(\"%s\",\"%s\")",
$student_record[$i][0], $student_record[$i][1]);
Upvotes: 0
Views: 118
Reputation: 64526
Remove the quotes around %
and change the %
prefix of data1
to $
:
$insert_sql = sprintf("INSERT INTO detail_paket (id_paket, menu_paket) select paket.id,%s from paket where paket.nama_paket='$nama_paket'",$data1[$i]);
The other problem is you need to separate the two queries with a ;
. This won't work on all MySQL APIs, not all of them allow multiple queries. You might need to run two queries instead of a single multiple query.
Upvotes: 1
Reputation: 73
If you must do it this way, you can replace '%s' with '{$data1[$i]}' and get rid of the sprintf. Although I think it is more secure to use prepared statements http://php.net/manual/en/pdo.prepared-statements.php
Upvotes: 0