-1

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
PRossouw
  • 3
  • 3
  • 1
    **Warning!** You're open to [SQL injection attacks](https://owasp.org/www-community/attacks/SQL_Injection)! Read [how to prevent SQL injection in PHP](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) by using prepared statements with bound parameters instead of injecting variables directly into your queries. It's not just about security. If your data contains, for example, a single quote `'`, your query will break. – M. Eriksson Sep 23 '21 at 06:36
  • 2
    HTML textarea does not have `value` - put the text between the tags `` – Honk der Hase Sep 23 '21 at 06:36
  • What **exactly** is not working with that single value? What have you tried to resolve the problem? – Nico Haase Sep 23 '21 at 06:37
  • Why are you using a `while` loop if there's only one row? – Barmar Sep 23 '21 at 06:37
  • 1
    _Suggestion:_ Use `mysqli_fetch_assoc()` instead of `mysqli_fetch_array()` and you will be able to fetch the values by their column names (`$row['title']` and so on), which tends to be easier to both read and manage. – M. Eriksson Sep 23 '21 at 06:39
  • Use mysqli_fetch_assoc function. It's easier to code as your array keys are the columns names. See docs: https://www.php.net/manual/en/mysqli-result.fetch-assoc.php – Robert Sep 23 '21 at 06:56
  • @MagnusEriksson Thank you - I have updated my code to prevent SQL injection – PRossouw Sep 23 '21 at 07:21
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Sep 30 '21 at 10:22

2 Answers2

1

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>
Harshit Rastogi
  • 1,996
  • 1
  • 10
  • 19
0

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>
David Buck
  • 3,752
  • 35
  • 31
  • 35
Ahsan khan
  • 26
  • 2
  • If you write HTML in a question or answer, and don't format it as code, it renders as HTML and is unreadable. – David Buck Sep 23 '21 at 07:16