无法从PHP更新/更改MySQL中的数据

时间:2015-11-28 17:41:16

标签: php mysql

我几乎就在那里,但更新效果不佳,特别是在底部。

<?php

require('dbconnect.php');//Connects to the database
session_start();
$user_check=$_SESSION['login_user'];
$ses_sql=mysqli_query($link,"SELECT username FROM members WHERE         username='$user_check'");
$row=mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
$loggedin_session=$row['username'];
if(!isset($loggedin_session))
{
    header("Location: login.php");
}//To ensure that you must be logged in to access this page

?>
<html>
<head>
    <title>Healing Food Form</title>
    <meta charset="iso-8859-1"> <!--charset specifies characters available-->
    <meta name="author" content="Klarenz Kristoffer M. Qui;ntildeones">
    <meta name="description" content="form to update healing food">
    <meta name="keywords" content="healing food,form">
</head>

<body>
<?php
$id=$_GET['hf_id'];
$query = "SELECT * FROM healingfood WHERE hf_id='$id'";

if(!mysqli_query($link,$query))
{
die("Sorry. There's a problem with the query.");
}

//stores the result of the query
$result = mysqli_query($link,$query);

while($record = mysqli_fetch_assoc($result))
{
    $hf_id=$record['hf_id'];
    $hf_title=$record['hf_title'];
    $a_id=$record['a_id'];
    $hf_image=$record['hf_image'];
    $hf_description=$record['hf_description'];
    $hf_benefits=$record['hf_benefits'];
    $hf_source=$record['hf_source'];

?>
<form action="updatehealingfood.php?hf_id=<?php echo $record['hf_id']; ?>" method="POST">
    <table id="container" align="center">
        <caption>Update healing food</caption>
    <tr>
        <td>Title:</td>
        <td><input name="hf_title" type="text" value="<?php echo $hf_title; ?>"><br></td>
    </tr>
    <tr>
        <td>Author ID:</td>
        <td><input name="a_id" type="text" value="<?php echo $a_id; ?>"><br></td>
    </tr>
    <tr>
        <td>Image URL:</td>
        <td><input name="hf_image" type="url" value="<?php echo $hf_image; ?>"><br></td>
    </tr>
    <tr>
        <td>Description:</td>
        <td><textarea name ="hf_description" rows="18" cols="60"><?php echo $hf_description; ?></textarea><br></td>
    </tr>
    <tr>
        <td>Benefits:</td>
        <td><input name="hf_benefits" type="text" value="<?php echo $hf_benefits; ?>"><br></td>
    </tr>
    <tr>
        <td>Source:</td>
        <td><input name="hf_source" type="text" value="<?php echo $hf_source; ?>"><br></td>
    </tr>
    <tr>
        <td colspan="2" align="right"><input type="submit" name="update" value="Update Healing Food"></td>
    </tr>
    </table>
</form>

</body>
</html>
<?php
}

$id=$_GET['hf_id'];
if(isset($_POST['update']))
    {
        $hf_title=$_POST['hf_title'];
        $a_id=$_POST['a_id'];
        $hf_image=$_POST['hf_image'];
        $hf_description=$_POST['hf_description'];
        $hf_benefits=$_POST['hf_benefits'];
        $hf_source=$_POST['hf_source'];
        $query2="UPDATE healingfood SET hf_title='$hf_title', a_id='$a_id', hf_image='$hf_image', hf_description='$hf_description', hf_benefits='$hf_benefits', hf_source='$hf_source' WHERE hf_id='$id'";
        $result2=mysql_query($query2) or die();
        echo "Updated";
    }

?>

当我应该更新数据时,数据保持不变。没有人改变。我没有得到$ id = $ _ GET ['hf_id'];

我的错误是什么?

1 个答案:

答案 0 :(得分:1)

您正在混合mysqli_*mysql_*

在第一部分,您使用mysqli_query(),稍后您使用的mysql_query()尚未与数据库建立连接。

坚持mysqli_*

变化:

$result2=mysql_query($query2) or die();

为:

$result2=mysqli_query($link, $query2) or die( "MySQL error: " . mysqli_error($link) );