doujing5435 2015-05-26 12:19
浏览 111
已采纳

将$ _GET与$ _POST一起使用

I'm trying to post a new record in a table with <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">. But every row in my table has an unique id, and that id is put next to every name. So I'm trying to get the id with $_GET but it's been unsuccesful so far. Is the method I'm trying wrong or am I doing something else wrong? If anybody can tell me what's going wrong, I'd appreciatie.

PHP that gets placed above <html>

<?php
    if (isset($_POST['saveRecord'])) {
        if (isset($_POST["newRecord"]) && !empty($_POST["newRecord"])) {
            $id = $_GET['record'];
            $klant=$_POST['newRecord'].$id;
            $query = "INSERT INTO table2
                (recordid, recordname)
                VALUES
                (NULL, '$record')";

            mysqli_query($con, $query);
        }
    }
?>

Markup

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <table>
        <?php
            $query = ("select * from table1");
            $result = mysqli_query($con, $query) or die (mysqli_error());
            while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
            $id = $row['rowid'];
        ?>
        <tr>
            <td>
                <input class="newRecord<?php echo $id; ?>" type="text" name="newRecord<?php echo $id; ?>" />
                <a href="?record=<?php echo $id; ?>">
                    <button class="saveRecord<?php echo $id; ?>" name="saveRecord<?php echo $id; ?>">Save</button>
                </a>
            </td>
        </tr>
        <?php } ?>
    </table>
</form>
  • 写回答

3条回答 默认 最新

  • dongzhizhai4070 2015-05-26 12:30
    关注

    Don't bother trying to do both at once (the $_GET variables will only be passed if it is included in the action of the form).

    The script won't pick up the the records from the $_POST as the names of the field have the ID included in them.

    Either create each record as an individual form (move the whole lot inside the WHILE loop), or you could use the ID held within the field name, like this:

    $newdata = array();
    foreach($_POST as $k => $v) {
      if ((substr($k,0,9) == 'newRecord') && (!empty($v)) {
        $id = substr($k,9);
        $klant = $v;
    
        $newdata[$id] = $klant;
      }
    }
    

    Which should extract the ID from the field name and associate it with the data entered to the form.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)