0

我目前正在用 php 编写一个网站,不幸的是我遇到了障碍,我似乎无法让我的 amend.php 和 update.php 页面正常工作,并且在下面创建的显示页面上更新是代码。

Display page displays a table with descriptive columns when the hyperlink 'amend' is select it runs the amend.php.

修正

<?php
include 'connection.php';



$id = $_GET ['theid'];

$query = "SELECT * FROM place WHERE placeid = '$id'";

$results = mysqli_query($connection,$query);

$row = mysqli_fetch_assoc($results);
?>
<?php include 'header.php'; ?>

    <body>
        <h2>Amend</h2>

            <form method="post" action="updateplace.php">

                <fieldset class="fieldset-width1">

                    <input type="hidden" name="hiddenID" value= "<?php echo $row['placeid']; ?>" />
                    <br />
                    <br />
                    <label class="align" for="txtplacename">Place Name: </label>
                    <input type="text" name="txtplacename" value = "<?php echo $row['placename']; ?>" />
                    <br />
                    <br />
                    <label class="align"for="txtplacedesc">Place description: </label>
                    <input type="text" name="txtplacedesc" value = "<?php echo $row['placedesc']; ?>" />
                    <br />
                    <br />
                    <label class="align"for="txtplacecat">Place category: </label>
                    <input type="text" name="txtplacecat" value = "<?php echo $row['placecat']; ?>" />
                    <br />
                    <br />
                    <label class="align" for="txtplaceimg">Place image: </label>
                    <input type="text" name="txtplaceimg" value = "<?php echo $row['placeimg']; ?>" />
                    <br />
                    <br />
                    <input type="submit" value="Submit" name='submit' />
                    </fieldset>
            </form>
        </p>
<?php include 'footer.php'; ?>
    </body>

</html>

这个 php 页面的工作原理是它使用选定的 id 显示来自 phpmyadmin 的所有数据。

更新

<?php
include 'connection.php';

if(isset($_POST['submit'])){

 $placeid = $_POST['hiddenID'];
 $placename = $_POST['txtplacename'];
 $placedesc = $_POST['txtplacedesc'];
 $placecat = $_POST['txtplacecat'];
 $placeimg = $_POST['txtplaceimg'];
}

$query = "UPDATE place 
SET placename = '$placename';
SET placedesc = '$placedesc';
SET placecat = '$placecat';
SET placeimg = '$placeimg';
WHERE
placeid = '$placeid'";

mysqli_query($connection,$query);

header("location:admin.php");

当我选择提交按钮时,标题会重定向我,但是我更改的所有列都不会更新。任何帮助将不胜感激谢谢

4

2 回答 2

0

你不应该仅仅假设查询是成功的。用这个替换你的mysqli_query行来弄清楚发生了什么:

if (!mysqli_query($connection, $query)) {
    echo("Error description: " . mysqli_error($connection));
    die();
}

假设您有某种错误,它将阻止重定向和显示。如果您仍然获得重定向,则查询本身没有问题,而是您的$placeid值在数据库中不存在。

于 2017-01-06T17:38:02.150 回答
0

看你的UPDATE查询,

$query = "UPDATE place 
SET placename = '$placename';  <==
SET placedesc = '$placedesc';  <==
...

您正在使用 终止UPDATE每一行中的操作;,这会破坏您的查询。此外,您的UPDATE查询本身是错误的,应该是这样的:

$query = "UPDATE place SET placename = '$placename', placedesc = '$placedesc', placecat = '$placecat', placeimg = '$placeimg' WHERE placeid = '$placeid'";

旁注:了解准备好的语句,因为现在您的查询容易受到 SQL 注入攻击。此外,这里还有一本很好的读物,介绍了如何防止 PHP 中的 SQL 注入

于 2017-01-06T17:41:24.487 回答