php - MySQL、PHP - 表单帮助

标签 php mysql forms

问候,

我有以下代码

          <?       
        include("conn.php");
        $sn=$_GET["sn"];
        $sql="select * from kpi where no='$sn'";

        $result=mysql_query($sql,$connection) or die(mysql_error());
        while($row=mysql_fetch_array($result)) {
            $sn=$row['id'];
            $no=$row['no'];
            $pdetails=$row['pdetails'];
            $kpistatus=$row['kpistatus'];
            $status=$row['status'];
            $cols=$row['cols'];
            $rows=$row['rows'];
        }
    ?>

    <form name="form1" method="post" action="formsubmit.php?mode=addtable">
        <table width="100%" border="1" align="center" cellpadding="2" cellspacing="2">
          <tr>
            <td colspan="2"><strong>Add Table</strong></td>
               </td>
          </tr>
          <tr>
            <td>NO</td>
            <td><input name="no" type="text" id="no" value="<? echo $no; ?>"></td>
          </tr>
          <tr>
            <td>PROJECT DETAILS</td>
            <td><textarea name="pdetails" rows="10" cols="100"><? echo $pdetails; ?></textarea></td>
          </tr>
                        <tr>
            <td>KPISTATUS</td>
            <td>
            <?
            echo "<table border=\"1\" align=\"left\">\n";
            $j=0;
            while ($j < $rows) 
            {
            echo "<tr>\n";
            $i=0;
            while ($i < $cols) 
            {
            ?>
            <td><input type="text" name="kpistatus" id="kpistatus"></td>
            <?
            $i++;
            }       
            echo "</tr>\n";
            $j++;
            }
            echo "</table>\n";
            ?>
            </td>
            </tr>
            <tr>
            <td>STATUS</td>
            <td><textarea name="status" rows="10" cols="100"><? echo $status; ?></textarea></td>
          </tr>
          <tr>
            <td colspan="2"><input type="submit" name="Submit" value="ADD TABLE"></td>
          </tr>
        </table>
      </form>
        elseif($mode=="addtable") {
        $no=$_POST["no"];
        $pdetails=$_POST["pdetails"];
        $kpistatus=$_POST["kpistatus"];
        $status=$_POST["status"];
        $sn=$_POST["id"];
        $sql="update kpi set pdetails='$pdetails',kpistatus='$kpistatus',status='$status' where no='$no'";
        //echo $sql;
        $result=mysql_query($sql,$connection) or die(mysql_error());
        //header("location: index.php");
      }
      ?>

表格截图: http://img395.imageshack.us/my.php?image=1226818203913yi6.png

用户可以输入他们需要插入数据的行数和列数。在屏幕截图中,我的行数为 10,而列数为 5。

现在我卡住的部分是,我如何确定所有输入的数据 < input type="text"name="kpistatus"id="kpistatus"> 保存在 kpistatus mysql 表中..

请帮帮我。

谢谢。

最佳答案

如果将方括号放在 input 中name,php会自动在post数组中帮你把它们变成一个数组。然后你可以遍历它并根据需要保存它们。在你的表格中,你会把

<input type="text" name="kpistatus[]" id="kpistatus">

(注意两个括号的添加)。

然后,在您的表单处理代码中,您将拥有 $_POST['kpistatus']作为一个数组。您可以使用 PHP 的 implode函数通过执行类似于 implode(',', $_POST['kpistatus'] 的操作将其转换为逗号分隔列表.

快速说明:

在您的代码中,您需要使用 mysql_real_escape_string在你插入它们之前对你的所有变量。否则,用户可以将 SQL 代码输入到其中一个输入中,并能够为所欲为(这称为 SQL 注入(inject))。

想象一下,如果有人在他们的状态字符串中使用单引号会发生什么。最好的情况是它会导致错误,最坏的情况是它们可能会覆盖或删除您的数据。

抱歉,如果这对您来说很明显,但我只是想确保涵盖它。

关于php - MySQL、PHP - 表单帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/293595/

相关文章:

php - $(Document).ready 不会触发代码,但会在控制台中运行

html - 输入中的文本右对齐

php - 函数 getName 在 symfony 2 形式中做什么

php - 显示端口号的 URL 问题

mysql - 我应该选择 MySQL 还是 MongoDB 来进行此设计

php - 设计实时聊天

javascript - 如何使用javascript提交多个表单中的一个

html - 无法从 Firefox 选择下拉列表中删除轮廓/虚线边框

php - 使用 jquery ajax 发送数组

php - 如何从网络创建不可访问的 xampp 服务器?