表单不以PHP显示

时间:2014-02-14 22:34:03

标签: php html mysql forms

所以我有一个使用PHP和MySQL查询的页面。我想要做的是基本上创建一个“编辑”页面,该页面从我的数据库中获取数据并使用它来显示各种输入中的值。然后,用户可以更改输入中的数据,然后更新相应的MySQL表行。但是,无论出于何种原因,页面都不显示表单,而是转到else语句。我可以验证$ _SESSION ['weaponName']是否正常工作,因为它会回显正确的事情。关于为什么表格不会出现给我的任何想法?

edit.php

   <?php
session_start();

$con=mysqli_connect("localhost","username","password","db_name");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$weaponName = $_SESSION['weaponName'];

$query = mysqli_query($con, "SELECT * FROM weapons limit 1");

if(mysqli_num_rows($query)>=1){
while($row = mysqli_fetch_array($query)) {
    $creator= $row['creator'];
    $weaponCategory= $row['weaponCategory'];
    $weaponSubCategory= $row['weaponSubCategory'];
    $costAmount= $row['costAmount'];
    $costType= $row['costType'];
    $damageS= $row['damageS'];
    $damageM= $row['damageM'];
    $critical= $row['critical'];
    $rangeIncrement= $row['rangeIncrement'];
    $weight= $row['weight'];
    $weaponType= $row['weaponType'];
    $masterwork= $row['masterwork'];
    $attributes= $row['attributes'];
    $specialAbilities= $row['specialAbilities'];
    $additionalInfo= $row['additionalInfo'];
}

?>

<form action="weaponEditUpdate.php" method="post">
    <input type="hidden" name="weaponName" value="<?php echo $weaponName;?>">

    Weapon Name: <input type="text" name="weaponName" value="<?php echo $weaponName;?>">
    <br>
    Weapon Category: <select name="weaponCategory">
    <?php while ($row = mysqli_fetch_array($query)) {
    echo "<option value='" . $row['weaponCategory'] ."'>" . $row['weaponCategory'] ."</option>";
    } ?>
    </select>
    <input type="Submit" value="Change">
</form>

<?php
}else{
    echo 'No entry found. <a href="javascript:history.back()">Go back</a>';
}
?>

4 个答案:

答案 0 :(得分:1)

更改此

 $query = mysql_query("SELECT * FROM weapons limit 1");

 $query = mysqli_query("SELECT * FROM weapons limit 1");

但是当你通过mysqli连接时,你的所有代码都是mysql !!

答案 1 :(得分:1)

你正在混合功能

mysqli_connect("localhost","username","password","db_name");

无法使用

mysql_query("SELECT * FROM weapons limit 1");

尝试

$query = mysqli_query($con, "SELECT * FROM weapons limit 1");

然后

if($query->num_rows >= 1)

答案 2 :(得分:1)

根据OP的要求(来自评论对话)

而不是

if(mysqli_num_rows($query)>=1){ 

使用

if(mysqli_num_rows($query) >0){

答案 3 :(得分:0)

您与mysqli联系,这很好。那么,您尝试通过mysql运行查询。这是两个单独的扩展。你不能混合它们,因为它们不会彼此“沟通”。坚持使用mysqli。