MySQL - UPDATE和DELETE QUERY无法正常工作

时间:2014-12-04 16:09:42

标签: php mysql sql-update sql-delete

我只是编程方面的初学者,所以我只是通过教程来引用我的所有代码。幸运的是,我在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);
    ?> 

2 个答案:

答案 0 :(得分:0)

在您的查询中,$_POST[name]应为$_POST[\"name\"]。但是,这很糟糕,你对SQL注入非常开放。

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

答案 1 :(得分:0)

你的回声输出中有几个错误..

echo "<td>" . "<input type=text name=name id=name value=" . $record['name'] . " </td>";

你已经忘记了每个html-element-attribute的单引号,而且在多个输入元素的末尾就更大了......

试试这个:

echo "<td><input type='text' name='name' id='name' value='" . $record['name'] . "'> </td>";