变量不将POST方法传递给同一页面

时间:2016-02-17 06:17:35

标签: php html

似乎变量没有使用POST方法传递到同一页面...我是html的初学者,php ... 请帮忙... table-name = member attribute = name

<?php
include 'connection.php';
if(isset($_POST['submit'])) {
    $name = $_POST{'name'};

$query = "INSERT INTO member (`name`) VALUES ('$name')";
mysqli_query($conn, $query);
header('Location: register.php');
}
?>

<form class="form-horizontal" action="register.php" method="post" id="input">
<div class="form-group">
  <label  class="col-sm-2 control-label">Name</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" name="name" value="" />
    </div>
</div><br>
<input type="submit" name="submit" form="input" class="btn btn-primary" value="Register" />
</form>

7 个答案:

答案 0 :(得分:1)

首先尝试改变第三行

$name = $_POST{'name'};

$name = $_POST['name'];

因为在帖子中我们需要使用方括号...... 如果你发布到同一页面然后空行动属性值,bcz空行动属性意味着表格将发布到同一页面... 所以表格元素如下......

<form class="form-horizontal" action="" method="post" id="input">

让我知道它是否有帮助......

答案 1 :(得分:1)

<?php
/* $_POST is super global array you can check weather you are getting data from post or not by using below line of code  and use square brakets for array
instead of { } */

echo '<pre>';
print_r($_POST);
echo '</pre>';
include 'connection.php';
if(isset($_POST['submit'])) {
    $name = $_POST['name'];

$query = "INSERT INTO member (`name`) VALUES ('$name')";
mysqli_query($conn, $query);
header('Location: register.php');
}
?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form action="register.php" class="form-horizontal" id="input" method=
    "post" name="input">
        <div class="form-group">
            <label class="col-sm-2 control-label">Name</label>
            <div class="col-sm-10">
                <input class="form-control" name="name" type="text" value="">
            </div>
        </div><br>
        <input class="btn btn-primary" form="input" name="submit" type="submit"
        value="Register">
    </form>
</body>
</html>

答案 2 :(得分:0)

更改

if(isset($_POST['submit'])) 

if(isset($_POST['name'])) 

答案 3 :(得分:0)

请试试这个:

$name = $_POST{'name'};$name = $_POST['name'];

删除表单操作:action="register.php"action=""

答案 4 :(得分:0)

使用此代码代替您的代码..

  
<form class="form-horizontal" action="" method="post" id="input">
    <div class="form-group">
      <label  class="col-sm-2 control-label">Name</label>
        <div class="col-sm-10">
          <input type="text" class="form-control" name="name" value="" />
        </div>
    </div><br>
    <input type="submit" name="submit" class="btn btn-primary" value="Register" />
    </form>

将此代码替换为您现有的代码

答案 5 :(得分:0)

如果您的HTML代码和PHP代码都在同一页面上,则设置为action=''

<form class="form-horizontal" action="" method="post" id="input">

始终在查询代码中检查 mysqli_error() mysqli_connect_errno()

header("Location: YOUR_URL");

之后也使用退出;

您的PHP代码应如下所示: -

include 'connection.php';
if ( isset( $_POST['submit'] ) ) {
  // You have wrongly used bracket. Use [] bracket instead of {}
  $name = $_POST['name'];

  /* check connection */
  if ( mysqli_connect_errno() ) {
    printf( "Connect failed: %s\n", mysqli_connect_error() );
    exit();
  }

  $query = "INSERT INTO member(`name`) VALUES('$name')";
  // Check your query successfully executed or not
  $result = mysqli_query( $conn, $query ) or die( mysqli_error( $conn ) );

  header( 'Location: register.php' ); /* Redirect browser */
  exit;  // Write exit after header
  /* Make sure that code below does not get executed when redirect. */
}

希望它会对你有所帮助:)。

答案 6 :(得分:0)

<?php
include 'connection.php';
if(isset($_POST['name'])) {
    $name = $_POST['name'];

$query = "INSERT INTO member(name) VALUES ('".$name."')";
mysqli_query($conn, $query);
}
?>

<form class="form-horizontal" action="register.php" method="post" id="input">
<div class="form-group">
  <label  class="col-sm-2 control-label">Name</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" name="name" value="" />
    </div>
</div><br>
<input type="submit" name="submit" form="input" class="btn btn-primary" value="Register" />
</form>

另外, 请确认$ conn不是假的