-2

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);
Felix Pamittan
  • 31,544
  • 7
  • 41
  • 67
mzml
  • 1
  • 1
  • 2
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 25 '16 at 02:03
  • Fatal error: Call to undefined function mysqli_connect() in..i got this error when use mysqli..why? – mzml Apr 25 '16 at 02:47

2 Answers2

1

remove last ")" in your query

$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";
Ing. Gerardo Sánchez
  • 1,607
  • 15
  • 14
0

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)";
Azlina T
  • 176
  • 2
  • 17