If x enter data, else retrieve data. PHP & SQL

2019-09-02 10:21发布

Here is my code, I have been attempting to find a way to do this.

The point of the code is to do this:

Check database, see if field is filled, if not post form. Once form is filled, submit data to database, then reload page to go to step 2.

Else if field is full show data in database, and bypass the first part.

    <?php
  require_once('connectvars.php');
?>

<!DOCTYPE html 

     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PDI Non-Conforming Materials Report</title>
<link rel="stylesheet" type="text/css" href="CSS/view.css" />
</head>
<body>
</body> 
</html>
<?php

echo '<form id="all">';
echo '<fieldset>';
echo '<div id="box4-1">';
// We know both $ncmrsr AND $ncmrsc are blank
$row['ncmrsr'] = trim($row['ncmrsr']);
$row['ncmrsc'] = trim($row['ncmrsc']);
if (empty($row['ncmrsr']) && empty($row['ncmrsc'])) {
     // code to add comment would go here.
        echo '<div id="ncmrsr"><span class="b">NCMR Supplier Response:<br /></span><textarea name="ncmrsr" rows="6" cols="85">"N/A"</textarea></div><br />';
        echo '<br />';
        echo '<div id="ncmrsc"><span class="b">NCMR Supplier Comment:<br /></span><textarea name="ncmrsr" rows="6" cols="85" ></textarea></div><br />';
        }



else {
// echo the two fields
                if (!empty($row['ncmrsr'])) {
                    echo '<div id="ncmrsr"><span class="b">NCMR Supplier Response:<br /></span>' . $row['ncmrsr'] . '</div>';}
                if (!empty($row['ncmrsc'])) {
                    echo '<div id="ncmrsc"><span class="b">NCMR Supplier Comment:<br /></span>' . $row['ncmrsc'] . '</div>';}
                    echo '</div>';
echo '</div>';
echo '</fieldset>';
echo '</form>';

}
?>

1条回答
Emotional °昔
2楼-- · 2019-09-02 10:43

so first of all you end your html tag before you echo any dynamic fields. Second you always generate the form no matter what the data says. Third you echo <tr> and <td> tags without a containing table. Fourth you only end the form and div tags if there is no data in the $row variable. So there is a lot going on thats wrong.

<?php
  require_once('connectvars.php');
?>

<!DOCTYPE html 

     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PDI Non-Conforming Materials Report</title>
<link rel="stylesheet" type="text/css" href="CSS/view.css" />
</head>
<body>
<?php



// We know both $ncmrsr AND $ncmrsc are blank
$row['ncmrsr'] = trim($row['ncmrsr']);
$row['ncmrsc'] = trim($row['ncmrsc']);
if (empty($row['ncmrsr']) && empty($row['ncmrsc'])) {
     // code to add comment would go here.
        echo '<form id="all" method="post" action="pagetosaveformtoDB.php">';
        echo '<fieldset>';
        echo '<div id="box4-1">';
        echo '<div id="ncmrsr"><span class="b">NCMR Supplier Response:<br /></span><textarea name="ncmrsr" rows="6" cols="85">"N/A"</textarea></div><br />';
        echo '<div id="ncmrsc"><span class="b">NCMR Supplier Comment:<br /></span><textarea name="ncmrsr" rows="6" cols="85" ></textarea></div><br />
        <input name="submit" type="submit" value="submit" />';
        echo '</div></fieldset></form>';
        }

else {
// echo the two fields
       echo '<div id="box4-1">';
       if (!empty($row['ncmrsr'])) {
          echo '<div id="ncmrsr"><span class="b">NCMR Supplier Response:<br /></span>' . $row['ncmrsr'] . '</div>';}
       if (!empty($row['ncmrsc'])) {
          echo '<div id="ncmrsc"><span class="b">NCMR Supplier Comment:<br /></span>' . $row['ncmrsc'] . '</div>';}
           echo '</div>';

}
?>
</body> 
</html>

Also I am not sure what connectvars is doing and if thats where the $row variable is coming from so I would def more information if that doesnt fix it.

查看更多
登录 后发表回答