通过HTML更新PHP变量

时间:2015-07-17 06:36:58

标签: php html mysql

我希望能够使用HTML表单更改下面代码中的PHP变量$dispname$banstat

<?php
if (isset($_GET['p']) && $_GET['p'] == "login") {
$servername = "localhost";
$username = "foo";
$password = "bar";
$dbname = "wordpress";
$banstat = '1';
$dispname = "Brendan";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

$sql = "UPDATE wp_oxygenpurchaseusers SET user_url=$banstat WHERE    display_name=$dispname";

if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}

$conn->close();
}
?>

</script>
<link href="http://jotform.us/static/formCss.css?3.3.8019" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="http://jotform.us/css/styles/nova.css?3.3.8019" />
<link type="text/css" media="print" rel="stylesheet" href="http://jotform.us/css/printForm.css?3.3.8019" />
<style type="text/css">
.form-label-left{
    width:150px !important;
}
.form-line{
    padding-top:12px;
    padding-bottom:12px;
}
.form-label-right{
    width:150px !important;
}
.form-all{
    width:650px;
    color:#555 !important;
    font-family:"Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Verdana, sans-serif;
    font-size:14px;
}
</style>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>?p=login" method="post">
<input type="hidden" name="formID" value="51970718174158" />
<div class="form-all">
<ul class="form-section page-section">
  <li id="cid_4" class="form-input-wide" data-type="control_head">
    <div class="form-header-group">
      <div class="header-text httal htvam">
        <h2 id="header_4" class="form-header">
          Ban Tool
        </h2>
      </div>
    </div>
  </li>
<form action="<?php echo $_SERVER['PHP_SELF'];  ?> method="post">
Name: <input type="text" name="dispname"><br>
E-mail: <input type="text" name="banstat"><br>
<input type="submit">
</form>
  <li style="display:none">
    Should be Empty:
    <input type="text" name="website" value="" />
  </li>
</ul>
</div>
<input type="hidden" id="simple_spc" name="simple_spc" value="51970718174158" />
<script type="text/javascript">
document.getElementById("si" + "mple" + "_spc").value = "51970718174158-51970718174158";

3 个答案:

答案 0 :(得分:0)

你需要获得$ _POST并设置变量

$dispname = $_POST['name'];

代码中的错误:

您打开两个表单,然后关闭一个

如果您想使用帖子,请检查 $ _ POST

<?php
if (isset($_GET['p']) && $_GET['p'] == "login") {
   if ($_POST) {
      //update variables and update bd
   }
}
?>

答案 1 :(得分:0)

您的代码中存在多个问题。

尝试

<?php
if (isset($_GET['p']) && $_GET['p'] == "login")
{
    /* Database Connection Info */
    $servername = "localhost";
    $username = "foo";
    $password = "bar";
    $dbname = "wordpress";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error)
    {
        die("Connection failed: " . $conn->connect_error);
    } 

    /* getting form values */
    $banstat = mysqli_real_escape_string($conn, $_GET['banstat']);//'1';
    $dispname = mysqli_real_escape_string($conn, $_GET['dispname']);

    $sql = "UPDATE wp_oxygenpurchaseusers SET user_url='$banstat' WHERE display_name='$dispname'";

    if ($conn->query($sql) === TRUE)
    {
        echo "Record updated successfully";
    }
    else
    {
        echo "Error updating record: " . $conn->error;
    }

    $conn->close();
}
?>

</script>
<link href="http://jotform.us/static/formCss.css?3.3.8019" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="http://jotform.us/css/styles/nova.css?3.3.8019" />
<link type="text/css" media="print" rel="stylesheet" href="http://jotform.us/css/printForm.css?3.3.8019" />
<style type="text/css">
.form-label-left{
    width:150px !important;
}
.form-line{
    padding-top:12px;
    padding-bottom:12px;
}
.form-label-right{
    width:150px !important;
}
.form-all{
    width:650px;
    color:#555 !important;
    font-family:"Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Verdana, sans-serif;
    font-size:14px;
}
</style>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>?p=login" method="get">
<input type="hidden" name="formID" value="51970718174158" />
<div class="form-all">
<ul class="form-section page-section">
  <li id="cid_4" class="form-input-wide" data-type="control_head">
    <div class="form-header-group">
      <div class="header-text httal htvam">
        <h2 id="header_4" class="form-header">
          Ban Tool
        </h2>
      </div>
    </div>
  </li>

Name: <input type="text" name="dispname"><br>
E-mail: <input type="text" name="banstat"><br>

  <li style="display:none">
    Should be Empty:
    <input type="text" name="website" value="" />
  </li>

<input type="submit">
</form>
</ul>
</div>

代码中的错误

  • 您打开两个表单。
  • 您在表单中使用post method,但在php代码中使用get method
  • 您的变量未更新。

答案 2 :(得分:0)

好的,所以我接受了@javi和@Hassan提供的其他答案并用它们重新编写它,我从Hassan获取了大部分代码然后用Javi改变了一些东西并将它们组合在一起。我想也许其他人可以使用它。再次感谢你们。

<?php
if (isset($_GET['p']) && $_GET['p'] == "login") {
if ($_POST) {
$servername = "localhost";
$username = "foo";
$password = "bar";
$dbname = "wordpress";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
$banstat = $_POST['banstat'];
$dispname = $_POST['dispname'];
$sql = "UPDATE wp_oxygenpurchaseusers SET user_url=$banstat WHERE user_login='$dispname'";

if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}

$conn->close();
}
}
?>

</script>
<link href="http://jotform.us/static/formCss.css?3.3.8019" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="http://jotform.us/css/styles/nova.css?3.3.8019" />
<link type="text/css" media="print" rel="stylesheet" href="http://jotform.us/css/printForm.css?3.3.8019" />
<style type="text/css">
.form-label-left{
    width:150px !important;
}
.form-line{
    padding-top:12px;
    padding-bottom:12px;
}
.form-label-right{
    width:150px !important;
}
.form-all{
    width:650px;
    color:#555 !important;
    font-family:"Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Verdana, sans-serif;
    font-size:14px;
  }
</style>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?p=login" method="post">
Display Name: <input type="text" name="dispname"><br>
Ban Status: <input type="text" name="banstat"><br>
<input type="submit">
</form>
  <li style="display:none">
    Should be Empty:
    <input type="text" name="website" value="" />
  </li>
</ul>