2013-04-17 42 views
0

我有了在我已插入使用HTML表单的一些数据两列(名称和说明)MySQL表存在于行单元格中的MySQL表。如何更新使用PHP

但是我试图找出是否存在,如果用户输入现有的名称和不同的描述,以便更新为现有名称,而不是与现有的名称和新的说明中添加新行的描述方式。

这是我的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> 

现在它只是插入输入任何数据,但我无法弄清楚如何做说明的更新只有在输入现有的名称。我可以使用条件语句来检查现有名称吗?

+2

一旦你得到了它的工作,修复SQL注入漏洞。 – Joe

回答

1

我们使用UPDATE。

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

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

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

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

如果您想查询数据库中退出名称,或者您需要在MySQL计数数据ID。为做到这一点,请

  <?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); 
      ?> 

感谢

+0

您的原帖的查询工作正常。也不需要担心注入攻击,这只会在一两周内在本地主机上运行。它永远不会活着。它的突出部分用于测试目的。再次感谢! – Nick

3

你应该看看在MySQL UPDATE命令。

埃德姆的回答一个无效的查询。

它应该是这个样子:

$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); 
+0

我的回答是对的。而且你不应该使用mysql_query。 –

+0

我看到你修好了。您前面提到:。“UPDATE表名SET列名= 'NEWNAME' WHERE = 'ID' – Kaiwa

+0

是的,它是错误的谢谢你。 –