2014-11-29 1 views
0

이 코드는 램프 스택에 설정됩니다. 삭제 페이지에 문제가 있습니다. PHP의 삭제 기능은 mysql 테이블의 항목이 숫자 일 때만 작동합니다. 항목에 관계없이 행을 삭제해야합니다. 전자 메일 필드는 mysql에서 "varchar (25)"로 설정됩니다. 코드에서 변수를 정의하지 않아서 왜 숫자로 제한되는지 이해할 수 없습니다. 여기 항목이 숫자 인 경우에만 PHP가 행을 삭제합니다.

페이지의 :

<!DOCTYPE HTML> 
<html> 
<head> 

</head> 
<body> 
<div id="gradientbackground"> 

    <table class="beachpictures" align="center" border="1"> 
     <thead> 

     <tr> 
      <th>Name</th> 
      <th>Email</th> 
      <th>Website</th> 
      <th>Gender</th> 
      <th>Delete</th> 
     </tr> 
     </thead> 
     <tbody> 
    <?php 

     require 'project_db.php'; 
     mysql_connect("$servername", "$username", "$password")or die("cannot connect"); 
     mysql_select_db("$database")or die("cannot select DB"); 

     //execute the SQL query and return records 
     if (!$result = mysql_query("SELECT * FROM $table")) 
     echo 'mysql error: ' .mysql_error(); 

     //fetch tha data from the database 
     while ($row = mysql_fetch_array($result)) { 
    ?> 

     <tr> 
      <td><?php echo $row['Name']; ?></td> 
      <td><?php echo $row['Email']; ?></td> 
      <td><?php echo $row['Website']; ?></td> 
      <td><?php echo $row['Gender']; ?></td> 
      <td class="record-delete"> 
       <form action='delete.php?Email="<?php echo $row['Email']; ?>"' method="post"> 
        <input type="hidden" name="Email" value="<?php echo $row['Email']; ?>"> 
        <input type="submit" name="submit" value="Delete"> 
       </form> 
      </td> 
     </tr> 
    <?php } 
    ?> 
     </tbody> 
    </table> 
<br> 
</div> 
</body> 
</html> 

그리고 여기에 실제 쿼리 수행 delete.php 파일입니다 :

<?php 
    require 'project_db.php'; 
    mysql_connect("$servername", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$database")or die("cannot select DB"); 

//Define the query 
$query = "DELETE FROM $table WHERE Email={$_POST['Email']} LIMIT 1"; 

//sends the query to delete the entry 
mysql_query ($query); 

if (mysql_affected_rows() > 0) { 
//if it updated 
?> 
      <strong>Record Has Been Deleted</strong><br /><br /> 
<?php 
} else { 
//if it failed 
?> 
      <strong>Deletion Failed</strong><br /><br /> 
<?php 
} 


?> 

답변

0

당신은 따옴표를 추가 할 필요가 : 그렇지 않으면, MySQL은 $ _POST [ '을 기대 이메일]을 숫자로 사용하십시오.
기능이 지원되지 않으며 SQL injection으로 열었 기 때문에 PDO 또는 MySQLi으로 전환해야합니다. 코드는 다음과 같아야합니다.

<?php 
    require 'project_db.php'; 
    $connect = mysqli_connect($servername,$username,$password,$database)or die("cannot connect"); 

//Define the query 
$query = "DELETE FROM $table WHERE Email='".mysqli_real_escape_string($connect,$_POST['Email'])."' LIMIT 1"; 

//sends the query to delete the entry 
$execute = mysqli_query ($connect,$query); 

if (mysqli_affected_rows($execute) > 0) { 
//if it updated 
?> 
      <strong>Record Has Been Deleted</strong><br /><br /> 
<?php 
} else { 
//if it failed 
?> 
      <strong>Deletion Failed</strong><br /><br /> 
<?php 
} 


?> 
+0

위대한 작품입니다! 고맙습니다!! 남은 유일한 문제는 실제로 성공했지만 "삭제 실패"를 출력한다는 것입니다. – Noor

+0

@Noor you 're welcome :) 그것은 내 잘못입니다. 죄송합니다. 내 대답을 업데이트했습니다. 이제는 작동 할 것입니다. – Stubborn

+0

Thanks @Stubborn, 그것은 그것을 고치지 못했지만 그 다음에는 2 문장을 뒤집었고 훌륭하게 작동합니다 - 정말 고마워요 !! – Noor

관련 문제