【问题标题】:Reading TEXT (not VARCHAR) field from MySQL with PHP not displaying in HTML textarea从 MySQL 读取 TEXT(非 VARCHAR)字段,PHP 未显示在 HTML textarea 中
【发布时间】:2021-09-23 06:33:58
【问题描述】:

我正在尝试创建一个编辑页面,其中使用 PHP 从 MySQL 读取数据,然后以 HTML 格式显示。 像 VARCHAR 这样的普通字段可以正确显示,但我在显示 TEXT 字段时遇到了困难。

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

这是我要显示/编辑的数据。

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"

除了 minreq,它是 MySQL 中的一个 TEXT 字段之外,一切都很好。它没有显示出来,但是 echo 显示它是在 $row 数组中检索到的。

【问题讨论】:

  • 警告!您对SQL injection attacks 开放!阅读how to prevent SQL injection in PHP,使用带有绑定参数的预准备语句,而不是将变量直接注入查询中。这不仅仅是安全问题。例如,如果您的数据包含单引号 ',您的查询将中断。
  • HTML textarea 没有value - 将文本放在标签&lt;textarea&gt;&lt;/textarea&gt;之间
  • 什么究竟不能使用那个单一的值?您尝试过什么来解决问题?
  • 如果只有一行,为什么还要使用while 循环?
  • 建议: 使用mysqli_fetch_assoc() 而不是mysqli_fetch_array(),您将能够通过列名($row['title'] 等)获取值,这往往更易于阅读和管理。

标签: php html mysql


【解决方案1】:

尝试在 textarea 标签之间回显值,而不是在 value 属性中:

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

【讨论】:

    【解决方案2】:

    请从文本区域中删除该值并编写如下代码:

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

    【讨论】:

    • 如果您在问题或答案中编写 HTML,并且不将其格式化为代码,它会呈现为 HTML 并且不可读。
    猜你喜欢
    • 1970-01-01
    • 2015-12-09
    • 2010-09-14
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    相关资源
    最近更新 更多