mzml mzml - 7 months ago 14
SQL Question

Could not enter data: You have an error in your SQL syntax;

Can someone help me out from this problem?
Here is my PHP code but I get stuck with the error:


Could not enter data: You have an error in your SQL syntax; check the
manual that corresponds to your MariaDB server version for the right
syntax to use near ')' at line 1


How to fix it?

$sql = "INSERT INTO pemohon2(p_id,k_nom_siri,p_jenis_aset,p_pengguna_terakhir,p_tarikh_rosak) SELECT P_ID,K_nom_siri,P_jenis_aset,P_pengguna_terakhir,P_tarikh_rosak FROM pemohon )";

mysql_select_db('kenderaan');
$retval = mysql_query( $sql, $conn );

if(! $retval ) {
die('Could not enter data: ' . mysql_error());
}

mysql_close($conn);

Answer

First of all, please use mysqli instead mysql as it is deprecated. For instance,

$con=mysqli_connect("localhost","my_user","my_password","my_db");

Second, you are missing word "values" inside query.

$sql = "INSERT INTO pemohon2 values(p_id,k_nom_siri,p_jenis_aset,p_pengguna_terakhir,p_tarikh_rosak) ;

lastly, your variable reference is not assign inside PHP. For instance,

$p_id=p_id;
$k_nom_siri=k_nom_siri;    

$sql = "INSERT INTO pemohon2($p_id,$k_nom_siri)";