使用 form 和 PHP 更新 SQL.提交时值重置为 0


Updating SQL with form and PHP. Values resetting to 0 on submit?

我正在尝试创建一个简单的表单,该表单根据行的ID更新MYSQL数据库中的行。

我已经设法使表单和更新值正常工作,但是对于我的一个变量,我需要根据另外两个变量的值将其新值添加到其中。(所以像$currPoints = $currPoints+$addPoints-$remPoints;(。

我面临的问题是,每当提交表单时,$currPoints要么重置为 0,然后添加和减去其他值,要么找不到 $cuurPoints 的值,因此无法添加到其原始值。

我不确定在我的代码中具体出错的地方,所以如果可以的话,我会粘贴整个页面!

我的表单函数。这在页面加载时调用:

// creates the form
function renderForm($name = '', $currPoints = '', $addPoints = '', $remPoints = '', $reason = '', $error = '', $id = '')
{ ?>
<title>
<?php if ($id != '') { echo "Edit Punk"; } else { echo "New Punk"; } ?>
</title>
<h1><?php if ($id != '') { echo "Edit Punk"; } else { echo "New Punk"; } ?></h1>
<?php if ($error != '') {
echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error
. "</div>";
} ?>
<form name="pointsForm" action="" method="post" style="margin-top:50px;">
<?php if ($id != '') { ?>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<p>Name: <?php echo $name; ?> / <?php echo $currPoints; ?></p>
<?php } ?>
    <input type="number" name="addPoints" placeholder="Add Punk Points">
    <input type="number" name="remPoints" placeholder="Remove Punk Points">
    <input type="text" name="reason" placeholder="Reason">
    <input type="submit" name="submit" value="Update Punk Points">
</form>

</body>
</html>
<script>
$(function() {
    $('form[name="pointsForm"]').submit(function(e) {
        var reason = $('form[name="pointsForm"] input[name="reason"]').val();
        if ( reason == '') {
            e.preventDefault();
            window.alert("Enter a reason, fool!")
        }
    });
});
</script>

<?php
}

然后我的PHP用于编辑记录:

我从我添加的 URL/表单中获取变量的位置$currPoints = $currPoints+$addPoints-$remPoints;

然后在我的bind_param上只是添加$currPoints。

我相信我在这些线周围的某个地方出错了......或者我在哪里设置当前点 = ? .那应该是别的什么吗?

原谅我只是在学习PHP。

/*
EDIT RECORD
*/
// if the 'id' variable is set in the URL, we know that we need to edit a record
if (isset($_GET['id']))
{
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{
// make sure the 'id' in the URL is valid
if (is_numeric($_POST['id']))
{
// get variables from the URL/form
$id = $_POST['id'];
$addPoints = htmlentities($_POST['addPoints'], ENT_QUOTES);
$remPoints = htmlentities($_POST['remPoints'], ENT_QUOTES);
$reason = htmlentities($_POST['reason'], ENT_QUOTES);
$currPoints = $currPoints+$addPoints-$remPoints;

// if everything is fine, update the record in the database
if ($stmt = $mysqli->prepare("UPDATE points SET currPoints = ? , addPoints = ?, remPoints = ?, reason = ?
WHERE id=?"))
{
$stmt->bind_param("iiisi", $currPoints, $addPoints, $remPoints, $reason, $id);
$stmt->execute();
$stmt->close();
}
// show an error message if the query has an error
else
{
echo "ERROR: could not prepare SQL statement.";
}
// redirect the user once the form is updated
header("Location: index.php");
}
// if the 'id' variable is not valid, show an error message
else
{
echo "Error!";
}
}
// if the form hasn't been submitted yet, get the info from the database and show the form
else
{
// make sure the 'id' value is valid
if (is_numeric($_GET['id']) && $_GET['id'] > 0)
{
// get 'id' from URL
$id = $_GET['id'];
// get the record from the database
if($stmt = $mysqli->prepare("SELECT * FROM points WHERE id=?"))
{
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->bind_result($id, $name, $currPoints, $addPoints, $remPoints, $reason, $date);
$stmt->fetch();
// show the form
renderForm($name, $currPoints, $addPoints, $remPoints, $reason, NULL, $id);
$stmt->close();
}
// show an error if the query has an error
else
{
echo "Error: could not prepare SQL statement";
}
}
// if the 'id' value is not valid, redirect the user back to the view.php page
else
{
header("Location: index.php");
}
}
}

?>

对不起,如果我太含糊了。如果您需要更多信息,请告诉我。

谢谢!

哦,

发现了我认为的错误,在尝试使用它之前,您永远不会定义$currPoints,因此您无法拥有$currPoints = $currPoints+..,因为它尚未创建。PHP 或多或少会逐行读取,因此您必须查询 SQL 表并将$currPoints设置为等于数据库中的值,然后再执行$currPoints = $currPoints+$addPoints-$remPoints;

好的,这可能行不通,但您应该能够弄清楚我更改了什么并调整您的代码以使用它。我不会说这是"正确"的方式,但是当您在顶部使用 if 语句来处理提交与未提交的数据时,阅读和查看代码在做什么会更容易一些。

if (!isset($_GET['id'] || !isset($_POST['submit'])))
{
    echo "No Data!"
    return;
}
if (!is_numeric($_POST['id']))
{
    echo "Invalid ID!";
    header("Location: index.php");
    return;
}
// get variables from the URL/form
$id = $_POST['id'];
$addPoints = htmlentities($_POST['addPoints'], ENT_QUOTES);
$remPoints = htmlentities($_POST['remPoints'], ENT_QUOTES);
$reason = htmlentities($_POST['reason'], ENT_QUOTES);
$currPoints = 0;
//Check what the current points are first
// make sure the 'id' value is valid also
if (is_numeric($_GET['id']) && $_GET['id'] > 0)
{
    // get 'id' from URL
    $id = $_GET['id'];
    // get the record from the database
    if($stmt = $mysqli->prepare("SELECT * FROM points WHERE id=?"))
    {
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $stmt->bind_result($id, $name, $currPoints, $addPoints, $remPoints, $reason, $date);
        $stmt->fetch();
        // show the form
        renderForm($name, $currPoints, $addPoints, $remPoints, $reason, NULL, $id);
        $stmt->close();
    }
    else
        echo "Error: could not prepare SQL statement";
}
//Now update currPoints
$currPoints += $addPoints-$remPoints;

// if everything is fine, update the record in the database
if ($stmt = $mysqli->prepare("UPDATE points SET currPoints = ? , addPoints = ?, remPoints = ?, reason = ?
WHERE id=?"))
{
    $stmt->bind_param("iiisi", $currPoints, $addPoints, $remPoints, $reason, $id);
    $stmt->execute();
    $stmt->close();
}
else
    echo "ERROR: could not prepare SQL statement.";
// redirect the user once the form is updated
header("Location: index.php");