dongre6404 2014-11-19 15:35
浏览 133
已采纳

从具有相同名称的文本字段提交多个值

i have a form, the carries two textfields with the name attributes "name" and "amount"

<form method="post" name="form1" id="form1" action="test.php">
<input type="text" name="name" value="wole" size="32" required="required" />
  <input type="text" name="amount" value="100" size="32" required="required" />
  <input type="text" name="name" value="yetunde" size="32" required="required" />
  <input type="text" name="amount" value="200" size="32" required="required" />
</form>

my intention is to capture name and amount side by side in two rows, based on how my form is structured.

i have tried submittin the data into the database using this snippet

$sql="INSERT INTO records(name,  amount)VALUES('$_POST[name]', '$_POST[amount]')";

but it only submits one row of data, "wole and 100", how can i make sure i capture the two different rows from my form

  • 写回答

4条回答 默认 最新

  • dtqpw68806 2014-11-19 15:38
    关注

    As requested, here is the full code. I added a new field "score" to show you how to add new fields.

    <form method="post" name="form1" id="form1" action="test.php">
      <input type="text" name="studentname[]" value="wole" size="32" required="required" />
      <input type="text" name="course[]" value="100" size="32" required="required" />
      <input type="text" name="score[]" value="100" size="32" required="required" />
      <input type="text" name="studentname[]" value="yetunde" size="32" required="required" />
      <input type="text" name="course[]" value="200" size="32" required="required" />
      <input type="text" name="score[]" value="100" size="32" required="required" />
    </form>
    
    
    <?php
    $con=mysqli_connect
    ("localhost","root","famakin","results");
    //Checkconnection
    if(mysqli_connect_errno())
    {
        echo"FailedtoconnecttoMySQL:".mysqli_connect_error();
    }
    foreach($_POST['studentname'] as $idx => $studentname) {
        $sql="INSERT INTO records(studentname,  course, score)VALUES('" . $studentname . "', '" . $_POST['course'][$idx] . "', '" . $_POST['score'][$idx] . "')";
        if(!mysqli_query($con,$sql))
        {
            die('Error:'.mysqli_error($con));
        }
    }
    
    
    echo '<META HTTP-EQUIV="Refresh" Content="0; URL=result.php">';
    mysqli_close($con);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路