2013-05-04 83 views
1

我做了一个html表单,更新mySQL DB中的表中的一些字段成功更新后,应该指示我成功的页面,否则给错误消息。指示错误mysql表更新没有结果

我使用形式验证,但到目前为止,什么也没有发生在我的表中的字段只指示我到一个空白页

这里是表单代码:

<html> 
    <body> 
    <form action="http://localhost/wordpress/orgupdate.php" method="post" name="myForm"> 
     Name <input id="name" type="text" name="name" /> 
     Telephone <input id="telephone" type="text" name="telephone" /> 
     Fax <input id="fax" type="text" name="fax" /> 
     Web address <input id="webaddress" type="text" name="webaddress" /> 
     State <input id="state" type="text" name="state" /> 
     Address <input id="address" type="text" name="address" /> 
     <input type="submit" name= "submit" value="update" /> 
    </form> 
    </body> 
</html> 


<script type="text/javascript">// <![CDATA[ 
    /* JS validation code here */ 

    function validateForm() 
    { 
     /* Validating name field */ 
     var x=document.forms["myForm"]["name"].value; 
     if (x==null || x=="") 
     { 
      alert("Name must be filled out"); 
      return false; 
     } 
     /* Validating email field */ 
     var x=document.forms["myForm"]["telephone"].value; 

     if (x==null || x=="") 
     { 
      alert("telephone must be filled out"); 
      return false; 
     } 
    } 
// ]]> 
</script> 

,这是我的php文件:

<?php 
//establish connection 
$con = mysqli_connect("localhost","xx","xxxx","android_app"); 
//on connection failure, throw an error 
if(!$con) { 
    die('Could not connect: '.mysql_error()); 
} 

//get the form elements and store them in variables 
$name=$_POST['name']; 
$telephone=$_POST['telephone']; 
$fax=$_POST['fax']; 
$webaddress=$_POST['webaddress']; 
$state=$_POST['state']; 
$address=$_POST['address']; 

$query= update islamic_organisation SET (Telephone ='$telephone', Fax ='$fax', WebAddress ='$webaddress', state ='$state', Address ='$address' WHERE Name ='$name'); 

//Redirects to the specified page 
header("Location: http://localhost/wordpress/?page_id=857"); 
?> 

有什么想法吗?

+1

你的更新查询是错误的,而函数名是可疑的 – samayo 2013-05-04 08:27:11

+0

首先,你如何不在'$ query = update ...'上得到解析错误,它需要正确引用。其次,你永远不会执行你的查询,只将它分配给一个变量。 – Jon 2013-05-04 08:27:13

+2

我恳求你,在将此代码推向生产之前,请阅读有关SQL注入的内容。 :) – spacediver 2013-05-04 09:39:33

回答

2
$query= "update islamic_organisation SET Telephone ='$telephone', Fax ='$fax', WebAddress ='$webaddress', state ='$state', Address ='$address' WHERE Name ='$name'"; 
$result=mysqli_query($con,$query) or die(mysqli_error()); 

编辑1:

printf("Affected rows (UPDATE): %d\n", mysqli_affected_rows($con)); 
+0

我试过你的查询仍然没有更新! – user2309720 2013-05-04 09:01:30

+0

@ user2309720,如果没有错误返回,那么你的查询没有问题。你可以用mysqli_affected_rows($ con)来检查是否更新了一些行,如果更新了零记录,那么你的查询没有找到你指定的where条件的记录 – Amir 2013-05-04 09:07:47

+0

我查看表并且所有的字段都是完整的,没有变化 – user2309720 2013-05-04 09:10:23

2

几点:

  1. 如果你得到空白页通常意味着有一个错误的地方,但没有展示,因为您已关闭调试。

    打开调试通过将WP-config.php文件,并将其设置为true:

    define('WP_DEBUG', true); 
    
  2. $query= update线真的很奇怪。正如乔恩在 评论中提到的那样,您应该在那里得到一个错误。
  3. 当在WordPress中为表单命名字段时,请避免使用 通用字词,如“name”,“city”,并使用更多独特字词。

UPDATE:(重要)

我只注意到你是把原始用户输入的SQL语句。您应该保护您的查询,防止SQL注入

这是一个answermysqli_query问题,告诉你如何做到这一点。

+0

我已经在没有任何事情发生的情况下调试调试 我已经更新了我的查询以下Amir的脚本也没有更新字段 – user2309720 2013-05-04 09:08:56

+0

在http:// localhost/wordpress/orgupdate。 PHP添加一些回声“你好”;以测试页面是否开始在那里执行。 – user850010 2013-05-04 09:19:40

+0

现在它正在更新并将我引导到成功页面......但我只是想知道如果我需要使它有点专业,如果用户键入无效的名称,它会显示错误,而不是成功页面我该怎么做? – user2309720 2013-05-04 09:33:56