【问题标题】:How to post array data from a form to mysql DB如何将数组数据从表单发布到 mysql DB
【发布时间】:2013-07-05 00:58:29
【问题描述】:

请原谅我的幼稚和天真...我不是程序员!我已经在这 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);

}

?>

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

【问题讨论】:

  • 你的问题/你的问题是什么?
  • 显然 PHP 不工作。我只学到了一点点,不知道这个论坛是由程序员(@Rajeev)“拥有”的。
  • 我尝试将 isset 用于复选框,但我在表单中未选中的那些上得到未定义的索引,所以我认为数组,但我认为数组语法全错了....
  • @user2550346 此站点不归程序员所有,但由程序员主持。

标签: php mysql arrays post checkbox


【解决方案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

process.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";
             }
        ?>

【讨论】:

  • 非常感谢 - 到目前为止,在提交表单时收到此错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 2 行的 ',,no,no,yes,no,no)' 附近使用正确的语法。我正在使用 MySQL 版本 5.5.27 运行 XAMPP ...
  • 您需要将查询修改为:- $sql="INSERT INTO tblchildren(childname, age, parent_name, contact_no, scratch,app_inventor,html, sesNov12Attnd, sesDec12Attnd ) VALUES ('$childname',$年龄,'$parentName','$contactNo','$scratch','$appInventor','$html','$nov12','$dec12')";
  • 为什么要使用枚举?使用 bit(1) 或 tinyint(1)
  • 如果还有其他问题请告诉我。
  • @mc_fish ,枚举有更好的性能。有关更多详细信息,请查看:- link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-11
  • 2018-06-20
  • 2021-12-01
相关资源
最近更新 更多