Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi, I'm trying to insert a data to a table. some of the field that i want to insert are come from that table. so i create a temporary variable to define the data, then call the variable in the query. when i try to execute from sqlyog it's fine but when i try to execute it using php i get an error message
Quote:
ERROR 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 'SELECT @lvl := level+1 from eslii_logicalframework where id_framework='5'; ' at line 2


my php code

$sq_insert="SELECT @kdfrm := CONCAT(kode_framework,'.".$nomor_urut."') from eslii_logicalframework where id_framework='".$lookup_id."';
							SELECT @lvl := level+1 from eslii_logicalframework where id_framework='".$lookup_id."';
							insert into eslii_logicalframework
							(
								kode_framework,
								no,
								level,
								id_jenis,
								logical_framework,
								indikator_framework,
								unitkey
							)values
							(
								@kdfrm,
								'".$nomor_urut."',
								@lvl,
								'".$data_post_select."',
								'".$data_post."',
								'".$data_post_ind."',
								'".$unitkey_login."'
							);";

	             
			    if (!$result = mysqli_query($conmysql,$sq_insert)) {
			        echo"<div class='alert alert-mini alert-danger margin-bottom-30'> ERROR  ".mysqli_error($conmysql)."</div>";
			    }


What I have tried:

i only try to execute the php code
Posted
Updated 26-Nov-18 17:44pm

1 solution

Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900