如何将发布的变量传递给PHP中的Anchor ID标记

时间:2018-02-01 10:39:39

标签: php mysqli

我一直在尝试将一些数据插入到DB中的表中,但错误来自于我的一个字段是来自锚标记的GET变量。

为了清楚了解,请查看我的代码:

索引页:,帖子值为id = 1

<form name="user_form" method="post" action="input.php">

<?php 
    $sql2 = "SELECT * FROM singleq ";
    $result2 = $conn->query($sql2);
    if ($result2->num_rows > 0) {                               
    while($row2 = $result2->fetch_assoc()) {  
        $id = $row2['id'];
        $cat = $row2['cat'];
?>
<tr >

<input type="hidden" name="q_id" id="q_id" value="<?php echo ($row2["id"]); ?>" />
<input type="hidden" name="tablum" id="tablum" value="singleq" />
<input type="hidden" name="test_id" id="test_id" value="<?php echo $_GET['id'] ?>" />
<td><a href='singleqbanksub.php?id=<?php echo $row2['id'];?>'><?php echo substr(($row2["question"]),0,100) ?>...</a></td>

<td><button class="button button1" type="submit" name="insert-data"  id="insert-data"  style="width:180px;">Add Question</button></td>
</tr> 

<?php     }
     } else {
    echo "";
        }
?>
</form>

input.php:

<?php include('db.php'); ?>
<?php
$q_id = $_POST['q_id'];
$test_id = $_POST['test_id'];
$tablum = $_POST['tablum'];

$date = date_default_timezone_set('America/New_York');
$date = date('M-d,Y H:i:s');
$date2 = date('M-d,Y');

$conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO english (cat, test_id, q_id, tablum)
VALUES ('$cat', '$test_id', '$q_id', '$tablum')";
if ($conn->query($sql) === TRUE) {

header('Location: index.php?id='echo "$test_id";'');
}
else {
    echo "ERROR" . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

在这里,当我尝试将id通过锚标记重定向传递到同一页面以循环该过程时,它显示有关未识别回声的错误。

1 个答案:

答案 0 :(得分:0)

你在头文件函数中使用echo是错误的,使用如下:

header('Location: index.php?id='.$test_id);