-1

请原谅天真和纯真...我不是程序员!我已经为此花费了 4 天中最好的部分,我已准备好参加 PHP 课程或强化治疗。

场景:数据库内置在 mySQL 中。包含除 ID 和年龄之外的所有列 varchar(50) 的表 - 均为 INT。见下文,我只需要复选框链接列/字段中的“是”值。

我想使用具有文本框和复选框的表单插入数据。我认为最好的方法是 php 数组 ...??

形式:

<form action="process.php" method="post" enctype="multipart/form-data">
  <label>Childname
  <input type="text" name="textfield[childname]" />
  </label>
  <p>
    <label>Age
    <input type="text" name="textfield[age]" />
    </label>
  </p>
  <p>
    <label>Parent Name
    <input type="text" name="textfield[parent_name]" />
    </label>
  </p>
  <p>
    <label>Contact Number
    <input type="text" name="textfield[contact_no]" />
    </label>
  </p>
  <p>Subjects<br />
    <label>
    <input type="checkbox" name="checkbox[scratch]" value="checkbox" />
    Scratch</label> 
    <label>
    <input type="checkbox" name="checkbox[app_inventor]" value="checkbox" />
    App Inventor</label>
    <label>
    <input type="checkbox" name="checkbox[html]" value="checkbox" />
    HTML</label>
  </p>
  <p>Sessions Attended<br />
    <label>
    <input type="checkbox" name="checkbox[nov12]" value="checkbox" />
    Nov 2012</label>
  </p>
  <p>
    <label>
    <input type="checkbox" name="checkbox[dec12]" value="checkbox" />
    Dec 2012</label>
  </p>
  <p>&nbsp;</p>
  <p>
    <label>
    <input type="submit" name="Submit" value="Submit" />
    </label>
  </p>
</form>

PHP 脚本:

<?php

include("config.php");

$childrecord = array("childname","age","parent_name","contact_no","scratch","app_inventor","html");

if(isset($_POST['childrecord'])){
    $childrecord = $_POST['childrecord'];
    $i = 0;
    foreach ($childrecord as $key => $value); {
    $i++;

    $sql="INSERT INTO tblchildren (childrecord) VALUES ($_POST['childrecord'])";
mysql_query($sql);

}

?>

请帮忙!提前致谢....

4

1 回答 1

1

您想将表单数据存储在代码中。

为此,您必须在代码和数据库中进行以下更改。

通知

note: Input field value should be relevant.

With your existing html code your process.php will get data in this structure


        Array
        (
            [textfield] => Array
                (
                    [childname] => dang
                    [age] => 18
                    [parent_name] => doctor
                    [contact_no] => 100
                )

            [checkbox] => Array
                (
                    [scratch] => checkbox
                    [app_inventor] => checkbox
                    [html] => checkbox
                    [nov12] => checkbox
                    [dec12] => checkbox
                )

            [Submit] => Submit
        )

所以你需要修改你的 process.php

在此之前,您必须修改表的结构,请按照下列步骤操作

1. Delete your existing table
2. Create new table




 DROP TABLE tblchildren ;


         CREATE TABLE tblchildren
         (
           id INT AUTO_INCREMENT PRIMARY KEY,
           childname VARCHAR(30),
           age TINYINT,
           parent_name VARCHAR(30),
          contact_no VARCHAR(20),
           scratch ENUM('yes','no'),
           app_inventor ENUM('yes','no'),
           html ENUM('yes','no'),
           sesNov12Attnd ENUM('yes','no'),
           sesDec12Attnd ENUM('yes','no')
         );

由于您是 php 新手,所以我使用了基本功能,我建议您使用 switch from mysql to mysqli

进程.php

        <?php

            $con=mysql_connect("hostname","username","pass");
            $db=mysql_select_db('dbname', $con);

            //check form is submitted    
            if(isset($_POST)){

            //mysql_real_escape_string() prevents from sql injection.
            $childname= mysql_real_escape_string($_POST['textfield']['childname']);
            $age=mysql_real_escape_string($_POST['textfield']['age']);
            $parentName=mysql_real_escape_string($_POST['textfield']['parent_name']);
            $contactNo=mysql_real_escape_string($_POST['textfield']['contact_no']);


            $scratch=isset($_POST['checkbox']['scratch'])?'yes':'no';
            $appInventor=isset($_POST['checkbox']['app_inventor'])?'yes':'no';
            $html=isset($_POST['checkbox']['html'])?'yes':'no';
            $nov12=isset($_POST['checkbox']['nov12'])?'yes':'no';
            $dec12=isset($_POST['checkbox']['dec12'])?'yes':'no';

            $sql="INSERT INTO tblchildren(childname, age, parent_name, contact_no, scratch,app_inventor,html, sesNov12Attnd, sesDec12Attnd )
            VALUES
            ('$childname',$age,'$parentName','$contactNo','$scratch','$appInventor','$html','$nov12','$dec12')";

            mysql_query($sql);
            }else{
                echo "Form Not SUbmitted";
             }
        ?>
于 2013-07-04T14:58:23.390 回答