확인 된 값을 PHP 테이블에 삽입했는데 두 개 이상의 항목을 선택하면 두 개 이상의 성공 또는 실패 메시지가 표시됩니다.
이것은 데이터베이스에 삽입하는 코드입니다.
//insert.php
include_once 'includes/dbconnect.php';
if(!empty($_POST['perms']))
{
$output = '';
$perms = $_POST['perms'];
$role = mysqli_real_escape_string($conn,$_POST['role']);
$module = mysqli_real_escape_string($conn,$_POST['module']);
foreach ($perms as $perm) {
$query = "INSERT INTO roles_permissions (role_id,module_id,perm_id)
VALUES ('$role','$module','$perm')";
if(mysqli_query($conn, $query))
{
$output .= '
<script>
bootbox.alert("<h4>Success:</h4> <p>Permissions have been added to selected role successfully </p>",function(){
window.location.reload(); });
</script>';
} else {
$output .= '
<script>
bootbox.alert("<h4>Sorry!</h4><p>Operation was not successful, Please cross check and try again.</p> ",function(){ window.location.reload(); });
</script>';
}
}
echo $output;
}
?>
사전에 도움을 주셔서 감사합니다.
메시지를 작성하는 코드가 foreach 루프 내부에 있기 때문에 성공 메시지가 두 개 이상 수신됩니다. 하나의 메시지 만 원할 경우 루프 밖으로 이동해야합니다.
<?php
//insert.php
include_once 'includes/dbconnect.php';
if(!empty($_POST['perms'])) {
$output = '';
$perms = $_POST['perms'];
$role = mysqli_real_escape_string($conn,$_POST['role']);
$module = mysqli_real_escape_string($conn,$_POST['module']);
foreach ($perms as $perm) {
$query = "INSERT INTO roles_permissions (role_id,module_id,perm_id)
VALUES ('$role','$module','$perm')";
$result = mysqli_query($conn, $query);
// if we get an error stop the loop
if ( ! $result) { break; }
}
if ($result) {
$output = '<script>
bootbox.alert("<h4>Success:</h4> <p>Permissions have been added to selected role successfully </p>",function(){
window.location.reload(); });
</script>';
} else {
$output = '<script>
bootbox.alert("<h4>Sorry!</h4><p>Operation was not successful, Please cross check and try again.</p> ",function(){ window.location.reload(); });
</script>';
}
echo $output;
}
?>
또한 스크립트는 SQL 주입 공격에 열려 있습니다. 입력을 이스케이프 하더라도 안전하지 않습니다! 연결된 값 대신 또는 API 에서 준비된 매개 변수화 된 문 을 사용하는 것을 고려해야 합니다.
MYSQLI_
PDO
준비된 명령문을 사용할 때 잠재적 인 속도 이점이 있습니다. 쿼리를 준비하고 (준비는 기본적으로 컴파일 및 최적화를 의미 함) 매개 변수 값을 변경 한 후 여러 번 재사용 할 수 있기 때문입니다.
따라서 코드는 다음과 같습니다.
<?php
//insert.php
include_once 'includes/dbconnect.php';
if(!empty($_POST['perms'])) {
$output = '';
$query = "INSERT INTO roles_permissions
(role_id,module_id,perm_id) VALUES (?,?,?)";
$stmt = $conn->prepare($query);
foreach ($perms as $perm) {
// for the purpose of this example I assumed all 3 values were integers
$stmt->bind_params('iii', $_POST['role'], $_POST['module'], $prem);
$result = $stmt->execute();
// if we get an error stop the loop
if ( ! $result) { break; }
}
if ($result) {
$output = '<script>
bootbox.alert("<h4>Success:</h4> <p>Permissions have been added to selected role successfully </p>",function(){
window.location.reload(); });
</script>';
} else {
$output = '<script>
bootbox.alert("<h4>Sorry!</h4><p>Operation was not successful, Please cross check and try again.</p> ",function(){ window.location.reload(); });
</script>';
}
echo $output;
}
?>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다