只需要一些php CRUD功能的帮助

时间:2017-01-06 17:30:25

标签: php cloud9

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

当超链接“修改”时,显示页面会显示一个包含描述性列的表格。选择它运行的是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");

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

2 个答案:

答案 0 :(得分:0)

您不应该只是假设查询成功。用此替换mysqli_query行以弄清楚发生了什么:

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

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

答案 1 :(得分: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'";

旁注:了解prepared statement因为您的查询现在容易受到SQL注入攻击。这里也很好地阅读how you can prevent SQL injection in PHP