0

This is my php coding. I got this from my template form.

The weirdest thing is, when I try to run this coding from my template I've got the ouput and it retrieved the data, but when I try to running this coding into my project assignment it come out with the blank page.

 <?php
    include"conn.php";
    $conn = connect();
    $db = connectdb();

    $wardID =$_REQUEST["wardID"];
    $RequestName = $_REQUEST["RequestName"];
    $Department =$_REQUEST["Department"];
    $Position = $_REQUEST["Position"];
    $Date = $_REQUEST["Date"];
    $TypeOfRequest = $_REQUEST["TypeOfRequest"];
    $PleaseSpecify = $_REQUEST["PleaseSpecify"];
    $DateRequire = $_REQUEST["DateRequire"];
    $DateReturn = $_REQUEST["DateReturn"];

    mysqli_select_db($conn,"misadmin") or die (mysqli_connect_error()."\n");

    //select from the table student
    $query = "select * from misform" ;
    //to return the query that has been request from the database 
    $result = $conn ->query($query);

    //Fetch a result row as an associative array
    $row=mysqli_fetch_assoc($result);
    //Select a MySQL database
    mysqli_select_db($conn,"misadmin")or die (mysqli_connect_error()."\n");

    $insert ="insert into misform(WardID,RequestName,Department,Postion,Date,TypeOfRequest,PleaseSpecify,DateRequire,DateReturn) values ('$wardID','$RequestName','$Department','$Position','$Date','$TypeOfRequest','$PleaseSpecify','$DateRequire','$DateReturn')";
    $rowinsert =$conn ->multi_query($insert) or die (mysqli_connect_error()."\n");

    header("Location:requestform3.php");
    ?>

here is my connection

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
$host_user="localhost";
$user_user="root";
$password_user="";

function connect (){
    $conn =mysqli_connect("localhost","root","") or die (mysqli_connect_error()."/n");
return $conn;
}

function connectdb(){
    $conndb="misadmin";
    return $conndb;
}
?>
</body>
</html>

here is my html

<body>
<form id="requestform" action="requestform2.php" method="post" >
<div><center><table border = "0" cellspacing="0" cellpadding"4"><tr><td>
<fieldset>
<legend>Request Form</legend>
<div id="errorDiv"></div>

<table><tr>
<td><label for="wardID">Ward ID:*</label></td>
<td><input type="text" id="wardID" name="wardID">
<span class="errorFeedback errorSpan" id="WardIDError"> Ward ID is required</span></td>
</tr>

<tr>
<td><label for="RequestName">Request Name:</label></td>
<td><input type="text" id="RequestName" name="RequestName"></td>
</tr>

<tr>
<td><label for="Department">Department:</label></td>
<td><input type="text" id="Department" name="Department"></td>
</tr>

<tr>
<td><label for="Position">Position:</label></td>
<td><input type="text"id="Position" name="Position"></td>
</tr>

<tr>
<td><label for="Date">Date:</label></td>
<td><input type="date" id="Date" name="Date"></td>
</tr>

<tr>
<td><label for="request">Type of request:* </label></td>
<td><select name="TypeOfRequest" id="request">
<option></option>
<option>Hardware</option>
<option>Software</option>
<option>Network</option>
<option>Others</option></select>
<span class="errorFeedback errorSpan" id="requestError">Please choose one</span>
</td></tr>

<tr>
<td></td>
<td><textarea name="PleaseSpecify" id="specify"> Please Specify </textarea>
<span class="errorFeedback errorSpan" id="specifyError">you not specify yet!</span>
</td></tr>

<tr>
<td><label for="DateRequire">Date Require:</label></td>
<td><input type="date" id="DateRequire" name="DateRequire">
</td></tr>

<tr>
<td><label for="DateReturn">Date Return:</label></td>
<td><input type="date" id="DateReturn" name="DateReturn">
</td></tr>

OUTPUT

Sai
  • 33
  • 8

1 Answers1

0

1) Postion in the INSERT sql statement seems to be incorrect.

Unknown column 'Postion' in 'field list'

I found this out by using:

$rowinsert = $conn->multi_query($insert) or die($conn->error . "\n");

instead of:

$rowinsert = $conn->multi_query($insert) or die(mysqli_connect_error() . "\n");

2) When the header(Location:...) is to be processed, then the following warning is raised:

( ! ) Warning: Cannot modify header information - headers already sent by (output started at .../conn.php:9) in .../requestform2.php on line 32

The problem is: when the header(Location:...) is processed, the PHP engine discovers that something was already sent to the client, e.g. to the browser, in order to be displayed. And this is not allowed. In your code this situation appears beacuse you have the HTML code inside "conn.php". This is recognized as output to the client and, therefore, not allowed. So, "conn.php" should just be:

<?php
    $host_user="localhost";
    $user_user="root";
    $password_user="";

    function connect (){
        $conn =mysqli_connect("localhost","root","") or die (mysqli_connect_error()."/n");
        return $conn;
    }

    function connectdb(){
        $conndb="misadmin";
        return $conndb;
    }
?>

How I found out? I enabled error reporting and display. So I used this statements:

error_reporting(E_ALL);
ini_set('display_errors', 1);

Put them in a file "errorReporting.php" and include it before "conn.php" when you develop. DON'T include it if your website is in production! If you don't choose to display the errors, you're just receiving a blank page, without knowing, for example, why the redirection to the third page doesn't take place.

Recommendations:

Error + exception handling is one of the most important parts in an app and should be very correctly implemented. Along with the user input's filtering and sanitizing. So:

  • Use prepared statements to avoid MySQL injection!
  • Use exception handling to always catch the exceptions thrown by your app.

See my posted complete example (OOP & procedural) and try to apply the steps I showed:

But I would suggest you to change to PDO instead of MySQLi. For this I posted a corresponding example as well:

Good luck!