编辑复选框,无线电和下拉菜单

时间:2012-06-10 15:51:17

标签: php mysql html

我发现这个网站提供了创建,阅读,更新和删除的代码。我对如何添加复选框,单选按钮和下拉列表感到困惑

http://www.killersites.com/community/index.php?/topic/1969-basic-php-system-vieweditdeleteadd-records/

我根本不关心分页 - 我主要关心的是能够输入两个下拉列表,是/否单选按钮,以及使用PHP的3个复选框的集合。

我的尝试没有用,因为当我尝试编辑选项时,值不会停留。包括我的三个文件:宠物,编辑和视图。(我更改了数据库的文件名等)

 <?php
     function renderForm($first, $last, $pets, $size, $type, $years, $error)
     {
     ?>
     <!DOCTYPE html>
     <html>
     <head>
     <title>New Customer</title>
     </head>
     <body>

     <?php
     // display possible errors
     if ($error != '')
     {
     echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
     }
     ?>

     <form action="" method="post">
     <div>
     <strong>First Name:</strong> <input type="text" name="firstname" value="<?php echo  
     $first; ?>" /> 
     <strong>Last Name:</strong> <input align="center" type="text" name="lastname" value="<
    ?php echo $last; ?>" /><br/>

     <p><strong>No. Pets </strong>
     <select name="pets">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option> 
      <option value="4">4</option> 
     </select>
     </p>
     <br/>


     <strong>Size? </strong>
     <br/>
      Big<input type="radio" value="Yes"  name="size" checked><?php echo $size; ?><br />
      Small<input type="radio" value="No" name="size"<?php echo $size; ?><br />
      <br />


<p><strong>Type</strong><br/>
    <input name="type[]" type="checkbox" id="type[]"/>
    Cats
    <input name="type[]" type="checkbox" id="type[]"/>
    Dogs
    <input name="type[]" type="checkbox" id="type[]"/>
    Others
</p>
<br/>
 <strong>Years? </strong>
 <select name="year">
  <option value="Five or Less">Five or More</option>
  <option value="Six or More">Six or More</option>
 </select><br/>

 <input style="color:purple;" type="submit" name="submit" value="Create My Order :-)">
 </div>
 </form>

 <center><a href="view.php">Click Here for Orders</a></center>
 </body>
 </html>

 <?php
 }




 // connect to the database
 include('connect-db.php');

 // check if my form submits and, upon triumph, process the form and save it to the database
 if (isset($_POST['submit']))
 {
 // get form data, check its validity
 $firstname = mysql_real_escape_string(htmlspecialchars($_POST['firstname']));
 $lastname = mysql_real_escape_string(htmlspecialchars($_POST['lastname']));
 $pets = $_POST['pets'];
 $size = mysql_real_escape_string(htmlspecialchars($_POST['size']));
 $type = serialize(mysql_real_escape_string(implode(',', $_POST['type'])));
 $years = mysql_real_escape_string(htmlspecialchars($_POST['years']));

 // check to make sure everything is filled!
 if ($firstname == '' || $lastname == '' || $pets == '' || $size == '' || $type == '' || $years == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 // if either field is blank, show the form again
 renderForm($firstname, $lastname, $pets, $size, $type, $years, $error);
 }
 else
 {
 // save the data to the database
 mysql_query("INSERT customers SET firstname='$firstname', lastname='$lastname', pets='$pets', size='$size', type='$type', years='$years'")
 or die(mysql_error());

 // once saved, redirect to cheview page
 header("Location: view.php");
 }
 }
 else
 // if the form is not submitted, show my form again.
 {
 renderForm('','','','','','','');
 }
?>


    <?php

$types = array("Cats", "Dogs", "Others");

 function renderForm($id, $firstname, $lastname, $pets, $size, $type, $years, $error)
 {
 ?>

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html>
 <head>
 <title>Edit Record</title>
 </head>
 <body>
 <?php
 // if there are any errors, display them
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?>
 <form action="" method="post">
 <input type="hidden" name="id" value="<?php echo $id; ?>"/>
 <div>
 <p><strong>ID:</strong> <?php echo $id; ?></p>
 <strong>First Name: </strong> <input type="text" name="firstname" value="<?php echo $firstname; ?>" /><br/>
 <strong>Last Name: </strong> <input type="text" name="lastname" value="<?php echo $lastname; ?>" /><br/>

 <p><strong>No. Pets </strong>
 <select name="pets">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option> 
  <option value="4">4</option> 
 </select>
 </p>

  <strong>Size? </strong>
 <br/>
  Big<input type="radio" value="Yes"  name="size" checked><?php echo $size; ?><br />
  Small<input type="radio" value="No" name="size"<?php echo $size; ?><br />
  <br />

  <br />
  <br />
<p><strong>Type</strong><br/>
    <input name="type[]" type="checkbox" id="type[]"/>
    Cats
    <input name="type[]" type="checkbox" id="type[]"/>
    Dogs
    <input name="type[]" type="checkbox" id="type[]"/>
    Others
</p>

<br/>
 <strong>Years? </strong>
 <select name="year">
  <option value="Five or Less">Five or More</option>
  <option value="Six or More">Six or More</option>
 </select><br/>
<br /><br />
 <input type="submit" name="submit" value="Resubmit My Order :-)">
 </div>
 </form>
 </body>
 </html>
 <?php
 }


 // connect to the database
 include('connect-db.php');

 // check if the form has been submitted. If it has, process the form and save it to the database
 if (isset($_POST['submit']))
 {
 // check for id being an integer
 if (is_numeric($_POST['id']))
 {
 // get form data, making sure it is valid
 $id = $_POST['id'];
 $firstname = mysql_real_escape_string(htmlspecialchars($_POST['firstname']));
 $lastname = mysql_real_escape_string(htmlspecialchars($_POST['lastname']));
 $pets = $_POST['pets'];
 $size = mysql_real_escape_string(htmlspecialchars($_POST['size']));
 $type = serialize(mysql_real_escape_string(implode(',', $_POST['type'])));
 $years = mysql_real_escape_string(htmlspecialchars($_POST['years']));



 // check that firstname/lastname fields are both filled in
 if ($firstname == '' || $lastname == '' || $pets == '' || $size == '' || $type == '' || $years == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 //error, display form
 renderForm($id, $firstname, $lastname, $pets, $size, $type, $years, $error);
 }
 else
 {
 // save the data to the database
 mysql_query("UPDATE customers SET firstname='$firstname', lastname='$lastname', pets='$pets', size='$size', type='$type', years='$years' WHERE id='$id'")
 or die(mysql_error());

 // once saved, redirect back to the view page
 header("Location: view.php");
 }
 }
 else
 {
 // if the 'id' isn't valid, display an error
 echo 'Error!';
 }
 }
 else
 // if the form hasn't been submitted, get the data from the db and display the form
 {

 // get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
 if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
 {
 // query db
 $id = $_GET['id'];
 $result = mysql_query("SELECT * FROM customers WHERE id='$id' ")
 or die(mysql_error());
 $row = mysql_fetch_array($result);

 // check that the 'id' matches up with a row in the databse
 if($row)
 {

 // get data from db
 $firstname = $row['firstname'];
 $lastname = $row['lastname'];
 $pets = $row['pets'];
 $size = $row['size'];
 $type = serialize($row['type']);
 $years = $row['years'];


 // show form
 renderForm($id, $firstname, $lastname, $pets, $size, $type, $years, '');
 }
 else
 // if no match, display result
 {
 echo "No results!";
 }
 }
 else
 // if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
 {
 echo 'Error!';
 }
 }
?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
        <title>View customer orders</title>
</head>
<body>

<?php

        include('connect-db.php');


        $result = mysql_query("SELECT * FROM customers")
                or die(mysql_error());

        echo "<table border='1' cellpadding='10'>";
        echo "<tr> <th>ID</th> 
                   <th>First Name</th> 
                   <th>Last Name</th> 
                   <th>Pets</th> 
                   <th>Size</th>
                   <th>Type</th> 
                   <th>Years</th> 
                   <th></th> 
                   <th></th>
             </tr>";

        while($row = mysql_fetch_array( $result )) {


                echo "<tr>";
                echo '<td>' . $row['id'] . '</td>';
                echo '<td>' . $row['firstname'] . '</td>';
                echo '<td>' . $row['lastname'] . '</td>';
                echo '<td>' . $row['pets'] . '</td>';
                echo '<td>' . $row['size'] . '</td>';
                echo '<td>' . $row['type']. '</td>';
                echo '<td>' . $row['years'] . '</td>';
                echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
                echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
                echo "</tr>";
        }


        echo "</table>";
?>
<p><a href="pets.php">Add a new record</a></p>

</body>
</html>

我真的在这一点上输了。我已经整整一个月都在研究这个问题而且我不知道我在做什么了。如果有人能帮助我编辑这个工作,我会非常感激。

谢谢大家。

1 个答案:

答案 0 :(得分:0)

是的,要从数据库中获取已保存的结果,我确信您已了解如何执行此操作。

对于您的单选按钮,您将要从数据库中选择“大小”值,并使用if语句确定将“检查”哪个单选按钮

forename / surname只需从数据库中获取forename / surname并将相应的input标记设置为获取的值

您已经发布了一种处理组合/下拉框

的技术

您要对“类型”复选框执行类似的处理,将数据库值与复选框input标记的值进行比较,如果值匹配,请将复选框设置为选中。

下次请更详细地说明您要求帮助的内容,并将您的代码分成与其中包含的文件相对应的细分。

如果你有3个文件,那么每个文件都有一个代码块。