0

我是 PHP 的初学者,我正在使用我在网上找到的教程来处理我正在构建的新 CMS 的新部分。问题是本教程工作得很好,但是当我开始将其更改为我的使用时,它无法连接到数据库进行更新。我收到错误“数据库错误:无法更新记录”。我可以添加和删除但不能更新。

索引.php

    <html>
    <head>
        <title>MySQLi Tutorial</title>

    </head>
<body>

<?php
//include database connection
include 'db_connect.php';

$action = isset($_GET['action']) ? $_GET['action'] : "";

if($action=='delete'){ //if the user clicked ok, run our delete query

    $query = "DELETE FROM family WHERE fid = ".$mysqli->real_escape_string($_GET['fid'])."";
    if( $mysqli->query($query) ){
        echo "User was deleted.";
    }else{
        echo "Database Error: Unable to delete record.";
    }

}

$query = "SELECT * FROM family";
$result = $mysqli->query( $query );

$num_results = $result->num_rows;

echo "<div><a href='add.php'>Create New Record</a></div>";

if( $num_results ){

    echo "<table border='1'>";//start table
    //creating our table heading
    echo "<tr>";
        echo "<th>name</th>";
        echo "<th>Action</th>";
    echo "</tr>";

    //loop to show each records
    while( $row = $result->fetch_assoc() ){
            //extract row
            //this will make $row['firstname'] to
            //just $firstname only
            extract($row);

            //creating new table row per record
            echo "<tr>";
                echo "<td>{$name}</td>";
                echo "<td>";
                    echo "<a href='edit.php?fid={$fid}'>Edit</a>";
                    echo " / ";
                    echo "<a href='#' onclick='delete_user( {$fid} );'>Delete</a>";
                echo "</td>";
            echo "</tr>";
    }

    echo "</table>";//end table

}else{
    //if table is empty
    echo "No records found.";
}

//disconnect from database
$result->free();
$mysqli->close();

?>

<script type='text/javascript'>

    function delete_user( fid ){
        //this script helps us to

        var answer = confirm('Are you sure?');
        if ( answer ){ //if user clicked ok
            //redirect to url with action as delete and fid to the record to be deleted
            window.location = 'index.php?action=delete&fid=' + fid;
        } 
    }
</script>

</body>
</html>

添加.php

<html>
    <head>
        <title>MySQLi Create Record</title>

    </head>
<body>
<!--we have our html form here where user information will be entered-->
<form action='#' method='post' border='0'>
    <table>
        <tr>
            <td>Firstname</td>
            <td><input type='text' name='name' /></td>
        </tr>

            <td></td>
            <td>
                <input type='hidden' name='action' value='create' />
                <input type='submit' value='Save' />

                <a href='index.php'>Back to index</a>
            </td>
        </tr>
    </table>
</form>
<?php
$action = isset($_POST['action']) ? $_POST['action'] : "";

if($action=='create'){
    //include database connection
    include 'db_connect.php';

    //write query
    $query = "insert into family 
                set
                name = '" . $mysqli->real_escape_string($_POST['name']) ."'
                    ";

    if( $mysqli->query($query) ) {
        echo "User was created.";
    }else{
        echo "Database Error: Unable to create record.";
    }
    $mysqli->close();
}

?>



</body>
</html>

编辑.php

<?php
//include database connection
include 'db_connect.php';

$action = isset( $_POST['action'] ) ? $_POST['action'] : "";
if($action == "update"){
    //write query
    $query = "update users 
                set
                    name = '".$mysqli->real_escape_string($_POST['name'])."', 
                    where 
                    fid='".$mysqli->real_escape_string($_REQUEST['fid'])."'";

    if( $mysqli->query($query) ) {
        echo "User was updated.";
    }else{
        echo "Database Error: Unable to update record.";
    }
}

$query = "select fid, name 
            from family
            where fid='".$mysqli->real_escape_string($_REQUEST['fid'])."'
            limit 0,1";

$result = $mysqli->query( $query );
$row = $result->fetch_assoc();

$fid = $row['fid'];
$name = $row['name'];
?>
<!--we have our html form here where new user information will be entered-->
<form action='#' method='post' border='0'>
    <table>
        <tr>
            <td>Firstname</td>
            <td><input type='text' name='name' value='<?php echo $name;  ?>' /></td>
        </tr>

        <tr>
            <td></td>
            <td>
                <!-- so that we could identify what record is to be updated -->
                <input type='hidden' name='fid' value='<?php echo $fid ?>' /> 

                <!-- we will set the action to edit -->
                <input type='hidden' name='action' value='update' />
                <input type='submit' value='Edit' />

                <a href='index.php'>Back to index</a>
            </td>
        </tr>
    </table>
</form>

>

我一直无法找到合适的语言来解决这个问题,但我相信这是因为我正在使用某种形式的倍数,但我不熟悉 mysqli/php 语言来修复它。

谢谢

4

1 回答 1

0

您需要删除末尾的逗号:

name = '".$mysqli->real_escape_string($_POST['name'])."',

查询应该是:

$query = "update users 
            set
                name = '".$mysqli->real_escape_string($_POST['name'])."'
                where 
                fid='".$mysqli->real_escape_string($_REQUEST['fid'])."'";
于 2013-05-10T03:02:10.987 回答