可以';t从PHP更新/更改MySQL中的数据


Can't update/change data in MySQL from PHP

我差不多到了,但更新运行不好,尤其是在底部。

<?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']。

我的错误是什么?

您正在混合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) );