2016-06-30 2 views
0

데이터베이스에서 데이터를 가져 와서 표에 표시한다고 가정합니다. 모든 기록에는 영수증 번호를 입력하고 제출할 수있는 텍스트 상자가 있습니다. 제출시 해당 레코드의 데이터베이스에 영수증 번호를 삽입해야하며 텍스트 필드 대신 삽입 된 레코드를 표시하는 페이지가 다시로드됩니다. 나는 내가 뭘 잘못했는지 모르겠다. 왜냐하면 페이지가 빈 페이지로로드되기 때문이다.PHP HTML 표에서 양식 제출

$sql = "SELECT * FROM customer"; 
$result = $conn->query($sql); 

if ($result->num_rows > 0) { 

    echo " <table><tr><th>ID</th><th>Name</th> 
<th>Amount</th><th>TransactionID</th><th>Mobile Number</th> 
<th>Time Paid</th><th>Account</th> 
<th>Receipt Number</th></tr>"; 

    // output data of each row 
    while($row = $result->fetch_assoc()) { 
     echo "<tr><td>" . $row["id"]. "</td> 
<td>" . $row["name"]. "</td><td>" . $row["amount"]. "</td> 
<td>" . $row["trans_id"]. "</td><td>" . $row["msisdn"]. "</td> 
<td>" . $row["time_paid"]. "</td> 
<td>" . $row["status"]. "</td> 
<td><form action= 'receipt.php' method='post'> 
    <input type ='text' name='receipt'> 
    <input type ='hidden' name='hidden' value='".$row["receipt"]."'> 
    <input type='submit' name='submit' value='submit'></td></form></tr>"; 
} 
echo "</table> "; 


// receipt.php 

if(isset($_POST['submit'])) 
    { 

//connection 

$sql = "INSERT INTO customer (receipt, date_entered) VALUES ('" . $_POST['receipt'] . "','NOW()')"; 


if ($conn->query($sql) === TRUE) { 

    // echo "New record created successfully"; 
} else { 
    echo "Error: " . $sql . "<br>" . $conn->error; 
} 

$conn->close(); 


header('Location: http://localhost/com/payf.php'); 
} 
    else { 
    echo "Please Enter the Receipt Number"; 
    header('Location: http://localhost/com/payf.php'); 
    } 
+0

데이터베이스에서 가져온 모든 레코드에 나타납니다. –

+0

데이터베이스에서 가져온 레코드에만 표시되어 이상적으로 각 레코드를 개별적으로 처리 할 수 ​​있어야합니다. –

+0

당신이이 조건에 부합하지 않는 것 같네요. if ($ result-> num_rows> 0) {결과 num 행을 에코하여 해당 조건을 충족하는지 확인하십시오. – Jeff

답변

0

당신의 receipt.php 파일에; 의견에 따라이 코드에서

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"> 
    <title>title</title> 
    </head> 
    <body> 
<?php 

//NEW 
    $servername = "example.com"; 
    $username = "username"; 
    $password = "password"; 
    $dbname = "database"; 

    // Create connection 
    $conn = new mysqli($servername, $username, $password, $dbname); 
    // Check connection 
    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 
//NEW 

$sql = "SELECT * FROM customer"; 
$result = $conn->query($sql); 


if ($result->num_rows > 0) { 

    echo " <table><tr><th>ID</th><th>Name</th> 
<th>Amount</th><th>TransactionID</th><th>Mobile Number</th> 
<th>Time Paid</th><th>Account</th> 
<th>Receipt Number</th></tr>"; 

    // output data of each row 
    while($row = $result->fetch_assoc()) { 
     echo "<tr><td>" . $row["id"]. "</td> 
<td>" . $row["name"]. "</td><td>" . $row["amount"]. "</td> 
<td>" . $row["trans_id"]. "</td><td>" . $row["msisdn"]. "</td> 
<td>" . $row["time_paid"]. "</td> 
<td>" . $row["status"]. "</td> 
<td><form action= 'receipt.php' method='post'> 
    <input type ='text' name='receipt'> 
    <input type ='hidden' name='tid' value='".$row["id"]."'> 
    <input type='submit' name='submit' value='submit'></td></form></tr>"; 
} 
echo "</table>"; 
} else { //FIX ************ 
    echo "no results"; 
} 
// receipt.php 

if(isset($_POST['submit'])) { 
    //connection 
    //$sql = "INSERT INTO customer (receipt, date_entered) VALUES ('".$_POST['receipt']."',NOW())"; //FIX *** 
    $sql = "UPDATE customer SET `receipt` = '".$_POST['receipt']."', `date_entered` = NOW() WHERE `id` = '".$_POST['tid']."'"; //NEW 
    if ($conn->query($sql) === TRUE) { 
    // echo "New record created successfully"; 
    } else { 
     echo "Error: " . $sql . "<br>" . $conn->error; 
     exit(); //NEW 
    } 

    $conn->close(); 
    header("Location: http://{$servername}/project/payf.php"); //FIX ************ 
} else { 
    //echo "Please Enter the Receipt Number"; //FIX ************ this goes into payf.php 
    //header("Location: http://{$servername}/project/payf.php");//FIX ******** this redirects when main table is dispayed 
    } 
echo "</body></html>"; 

?> 
</body></html> 

많은 문제 헤더 전에

  • 에코
  • 없이 $의 CONN 설정
  • 리디렉션 테이블을 닫는 브라켓 누락
  • 을 표시
  • 잘못된 SQL을 재 구문 (따옴표)
  • 삽입 업데이트
  • 가난한 HTML
  • 가난한 구조

이 페이지를 검토하십시오;

코드가 작동

테스트 됨. 행복한 코딩!

+0

ahh는 방금 중간에 //receipt.php라고 지적했습니다. 이제 하나가 아닌 두 개의 파일을 보여주고 있다고 가정합니다. 이것을 두 개의 코드 블록으로 나누어야합니다. 나는 이것을 완전히 놓쳤으며이 기능을 하나의 파일에서 만들었습니다. – 2114L3

+0

같은 질문을 한 것입니다. 그래서 그 밖의 모든 것은 중단 점을 제외하고 약자입니까? @ 2114L3 –

+0

위의 코드는 하나의 파일입니다. receipt.php로 표시됩니다. 이름을 바꾸려면 양식의 이름을 새 이름으로 변경하십시오. 다시 2로 나누려면 $ conn을 다시 선언하고 html을 정리해야합니다. – 2114L3