Search code examples
phpmysqlformsmultiple-users

Pre enter fields on a form and disable a text field for certain user php and mysql database


I am implementing a minutes form, where minutes of a meeting between a student and supervisor are saved within a database table called 'minute.php'. When a student is adding minutes, they are required to enter some data but some fields need to be disabled for only a supervisor to enter. However when i disable the text box it throws an error that the field cannot be null as the database is expecting something...

Also, i have pre-filled two of the fields (Bnumber and Status), however when the 'add minutes' form appears these fields are not pre-populated, any one have any idea how to go about this?

<?php
include ("dbConnect.php");
include ("studenthead.php");

session_start();
if (!(isset($_SESSION["sess_username"]))) header ("Location: index.php");

$currentUser=$_SESSION["sess_username"];   

if (isset($_POST['addMinute'])) {
$newB_number = $currentUser;
$newDate = $_POST['Date'];
$newTime = $_POST['Time'];
$newDiscussion = $_POST['Discussion'];
$newActions = $_POST['Actions'];
$newDateofnextmeeting = $_POST['Dateofnextmeeting'];
$newStatus = "Submitted";
$newSupervisor_comments = $_POST['Supervisor_comments'];


$dbQuery  = $db->prepare("insert into minute values (null, :newB_number, :newDate, :newTime, :newDiscussion, :newActions, :newDateofnextmeeting, :newStatus, :newSupervisor_comments )");
$dbParams = array('$newB_number'=>$newB_number, 'newDate'=>$newDate, 'newTime'=>$newTime, 'newDiscussion'=>$newDiscussion, 'newActions'=>$newActions, 'newDateofnextmeeting'=>$newDateofnextmeeting, 'newStatus'=>$newStatus, 'newSupervisor_comments'=>$newSupervisor_comments);
$dbQuery->execute($dbParams);

$lastInserted = $db->lastInsertId();
}

?>


Solution

  • You would either need to change your DB schema to allow for null values, or you need to send an empty string.

    You are not showing you code that generates the form, but pre-populateing a value is as simple as:

    <input type="text" name="B_number" value="<?php echo $b_number; ?" />
    

    This is of course assuming you have already determined the value you want to pre-populate into the filed and stored in $b_number variable.