2014-03-28 74 views
0

所以我试图用html表单和一些PHP代码更新数据库字段,但是我无法让它工作,它不会引发错误,但不会更新字段?,我不知道是否因为即时消息也在网页上回应该字段?它似乎只是打印失败信息。使用HTML表单和PHP更新MySQL数据库字段数据

HTML:

<html> 
    <form method="post" name="update" action="updateform.php" /> 

    Description: 

    <input type="text" name="description" /> 

      <input type="submit" name="Submit" Value="update" /> 
    </form> 

    </html> 

PHP:

<?php 
mysql_connect("localhost", "root", "*****") or die("Connection Failed"); 
mysql_select_db("Days")or die("Connection Failed"); 
$description = $_POST['description']; 
$query = "UPDATE test SET description = '$description' "; 
if(mysql_query($query)){ echo "updated";} else{ echo "fail";} ?> 

我的回声(工作):

   <?php 
include("include/session.php"); 
//connect to the server 
$connect = mysql_connect("localhost","root","*****"); 

//connect to the database 
mysql_select_db("days"); 

//query the database 
$query = mysql_query("SELECT * FROM hobby WHERE id = '1' "); 

//ferch the results/convert results into an array 

    WHILE($rows = mysql_fetch_array($query)): 

     $description = $rows['description']; 

    echo "<div style ='font:15px/21px Arial,tahoma,sans-serif;color:#cf5c3f  </h>'>$description"; 
    endwhile; 






?> 
+0

可能它抛出一个错误。你只是没有在寻找它。使用mysql_error()来查看你的sql错误。仅供参考,您对[SQL注入]广泛开放(http://stackoverflow.com/q/60174) –

+0

您的'UPDATE'语句不需要'WHERE'吗? –

+0

尝试:'$ res = mysql_query($ query)或者die(“error:”.mysql_error());' –

回答

1
UPDATE table_name 
SET column1=value, column2=value2,... 
WHERE some_column=some_value 

更新查询样本,但我没有得到你的SQL。你错过了你的where条款

+0

如果在哪里丢失它将更新所有行,不是吗? –

+1

总是需要where子句,如果你想更新所有行,你可以使用'WHERE 1 = 1' – Matt

+0

我修改了我的代码,好像我错过了WHERE语句,但是我仍然收到错误:Table'days.days'不存在,我不知道为什么它重复的日子,并添加一个。 – user3429806

0

无论何时尝试更新表格的某些内容,您都需要使用WHERE条件。

这里是我的代码:

的test.html

<html> 
<form method="post" action="updateform.php" /> 

Name : <input type="text" name="name" /> </br> 

<input type="submit" name="Submit" value="update" /> 

</form> 
</html> 

updateform.php

<?php 
$name = $_POST['name']; 
$connection = mysqli_connect("localhost", "root", "Enter Passwd Here","Enter db_name here"); 
if(mysqli_connect_errno()) 
{ 
    echo "failed to connect " . mysqli_connect_error(); 
} 

if(isset($_POST['Submit'])) 
{ 
    $query = "UPDATE `test_table` SET `name` = '$name' WHERE `cost` = 500"; 
    $result = mysqli_query($connection,$query); 

    if (!$result) { 
    die('Error' . mysqli_error($connection)); 
    } 
    else 
    { 
    echo "Successfully updated"; 
    } 
} 
?> 

为了证明我创建了一个数据库表&与test_table 3场。 (ID,姓名,成本)

这是我表的结构:

enter image description here

执行上面的脚本之前,我们的表包含此数据单项

enter image description here

执行脚本后,第二行中的名称从ramu更改为shiva,因为我们在WHERE条件中将cost指定为500。

$query = "UPDATE `test_table` SET `name` = '$name' WHERE `cost` = 500"; 


enter image description here