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条)

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法