0

我有 MySQL 表,它有两列(名称和描述),我已经使用 HTML 表单在其中插入了一些数据。

但是,我试图弄清楚是否有办法让用户输入现有名称和不同的描述,以便更新该现有名称的描述,而不是使用现有名称和新描述添加新行。

这是我的PHP代码:

<html>
<head>
  <meta charset = "utf-8">
  <title>Search Results</title>
<style type = "text/css">
     body  { font-family: sans-serif; } 
     table { background-color: lightblue; 
             border-collapse: collapse; 
             border: 1px solid gray; }
     td    { padding: 5px; }
     tr:nth-child(odd) {
             background-color: white; }
  </style>
</head>
<body>
    <?php

        $db_connect = mysql_connect("hostname", "name", "password");
        $db_query = "SELECT * FROM urltable";

        // connect to MySQL
        if (!$db_connect)
        {
            die("Could not connect to database");
        }
        // open Products database
        if ( !mysql_select_db( "urls", $db_connect ) )
            die( "Could not open products database </body></html>" );

        $db_insert = "INSERT INTO urltable(URL, description) VALUES ('$URL', '$Description')";

        $db_insert_data = mysql_query($db_insert);

        if ($db_insert_data)
        {
            echo("<br>Input data is Added</br>");
        }
        else
        {
            echo("<br>Input data not Added</br>");
        }   

        // query Products database
        if ( !( $result = mysql_query( $db_query, $db_connect ) ) ) 
        {
            print( "<p>Could not execute query!</p>" );
            die( mysql_error() . "</body></html>" );
        } // end if

        mysql_close( $db_connect );

    ?><!-- end PHP script -->

     <table border="1">
        </br>
        <caption>URL TABLE</caption>
        </br>
        <?php

            // build table headings
            print( "<tr>" );
            print("<th>URL</th>");
            print("<th>Description</th>");
            print( "</tr>" );

            // fetch each record in result set
            while ( $row = mysql_fetch_row( $result ) )
            {
                // build table to display results
                print( "<tr>" );
                foreach ( $row as $value ) 
                    print( "<td>$value</td>" );
                print( "</tr>" );
            } // end while
        ?><!-- end PHP script -->
    </table>

    </br>
    <a href="insert.html">Home</a></br>

</body>
</html>

现在它只会插入输入的任何数据,但我不知道如何仅在输入现有名称的情况下更新描述。我可以使用条件语句来检查现有名称吗?

4

2 回答 2

3

您应该查看 MySQL 中的 UPDATE 命令。

Erdem 的回答无效查询。

它应该看起来像这样:

$db_update = "UPDATE urltable SET description = \"New description\" WHERE url = \"http://urlyouwanttoupdate.com\"";

mysql_query($db_update);

补充乔所说的话。出于安全目的,您不能允许用户直接在查询中输入。这使您的数据库容易受到称为“SQL 注入”的攻击。网上已经有很多关于此的内容。

这是您正在使用的更新和插入功能的安全示例。

$safe_description = mysql_real_escape_string($Description);
$safe_url = mysql_real_escape_string($URL);

$db_update = "UPDATE urltable SET description=\"".$safe_description."\" WHERE url =\"".$safe_url."\"";
mysql_query($db_update);

对于安全插入:

$db_insert = "INSERT INTO urltable(URL, description) VALUES (\"".$safe_url."\", \"".$safe_description."\")";

$db_insert_data = mysql_query($db_insert);
于 2013-04-17T16:15:57.277 回答
1

我们使用更新。

"UPDATE tablename SET cloumnname = 'newname' WHERE ID = 'idoftheperson'"

也不要使用mysql_query()请使用 PDO 库或 mysqli。

这是一个如何在 php 中使用 UPDATE 命令的教程。

http://www.w3schools.com/php/php_mysql_update.asp

如果要检查数据库的现有名称或 ID,则需要计算 mysql 中的数据。为此请这样做

            <?php
            /* Open a connection */
            $link = mysqli_connect("localhost", "my_user", "my_password", "world");

            /* check connection */
            if (mysqli_connect_errno()) {
                printf("Connect failed: %s\n", mysqli_connect_error());
                exit();
            }

            $query = "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 20";
            if ($stmt = mysqli_prepare($link, $query)) {

                /* execute query */
                mysqli_stmt_execute($stmt);

                /* store result */
                mysqli_stmt_store_result($stmt);

                 if(mysqli_stmt_num_rows($stmt) >0){
                    /* do the mysql update here */
                 }

                /* close statement */
                mysqli_stmt_close($stmt);
            }

            /* close connection */
            mysqli_close($link);
            ?>

谢谢

于 2013-04-17T16:08:45.130 回答