2015-01-04 4 views
1

내 서버에서 파일을 선택하고 서버와 데이터베이스에서 파일을 삭제하고 싶습니다. 그것은 또한 그렇게하지만 삭제할 때 무한한 시간에 시작됩니다. 그래서 무한 루프가 있으며 나는 그 이유를 이해하지 못했습니다. 여기PHP는 무한 쿼리를 만듭니다

<html> 
<body> 
<title>Delete your uploads</title> 

<?php 
session_start(); 
$username =$_SESSION["uname"]; 
?> 
<form action="deleted.php" method="post"> 

<?php 
$con = mysqli_connect("localhost", "root", "", "webpage"); 
if (mysqli_connect_errno()) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
$sql= mysqli_query($con, "SELECT imagesnotes FROM datas where username='$username'"); 
echo "File or Image"; 

echo'<select name="imagesnotes">'; 
echo'<option value="" selected="selected">Select a File</option>'; 

while($row = mysqli_fetch_array($sql)) 

{ 
    echo'<option value="' . $row['imagesnotes'] . '">'. $row['imagesnotes'] .'</option>'; 
} 
echo'</select></p><p>'; 

mysqli_close($con); 
?> 
<td width="80"><input name="download" type="submit" class="box" id="download" value=" download "></td> 
</form> 
</body> 
</html> 

그리고 그것을 확인 내 deleted.php 파일입니다 :

<?php 
session_start(); 
$username =$_SESSION["uname"]; 
?> 
<?php 
$con = mysqli_connect("localhost", "root", "", "webpage"); 
if (mysqli_connect_errno()) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
$imagesnotes = $_POST['imagesnotes']; 
    $target_dir = "uploads/"; 
    $target_file = $target_dir . basename($imagesnotes); 
$sql = mysqli_query($con,"delete from datas where username='$username' and imagesnotes='$imagesnotes'"); 

while($sql) 
{ 
$delete = unlink($target_file); 
if($delete) 
{ 
echo '<br>you deleted your file from our server</br>'; 
} 
else 
{ 
echo 'An error occured'; 
} 
$sql2 = mysqli_query($con,"delete from userdata where username='$username' and imagesnotes='$imagesnotes'"); 
if(!$sql2) 
{ 
echo "<br>we couldn't delete some of your data from the database please contact administrators.</br>"; 
} 
echo "<br> We deleted your files from server and database. <br>"; 
} 
if(!$sql) 
{ 
echo "<br>There is a problem with connection or our MySQL code, Please contact administrators.</br>"; 
} 
mysqli_close($con); 
?> 

모든 사실이 보인다는
여기 내 파일 삭제를 선택 내 delete.php입니다. 도와 줄수있으세요?

+0

'while ($ sql)'로 시작하는 루프가 있지만 '$ sql'을 수정하는 루프 내부에는 아무 것도 없습니다. –

+0

네, 감사했습니다. :) – crysispeed

+0

코드도 안전하지 않습니다. 몇 가지 SQL 주입이 있습니다. http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 – Ziumin

답변

1

$ sql 결과의 행 중 하나를 반복해야합니다.

$sql = mysqli_query($con,"delete from datas where username='$username' and imagesnotes='$imagesnotes'"); 

$loop = mysqli_num_rows($sql); 

for ($i=0, $i <= $loop, $i++) { 

$delete = unlink($target_file); 

if($delete) { 
echo '<br>you deleted your file from our server</br>';` 
} 
else 
{ 
echo 'An error occured'; 
} 
2

성공적으로 삭제 쿼리를 수행 할 때 Funcion mysqli_query는 항상 TRUE를 반환합니다. 이것은 무한 루프의 원인입니다.

단일 파일 이름 ($ _POST [ 'imagesnotes'])이있을 때 루프 결과가 필요하지 않습니다.

... 
$sql = mysqli_query($con,"delete from datas where username='$username' and imagesnotes='$imagesnotes'"); // $sql = TRUE on success 
$delete = false; 
if (file_exists($target_file)) { 
    $delete = unlink($target_file); 
} 
...