警告:mysql_fetch_array()期望参数1是资源,布尔值

时间:2014-01-28 10:02:38

标签: php mysql

目前我的php代码存在很大问题。我正在尝试创建一个页面来添加用户(完成),现在问题是我想要一个脚本,让您可以自由地更改用户信息。现在问题:

按“ändraanvändare”。

enter image description here

按“ändra”按钮(问题错误)

enter image description here

Edit.php

    <!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title> Edit - Reset UF </title>
<link rel="shortcut icon" href="images/icon.ico" >
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<link href='http://fonts.googleapis.com/css?family=Days+One' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<script language="JavaScript" src="scripts/gen_validatorv31.js" type="text/javascript"></script>

</head>
<body>

<div id="content">

<div id="header">

<div id="position">
<div id="logo" onclick="location.href='index.html';">
</div>

<div id="meny">
<br>
<a class="menyfix" href="index.html"> Start </a>
<a class="menyfix" href="kontakt.html"> Kontakt </a>
<a class="menyfix" href="oppettider.html"> Öppettider </a>
<a class="menyfix" href="om.html"> Om oss </a>
<a class="menyfix" href="programbanken.html"> Programbanken </a>
<a class="menyfix" href="klara.php"> Färdiga datorer </a>
</div>
</div>
</div>
<div id="midcontent">
</div>
<div class="arrow-up"></div>
</div>

<div id="bottomcontent">
<p class="Article">
LÄGG TILL ELLER TA BORT
</p>
<?php
    include 'connection.php';

    $query = "SELECT * FROM kunder";

    $result = mysql_query($query);

while($kunder = mysql_fetch_array($result)) {
 echo "<p><b>". $kunder['Name'] . "</b>-" . $kunder['Description']. " </p>";
 echo"<a href=\"modify.php?id=" . $kunder['ID'] . "\">Ändra Användare</a> <br />";
 echo"<a href=\"modify.php?id=" . $kunder['ID'] . "\">Radera Användare</a>";

 }

?>
<h1> Lägg till en person</h1>
<form action="create.php" method="post">
Namn: <input type="text" name="inputName" value=""/> <br />
Problem: <input type="text" name="inputDesc" value=""/><br />
<br />
<input type="submit" name="submit"/>


</form>

</div>
</div>


</body>
</html>

_________________


*Modify.php

<?php
    include 'connection.php';

    if(isset($_GET['id'])){
    $id = $_GET['id'];
    }

    else{

    $id = $_POST['id'];

    }

    $q = "SELECT * FROM kunder WHERE ID = $id";
    $result = mysql_query($q);
    $kunder = mysql_fetch_array($result);

    ?>
    <h1>Du ändrar en användares information</h1>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Namn: <input type="text" name="inputName" value="<?php echo $kunder['Name']; ?>"/> <br />

Problem: <input type="text" name="inputDesc" value="<?php echo $kunder['Description']; ?>"/><br />
<br />
<input type="hidden" name="id" value="<?php $id ; ?>" />
<input type="submit" name="submit" value="Ändra" />



</form>
    <?php

    if(isset($_POST['submit'])){
    $u = "UPDATE kunder SET Name='$_POST[inputName]' , Description='$_POST[inputDesc]' WHERE ID = $id";
    mysql_query($u) or die (mysql_error());

    echo "Användaren har ändrats!";
    header("Location: edit.php");

    }
    else{

    }

    ?>*

2 个答案:

答案 0 :(得分:0)

此错误表示mysql_query返回false,表示查询出错。

要找出错误是什么,您可以使用:

$result = mysql_query($query) or die(mysql_error());

or die部分仅在mysql_query返回false时执行,并为您提供从mysql返回的错误消息。

答案 1 :(得分:0)

尝试更改脚本

从:

$u = "UPDATE kunder SET Name='$_POST[inputName]' , Description='$_POST[inputDesc]' WHERE ID = $id";

为:

$u = "UPDATE kunder SET Name='".$_POST[inputName]."', Description='".$_POST[inputDesc]."' WHERE ID = '".$id."';