
mysqli::real_escape_string 함수는 MySQLi 확장 함수로, SQL 인젝션 공격을 방지하기 위해 사용됩니다. 이 함수를 사용하기 전에, 데이터베이스 연결을 먼저 생성해야 합니다.
#hostingforum.kr
php
$conn = new mysqli($host, $username, $password, $database);
데이터를 escape 하기 전에, mysqli::real_escape_string 함수를 사용하여 데이터를 escape 합니다.
#hostingforum.kr
php
$escaped_data = mysqli_real_escape_string($conn, $data);
INSERT 문에서 값을 대입할 때는, 변수를 사용할 때는 다음과 같이 사용합니다.
#hostingforum.kr
php
$query = "INSERT INTO table_name (column1, column2) VALUES ('$escaped_data', '$escaped_data')";
$conn->query($query);
또는, prepared statement을 사용할 때는 다음과 같이 사용합니다.
#hostingforum.kr
php
$stmt = $conn->prepare("INSERT INTO table_name (column1, column2) VALUES (?, ?)");
$stmt->bind_param("ss", $escaped_data, $escaped_data);
$stmt->execute();
prepared statement을 사용하는 경우, SQL 인젝션 공격을 방지할 수 있습니다.
2025-03-04 10:02