我只是编程方面的初学者,所以我只是通过教程引用我的所有代码。幸运的是,我在youtube上找到了此在线教程,允许用户使用php在mysql中添加,更新和删除数据。我按照他的所有指示进行操作,但是我在其中添加了CSS之后,它停止了工作。

这不是一个普遍的问题,我只需要一些帮助。如果有人可以帮助我,万分感谢。非常感谢。

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "rssfeed";

    $connect = mysql_connect($servername, $username, $password, $dbname);
    if (!$connect) {
        die("Connection failed. Error" . mysql_error());
    }

    $database = mysql_select_db($dbname, $connect);
    if (!$database) {
        die("Can't select database");
    }

    $sql = "SELECT * FROM record";
    $data = mysql_query($sql, $connect);

    if (isset($_POST['update'])){
        $updateQuery = "UPDATE record SET name = '$_POST[name]', url = '$_POST[url]', description = '$_POST[desc]' WHERE name = '$_POST[hidden]'";
        mysql_query($updateQuery, $connect);
        header("Location: maintenance.php");
    };

    if (isset($_POST['delete'])){
        $deleteQuery = "DELETE FROM record WHERE name = '$_POST[hidden]'";
        mysql_query($deleteQuery, $connect);
        header("Location: maintenance.php");
    };

    if (isset($_POST['add'])){
        $addQuery = "INSERT INTO record (name, url, description) VALUES ('$_POST[iName]', '$_POST[iUrl]', '$_POST[iDesc]')";
        mysql_query($addQuery, $connect);
        header("Location: maintenance.php");
    };


    echo "<div class=center>
        <table id=myTable border=1>
        <tr>
        <th> Name </th>
        <th> URL </th>
        <th> Description </th>
        </tr>";

    while($record = mysql_fetch_array($data)) {
        echo "<form method=post action=maintenance.php>";
        echo "<tr>";
        echo "<td>" . "<input type=text name=name id=name value=" . $record['name'] . " </td>";
        echo "<td>" . "<input type=text name=url id=url value=" . $record['url'] . " </td>";
        echo "<td>" . "<textarea rows=1 cols=50 wrap=physical name=desc id=desc>" . strip_tags($record['description']) . "</textarea></td>";
        echo "<input type=hidden name=hidden value=" . $record['name'] . ">";
        echo "<td>" . "<input type=submit name=update id=update value=update" . " </td>";
        echo "<td>" . "<input type=submit name=delete id=delete value=delete" . " </td>";
        echo "</tr>";
        echo "</form>";


    }
        echo "</table>";
        echo "<table border=1>";
        echo "<form method=post action=maintenance.php>";
        echo "<tr>";
        echo "<td><input type=text name=iName></td>";
        echo "<td><input type=text id=url name=iUrl></input></td>";
        echo "<td><textarea rows=1 cols=50 name=iDesc></textarea></td>";
        echo "<td>" . "<input type=submit name=add id=add value=add" . " </td>";
        echo "</tr>";
        echo "</form>";
        echo "</table>";
        echo "</div>";

    mysql_close($connect);
    ?>

最佳答案

在您的查询中,$_POST[name]应该为$_POST[\"name\"]。但是,这太糟糕了,您非常愿意接受SQL注入。

请阅读this并停止使用mysql_query(已弃用)

关于php - MySQL-更新和删除查询不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27298638/

10-12 00:58