0

Supposed to have fetch data from a database, and display on a table. On every record is a text box where one can insert a receipt number and submit. On submit, the receipt number should be inserted on a database for that record and the page reloads displaying the inserted record instead of a text field. I'm not sure what I;m doing wrong because the page loads to a blank page.

$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');
  }
Bob Mwenda
  • 89
  • 1
  • 9
  • So that it appears on every record fetched from a database – Bob Mwenda Jun 30 '16 at 11:32
  • Ideally it should appear on only records fetched from a database so that on can deal with each individually. – Bob Mwenda Jun 30 '16 at 11:33
  • It sounds like you're not making it into this condition. if ($result->num_rows > 0) { try echoing the result num rows and make sure it meets that criteria – Jeff Jun 30 '16 at 11:33
  • You have errors in your code. To see the error messages, check this answer: http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Gjermund Dahl Jun 30 '16 at 11:33
  • I think instead of insert you need to do an update?isn't it? – Alive to die - Anant Jun 30 '16 at 11:34
  • You have syntax error here, must be `$sql = "INSERT INTO customer (receipt, date_entered) VALUES ('" . $_POST['receipt'] . "','NOW()')";` – mitkosoft Jun 30 '16 at 11:38
  • can not see $conn being setup. this might be generating an error that is causing the page to blank. or you have just omitted it from your code snip. – 2114L3 Jun 30 '16 at 11:41
  • `$sql = "INSERT INTO customer (receipt, date_entered) VALUES ('".$_POST['receipt']."',NOW())";` this is correct query – Alive to die - Anant Jun 30 '16 at 11:41
  • and is the page really blank? or showing `` in the source? which means the first if is failing as $result is empty. – 2114L3 Jun 30 '16 at 11:44
  • `'NOW()'` that's a string literal; `NOW()` is a function. – Funk Forty Niner Jun 30 '16 at 11:45
  • then you're outputting before header `echo "Please Enter the Receipt Number"; header('Location: http://localhost/com/payf.php.php');` - remove the echo. – Funk Forty Niner Jun 30 '16 at 11:46
  • another thing; `
    ` cannot be child of ``.
    – Funk Forty Niner Jun 30 '16 at 11:50
  • Tried updating it according to your directions. I now can load back to the original page but it inserts another at the end instead of updating the current specific row. It creates another row at the end of the database with time created and receipt number. @Fred-ii- – Bob Mwenda Jun 30 '16 at 12:04
  • Tried updating it according to your directions. I now can load back to the original page but it inserts another at the end instead of updating the current specific row. It creates another row at the end of the database with time created and receipt number @mitkosoft – Bob Mwenda Jun 30 '16 at 12:05
  • How do I update that specific column instead of inserting? – Bob Mwenda Jun 30 '16 at 12:06
  • ...the syntax is `" UPDATE table SET col_x = '?' WHERE col_y = 'x' "`. – Funk Forty Niner Jun 30 '16 at 12:08

1 Answers1

0

in your receipt.php file;

<!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>

many issues in this code as per comments

  • echo before header redirect
  • no $conn setup
  • redirect when table displayed
  • missing closing bracket
  • incorrect SQL syntax (quotes)
  • insert instead of update
  • poor HTML
  • poor structure

please review these pages;

this code is functional and tested. happy coding!

2114L3
  • 564
  • 5
  • 8
  • ahh just noted that you //receipt.php in the middle. now assume you are showing two files not one. you should break this into two blocks of code. i totally missed this and made this functional from one file. – 2114L3 Jun 30 '16 at 12:47
  • Was about to ask you the same. So everything else stands save for the break point? @2114L3 – Bob Mwenda Jun 30 '16 at 12:50
  • the above code is one file. displayed for receipt.php. if you want to rename then change the name in the form to the new one. if you want to divide it back into two then you will have to re-declare $conn and clean up the html – 2114L3 Jun 30 '16 at 12:59
  • also noted your/this code does not validate the input. so blank input is an error and this is vulnerable to SQL injection. – 2114L3 Jun 30 '16 at 13:01