1

我想做以下事情。我输入值并单击提交。如果该值超出范围 0 和 299,则将文本 'blabla' 保存到变量 $error 中。如果在 0 到 299 的范围内更新值到 MySQL 表中(行 - SkinID);这是我尝试过但没有奏效的方法。这是我的 HTML 表单:

<form action="skinid.php" method="POST">
                            <!--<fieldset class="textarea-field">
                                <textarea title="Message">Message</textarea>
                            </fieldset>-->
<br>
                            <fieldset>
                                <span class="ico user-ico"></span>
                                <input name="skinid" onkeypress="return isNumberKey(event)" type="text" class="field" value="Enter new Skin ID. (-2000$)" title="Enter new Skin ID. (-2000$)" />
                            </fieldset>
                            <center><input type="submit" class="submit btn blue-btn" value="Update" /></center>
                        </form>
  <?php $_SESSION['skinid'] = $_POST['skinid']; ?>

这是 skinid.php 文件:

<?php
session_start();
$newskinid = $_SESSION['skinid'];
if($newskinid < '0' || $newskinid > '299'){$error = 'The Skin ID must be in the range of 0 and 299.';}
else {
$con=mysqli_connect("localhost","USERNAME","PASSWORD","DB-NAME");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
mysqli_query($con,"UPDATE users SET SkinID=$newskinid
WHERE Username='$_SESSION[user]'");
mysqli_close($con);}
?>
4

2 回答 2

4

试试 skinid.php:

<?php
session_start();
$_SESSION['skinid'] = $_POST['skinid']; 
$newskinid = $_SESSION['skinid'];
if($newskinid < '0' || $newskinid > '299') {
    echo 'The Skin ID must be in the range of 0 and 299.';
}
else {
    echo 'dhgj';
}
?>
<form method="post">

<br>
<fieldset>
    <span class="ico user-ico"></span>
    <input name="skinid" onkeypress="return isNumberKey(event)" type="text" class="field" value="Enter new Skin ID. (-2000$)" title="Enter new Skin ID. (-2000$)" />
</fieldset>
<center><input type="submit" class="submit btn blue-btn" value="Update" /></center>
</form>
于 2013-04-30T13:33:03.063 回答
3

除非第一个代码示例也来自 skinid.php,否则在 HTML 表单中你$_POST['skinid']是空的,因此在 skinid.php 中你$_SESSION['skinid']也是空的。

相反,在 skinid.php 中使用: $newskinid = $_POST['skinid'];

于 2013-04-30T13:23:11.577 回答