2017-12-27 1 views
-1

PHP와 MySQLI를 계속 사용하면서 하나의 PHP 페이지에서 여러 개의 MySQLI 삭제 쿼리를 호스팅 할 수 있는지 궁금해했으며 혼란없이 해당 쿼리에 URL을 적용했습니다. 여기에이 코드가 있습니다. 웹에서 3 일 동안의 답변을 검색했지만 찾으려는 것이 내가 무엇을 찾고있는 것 같지는 않았습니다. 어떤 도움이나 적절하게 설명 된 예제를 기반으로 작성하면 크게 감사하겠습니다.하나의 PHP 페이지에서 다중 mysqli 삭제 코드

내 코드는 다음과 같습니다

<?php 
session_start(); 
include ('dbconnect.php'); 
$userr = $_SESSION['usr_id']; 
$uid=$_GET['pid']; 
$pid=$_GET['phid']; 
$user_id=$_GET['user']; 
$fileID=$_GET['file']; 
if($userr==$uid && $pid==$fileID){ 
    echo "ERROR No. 9B2AP"; 
    }else{ 
    mysqli_query($con,"delete from images where userID='$uid' AND PhotoID='$pid'") or die (mysqli_error()); 
    header ("location: /viewImage.php?pid=$uid"); 
} 

if($userr==$user_id && $fileID==$pid){ 
    echo "ERROR No. 39V41"; 
    }else{ 
    mysqli_query($con,"delete from music where userID='$user_id' AND Record_ID='$fileID'") or die (mysqli_error()); 
    header ("location: /users/music-gallery/$user_id"); 
} 
?> 

가 아무리 내가이 페이지의 코드를 사용하여 이미지 나 노래를 삭제이 코드를 재 작성 횟수, 그것은 단지 대신 적절한의 /users/music-gallery/에 저를 리디렉션하지 관련 페이지. 이 문제를 어떻게 해결할 수 있습니까? 내가 말했듯이, 나는 PHP와 MySQLI의 새로운 요정이며, 세부 사항에서 설명해야한다고 생각하는 모든 제안을 통해 내가 실수를 한 방법을 이해하고 이해할 수 있으며 나중에 코드에서 다시 발생하지 않도록 수정하고 방지 할 수 있습니다. 제발 고마워. 보안상의 이유로이하지 않는 경우 -MS

+0

바로이 같은 각 headerexit();을 사용할 필요가 헤더 후 exit 프로그램으로

:

가 문제를 해결하려면 '$ _GET [ 'file']'이것은 파일 처리를 제안합니다. POST 메소드와 유효한 enctype이 필요합니다. 오류보고 및 쿼리와 관련된 오류를 확인하십시오. mysqli_error()는 인수로 db 커넥션을 요구한다. –

+0

여기에 심각한 SQL 인젝션이 열려 있습니다. 알고 계셨습니까? –

답변

2

:

$uid = mysqli_real_escape_string($con, $_GET['pid']); 

당신은 확인 FILTERS을 사용할 수 있습니다 : 당신이 MySQLi을 사용하고 있기 때문에

// Consider escaping the incoming data for better security 
$uid=$_GET['pid']; 
$pid=$_GET['phid']; 
// ... 

을 당신은 당신의 데이터를 탈출하기 위해 사용할 수 있습니다 입력 데이터 유형 :

// If you are expecting an `INT` from $_GET['pid'] 
if (filter_var($_GET['pid'], FILTER_VALIDATE_INT)) 
{ 
    echo 'pid is an int'; 
} 
else 
{ 
    echo 'pid is not an int'; 
} 

필터 here.

모두의 가장

, stmtprepared mysqli statements를 사용

// prepare the statement 
$stmt = $con->prepare("delete from images where userID=? AND PhotoID=?"); 
// bind variables to the '?' and set types --> `i` = integer 
$stmt->bind_param("ii", $_GET['pid'], $_GET['phid']); 
// execute query 
$stmt->execute(); 

// Do the same for the next query 

더 준비된 문 here합니다.

header ("location: /viewImage.php?pid=$uid"); 
exit(); 

예를 들어 :

header ("location: /viewImage.php?pid=$uid"); 
// this line of code gets exucuted 
// this too 
// ... 

header ("location: /viewImage.php?pid=$uid"); 
exit(); 
// Nothing gets executed as program terminates and redirects 
+0

제안되었지만 문제가있는 것 같습니다. –

+0

완벽하게 작동했습니다. ty – vampnerdlord

+0

@vampnerdlord 그것은 당신을 위해 일해서 다행입니다. – Ivan86