Home > Software design >  PHP, MySQL, HTML - Reading TEXT (not VARCHAR) field from MySQL with PHP not displaying in HTML texta
PHP, MySQL, HTML - Reading TEXT (not VARCHAR) field from MySQL with PHP not displaying in HTML texta

Time:09-23

I'm trying to create an edit page where data is read from MySQL with PHP and then displayed in HTML. Normal fields like VARCHAR displays correctly, but I have difficulty in displaying TEXT fields.

<?php
    $DATABASE_HOST = '';
    $DATABASE_USER = '';
    $DATABASE_PASS = '';
    $DATABASE_NAME = '';
   
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $mysqli = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
    if ( mysqli_connect_errno() ) {
        exit('Failed to connect to MySQL: ' . mysqli_connect_error());
    }
       
    $idjob = $_POST['jobID']; 
    $jobquery = $mysqli->prepare('SELECT * FROM jobs WHERE id = ?');
    $jobquery->bind_param('s', $idjob); // 's' specifies the variable type => 'string'

    $jobquery->execute();

    $jobresult = $jobquery->get_result();
    while ($row = $jobresult->fetch_assoc()) {
        $jobtitle = $row['jobtitle'];
        $workhours = $row['workhours'];
        $type = $row['type']; 
        $availfrom = $row['availfrom'];
        $deadline = $row['deadline'];              
        $customer = $row['customer'];
        $province = $row['province'];
        $town = $row['town'];        
        $minreq = $row['minreq'];
        $posdetails = $row['posdetails'];
        $minsal = $row['minsal'];
        $maxsal = $row['maxsal'];
    }
    mysqli_free_result($jobresult);
    mysqli_close($mysqli);

?>
<!DOCTYPE html>

<html lang="en">
    <head>
        <title>Edit Job Listing</title>
        <link rel="shortcut icon" type="image/jpg" href="images/Logo.jpg">
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0 shrink-to-fit=no">        
        <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">             
        <link rel="stylesheet" href="../assets/css/style.css" type="text/css"/>
    </head>
    <body id="editjoblisting">
        <!-- Container (About Add New Job Listing Section) -->
        <div id="abouteditnewjob" class="container-fluid bg-grey">
            <div class="row">
                <div class="col-sm-2"></div>
                <div class="col-sm-8">
                    <div class="text-center">
                        <h2> Edit Job Listing </h2>
                    </div>
                </div>
                <div class="col-sm-2"></div>
            </div>
        </div>
        <div class="container-fluid bg-grey">
            <form method="post" id="updedit_form" action="../php/updeditjob.php">
                <div class="form-row">
                    <div class="col-sm-2"></div>
                    <div class="col-sm-4">
                        <div class="form-group">                               
                            <label for="jobtitle"> Job Title</label>
                            <input type="text" class="form-control"  name="jobtitle" value='<?php echo $jobtitle; ?>'>                                              

                            <label for="workhours"> Working Hours</label>                            
                            <input type="text" class="form-control"  name="workhours" value='<?php echo $workhours; ?>'>                            

                            <label for="worktype">Type</label>
                            <select class="form-control"  name="worktype" >
                                <option value='<?php echo $type; ?>' selected><?php echo $type; ?></option>
                                <option>--None--</option>
                                <option>Temporary</option>
                                <option>Permanent</option>                                
                            </select>

                            <label for="availfrom"> Available From</label>                            
                            <input type="date" class="form-control" id="availfrom" name="availfrom" required maxlength="50" value='<?php echo $availfrom; ?>'>                            

                            <label for="deadline"> Application Deadline</label>                            
                            <input type="date" class="form-control" id="deadline" name="deadline" required maxlength="50" value='<?php echo $deadline; ?>'>                            
                        </div>
                    </div>
                    <div class="col-sm-4">
                        <div class="form-group">
                            <label for="minreq">Minimum Requirements</label>
                            <textarea class="form-control" type="textarea" name="minreq" id="minreq" maxlength="6000" rows="5" value='<?php echo $minreq; ?>'></textarea>

                            <label for="posdetails">Position Details</label>
                            <textarea class="form-control" type="textarea" name="posdetails" id="posdetails" maxlength="6000" rows="5" value='<?php echo $posdetails; ?>'></textarea>

                            <label for="minsal"> Minimum Salary</label>                            
                            <input type="text" class="form-control" id="minsal" name="minsal" value='<?php echo $minsal; ?>'>                            

                            <label for="maxsal"> Maximum Salary</label>                            
                            <input type="text" class="form-control" id="maxsal" name="maxsal" value='<?php echo $maxsal; ?>'>                            
                        </div>                        
                    </div>
                    <div class="col-sm-2"></div>                        
                </div>                                                
            </form>
        </div>
    </body>
</html>  

This is the data I'm trying to show/edit.

id "251"    
jobid "ID#20101664"
jobtitle "SENIOR CONVEYANCING SECRETARY"    
workhours "Monday - Friday 7:00 - 17:00"    
type "Permanent"    
availfrom "2021-06-07"  
deadline "2021-10-29"       

minreq  
"- Matric Certificate
- Minimum 3 years experience 
- Knowledge in Law programs (Lexis Nexis, Windeed) – Compulsory
- Knowledge of Bonds: Absa, Standard Bank, FNB and Nedbank
- Bilingual (English & Afrikaans)
- Computer literate
- Be accurate and methodical etc.." \N  
minsal "0"  
maxsal "0"

Everything works nicely except minreq which is a TEXT field in MySQL. It is not displayed, yet echo shows that it was retrieved in the $row array.

CodePudding user response:

Try to echo the value between the textarea tags, not in the value attribute:

<textarea class="form-control" type="textarea" name="minreq" id="minreq" maxlength="6000" rows="5"><?php echo $minreq; ?></textarea>

CodePudding user response:

Please remove the value from inside the text area and write your code like this:

//here is the valid code...this will 100% work.
<textarea class="form-control" type="textarea" name="posdetails" id="posdetails" maxlength="6000" rows="5">
    <?php echo $posdetails; ?>
</textarea>
  • Related