0

目前我有一个使用字段和复选框的表单。字段和复选框都完美地更新了数据库:

<?php
 function renderForm($articletitle, $articleorganization, $articledate, $articleurl, $articletags )
 {
 ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="style.css">
  </head>
  <body>
    <div id="stylized" class="myform">
      <form id="form" name="form" action="" method="post">

. . . . . .

 if(count($articletags) > 0)
{
 $articletags_string = implode(",", $articletags);
}

 if (isset($_POST['submit']))
 { 
 $articletitle = mysql_real_escape_string(htmlspecialchars($_POST['articletitle']));
 $articleorganization = mysql_real_escape_string(htmlspecialchars($_POST['articleorganization']));
 $articledate = mysql_real_escape_string(htmlspecialchars($_POST['articledate']));
 $articleurl = mysql_real_escape_string(htmlspecialchars($_POST['articleurl']));
 $articletags = implode(',', $_POST['articletags']);

. . .

mysql_query("INSERT articles SET articletitle='$articletitle',   articleorganization='$articleorganization', articledate='$articledate',   articleurl='$articleurl', articletags='$articletags' ")
 or die(mysql_error()); 

 // once saved, redirect to success page
 header("Location:addsuccess.html"); 
 }
 }
 else

{
 renderForm('','','','');
 }
?>

不过,现在我想知道是否应该使用布尔复选框来代替。

原因是我也构建了一个编辑表单,它完全遵循新的输入表单,除了值已经通过 MySQL DB 填写。

所以,我假设使用布尔值会容易得多,对吧?

因此,我应该为复选框指定不同的名称,而不是使用数组,并且在 edit.php 页面上我可以使用如下内容:

<?php
 function renderForm($id, $articletitle, $articleorganization, $articledate, $articleurl, $articletags)
 {
 ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="style.css">
  </head>
  <body>
    <div id="stylized" class="myform">
      <form id="form" name="form" action="" method="post">
      <input type="hidden" name="id" value="<?php echo $id; ?>"/>
        <h1>Edit Details for &nbsp; &nbsp;<?php echo $articletitle; ?></h1>
        <fieldset>
          <legend>Article details</legend>
          <div class="row">
            <div class="field"><label>Article Title</label><input type="text" name="articletitle" value="<?php echo $articletitle; ?>"/></div>
          </div>
          <div class="row">
            <div class="field"><label>Article Author </label><input type="text" name="articleorganization" value="<?php echo $articleorganization; ?>"/></div>
            <div class="field"><label>Article Date </label><input type="text" name="articledate" value="<?php echo $articledate; ?>"/></div>
          </div>
          <div class="row">
            <div class="field"><label>Article Url: </label><input type="text" name="articleurl" value="<?php echo $articleurl; ?>"/></div>
          <div class="row">

          <input type="checkbox" name="articletags1" value="checkbox" id="articletags_0" />
          <input type="checkbox" name="articletags2" value="checkbox 2" id="articletags_1" />

          </div>
        </fieldset>
        <footer><input type="submit" name="submit" value="Submit"></footer></form>
    </div>
  </body>
</html>
<?php
 }

 include('settings.php');

 if (isset($_POST['submit']))
 { 
 if (is_numeric($_POST['id']))
 {
 $id = $_POST['id'];
 $articletitle = mysql_real_escape_string(htmlspecialchars($_POST['articletitle']));
 $articleorganization = mysql_real_escape_string(htmlspecialchars($_POST['articleorganization']));
 $articledate = mysql_real_escape_string(htmlspecialchars($_POST['articledate']));
 $articleurl = mysql_real_escape_string(htmlspecialchars($_POST['articleurl']));
 $articletags = implode(',', $_POST['articletags']);


 if ($articletitle == '' || $articletags == '')
 {
 $error = 'ERROR: Please fill in all required fields!';
 renderForm($id, $articletitle, $articletags);
 }
 else
 {
 mysql_query("UPDATE articles SET articletitle='$articletitle', articleorganization='$articleorganization', articledate='$articledate', articleurl='$articleurl', articletags='$articletags' WHERE id=$id")
 or die(mysql_error()); 

 header("Location: editsuccess.html"); 
 }
 }
 else
 {
 echo 'Error!';
 }
 }
 else
 {
 if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
 {
 $id = $_GET['id'];
 $result = mysql_query("SELECT * FROM articles WHERE id=$id")
 or die(mysql_error()); 
 $row = mysql_fetch_array($result);
 if($row)
 {
 $articletitle = $row['articletitle'];
 $articleorganization = $row['articleorganization'];
 $articledate = $row['articledate'];
 $articleurl = $row['articleurl'];
 $articletags = $row['articletags'];

 renderForm($id, $articletitle, $articleorganization, $articledate, $articleurl, $articletags, '');
 }
   else
 {
   echo "No results!";
 }
 }
   else
 {
   echo 'Error!';
    }
     }
?>

问题在于,我在 edit.php 页面上的复选框仍然没有显示选中状态。

4

1 回答 1

1

您需要以与使用checked="checked"相同的方式使用value. 见解决方案底部:

<input type="checkbox" <?php if(isset($_POST[articletags1])) echo 'checked="checked" ' ?>name="articletags1" value="checkbox" id="articletags_0" />
<input type="checkbox" <?php if(isset($_POST[articletags2])) echo 'checked="checked" ' ?>name="articletags2" value="checkbox 2" id="articletags_1" />

希望这有效。更新了正确的代码。用于ini_set('display_errors', 1);获取生成的错误。

于 2012-06-08T07:34:35.190 回答