简体   繁体   English

使用 PHP UPDATE 更新 MySQL 记录时出现问题,没有任何更新

[英]Problem updating MySQL record using PHP UPDATE, nothing updates

I'm trying to use a form to update records in my database.我正在尝试使用表单来更新我的数据库中的记录。 I've tried using a prepared statement to UPDATE records.我试过使用准备好的语句来更新记录。 It seems like it works but nothing changes in the database.它似乎有效,但数据库中没有任何变化。 I'm convinced I've missed something.我确信我错过了一些东西。

I have a html table that selects data from a mysql table.我有一个从 mysql 表中选择数据的 html 表。 The results are printed on that table.结果打印在该表上。 显示承诺的 HTML 表格

If you click under the column heading 'name' you can see the link is routed to that item's ID.如果您在列标题“名称”下单击,您可以看到链接被路由到该项目的 ID。

https://example.com/details.php?id=21 https://example.com/details.php?id=21


Then, clicking the link the details page loads just fine, along with pulling dynamic data about that record based on it's ID in the database. 然后,单击详细信息页面加载的链接,同时根据数据库中的 ID 提取有关该记录的动态数据。

详情页面

I am attempting to use the form found here to update the records in the database.我正在尝试使用此处找到的表单来更新数据库中的记录。 I've tried a ton of different methods but I think the closest is the following... everytime I try to save the record, it seems like it works but nothing actually updates in the database.我尝试了很多不同的方法,但我认为最接近的是以下方法......每次我尝试保存记录时,它似乎都有效,但数据库中实际上没有任何更新。 Here is the PHP code to handle updating:这是处理更新的 PHP 代码:

<?php 
include("const.php");
check_loggedin($con);
$msg = '';
$c_id = $_GET['id'];

if (isset($_GET['id'])) {
    $id = mysqli_real_escape_string($con, $_GET['id']);

    $sql = "SELECT * FROM sys_commits WHERE id = '$id' ";
    $result = $con->query($sql);
    $row = mysqli_fetch_array($result);

    if (isset($_GET['post'])) {
        $name = mysqli_real_escape_string($dbc, $_GET['name']);
        $description = mysqli_real_escape_string($dbc, $_GET['description']);
        $assignee = mysqli_real_escape_string($dbc, $_GET['assignee']);
        $length = mysqli_real_escape_string($dbc, $_GET['length']);
        $min_time = mysqli_real_escape_string($dbc, $_GET['min_time']);
    }
} else {
    header('Location: commitments.php');
}

$title = "Commitments | AA Meeting App";
$description = "Commitment edit page for the AA Meeting App";
require("inc/head.inc");
echo "
<body class='loggedin'>";
include("inc/nav.inc");
echo "
<div id='page-title'>
    <div class='container'>
        <div class='eight columns'>
            <h2>" .$row['name']. " Commitment</h2>
        </div>

        <div class='eight columns'>
            <nav id='breadcrumbs'>
                <ul>
                    <li>Welcome back </li>
                    <li><b>" .$_SESSION['name']. "</b></li>
                </ul>
            </nav>
        </div>
    </div>
</div>";

echo "<div class='container'>
        <h2>Details</h2>
        <form action='actions/update-commit.php' id='commit_details' method='post'>
            <label for='name'>Commitment Name</label>
            <input type='text' name='name' value='" .$row['name']. "'>
            <br>
            
            <label for='name'>Commitment Description</label>    
            <input type='text' name='description' value='" .$row['description']. "'>
            <br>
            
            <label for='name'>Commitment Assignee</label>   
            <input type='text' name='assignee' value='" .$row['assignee']. "'>
            <br>
            
            <label for='name'>Commitment Length</label> 
            <input type='number' name='length' value='" .$row['length']. "'>
            <br>

            <label for='name'>Minimum Time Required</label> 
            <input type='number' name='min_time' value='" .$row['min_time']. "'>
            <br>
            
            <input type='submit' name='update' value='Update' id='update' class='button small yellow'>
        </form>";
echo "
    </div>
</body>
</html>";

?>

Here is the details page:这是详细信息页面:

 <?php include("const.php"); check_loggedin($con); $msg = ''; $c_id = $_GET['id']; if (isset($_GET['id'])) { $id = mysqli_real_escape_string($con, $_GET['id']); $sql = "SELECT * FROM sys_commits WHERE id = '$id' "; $result = $con->query($sql); $row = mysqli_fetch_array($result); if (isset($_GET['post'])) { $name = mysqli_real_escape_string($dbc, $_GET['name']); $description = mysqli_real_escape_string($dbc, $_GET['description']); $assignee = mysqli_real_escape_string($dbc, $_GET['assignee']); $length = mysqli_real_escape_string($dbc, $_GET['length']); $min_time = mysqli_real_escape_string($dbc, $_GET['min_time']); } } else { header('Location: commitments.php'); } $title = "Commitments | AA Meeting App"; $description = "Commitment edit page for the AA Meeting App"; require("inc/head.inc"); echo " <body class='loggedin'>"; include("inc/nav.inc"); echo " <div id='page-title'> <div class='container'> <div class='eight columns'> <h2>" .$row['name']. " Commitment</h2> </div> <div class='eight columns'> <nav id='breadcrumbs'> <ul> <li>Welcome back </li> <li><b>" .$_SESSION['name']. "</b></li> </ul> </nav> </div> </div> </div>"; echo "<div class='container'> <h2>Details</h2> <form action='actions/update-commit.php' id='commit_details' method='post'> <label for='name'>Commitment Name</label> <input type='text' name='name' value='" .$row['name']. "'> <br> <label for='name'>Commitment Description</label> <input type='text' name='description' value='" .$row['description']. "'> <br> <label for='name'>Commitment Assignee</label> <input type='text' name='assignee' value='" .$row['assignee']. "'> <br> <label for='name'>Commitment Length</label> <input type='number' name='length' value='" .$row['length']. "'> <br> <label for='name'>Minimum Time Required</label> <input type='number' name='min_time' value='" .$row['min_time']. "'> <br> <input type='submit' name='update' value='Update' id='update' class='button small yellow'> </form>"; echo " </div> </body> </html>"; ?>

Double check how your binding parameters in the prepared statements, eg your first statement on the updating page has WHERE id = ?仔细检查准备好的语句中的绑定参数如何,例如更新页面上的第一个语句具有WHERE id = ? but no following $stmt->bind_param('i', $c_id)但没有关注$stmt->bind_param('i', $c_id)

Your other statements also have a different number of parameters specified in the statement than in the bind_param args, eg in您的其他语句在语句中指定的参数数量也与在bind_param args 中指定的数量不同,例如在

    $stmt = $con->prepare('SELECT * FROM sys_commits WHERE id = ?');
    $stmt->bind_param('ssssss', $c_id, $_POST['name'], $_POST['description'], $_POST['assignee'], $_POST['length'], $_POST['min_time']);

(1 '?' in statement, 6 strings being prepared) (声明中的 1 个 '?',正在准备 6 个字符串)

and

        $stmt = $con->prepare('UPDATE sys_commits SET name = ?, description = ?, assignee = ?, length = ?, min_time = ? WHERE id = ?');
        $stmt->bind_param('ssssssi', $_POST['name'], $_POST['description'], $_POST['assignee'], $_POST['length'], $_POST['min_time'], $c_id);

(6 '?' in statement, 6 strings and 1 integer being prepared) (声明中的 6 个“?”,正在准备 6 个字符串和 1 个整数)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM