2011-06-01 22 views
0
<form action="editauthor.php" method="post"> 
    <label>Author Name:</label> 
    <input type="text" name="txtUser" value="<?=$name;?>" /><br /><br /> 
    <label>Email:</label> 
    <input type="text" name="txtEmail" value="<?=$email;?>" /><br /><br /> 
    <input type="hidden" name="id" value="<?=$id?>" /> <!-- use hidden to hide id for using, but not display, here id is not important --> 
    <input type="submit" value="Edit" name="submit" /> 
</form> 
<?php 
    if(isset($_POST['submit'])) { 
    $con = @mysql_connect("localhost","root",""); 
    mysql_select_db("jokes",$con); 

    $name = $_POST['txtUser']; 
    $email = $_POST['txtEmail']; 

    $sql = "UPDATE authors 
       SET name = '".$_POST['txtUser']."', 
        email = '".$_POST['txtEmail']."' 
      WHERE id = ".$_GET['id']""; 

    $result = @mysql_query($sql, $con) or die(mysql_error()); 
    if($result) { 
     echo "New Author has been edited successfully!"; 
    } else { 
     echo "Cannot update this kind of author into the database. ".mysql_error(); 
    } 
    } ?> 

...它产生以下错误:MySQL的语法错误

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

+0

这个问题不在php代码中,而是在sql代码中。显示'$ sql'的转储,因为没有人知道你的变量的内容。 – KingCrunch 2011-06-01 14:35:58

+5

你听说过SQL注入吗? – 2011-06-01 14:36:11

回答

1

你缺少点这里WHERE id = ".$_GET['id']"";

+1

是的,但在这里输入代码示例时可能只是一个错字。错误消息显然是一个SQL语法错误,而缺少的。是一个PHP错误。 – 2011-06-01 14:41:19

1

在SQL语句结尾摆脱两个引号

$sql = "UPDATE authors SET name = '".$_POST['txtUser']."', email = '".$_POST['txtEmail']."' WHERE id = ".$_GET['id']; 
+0

这样的SQL语句末尾不需要空引号。除非您有单引号填写或另一个非空白值,否则可以省略。 – 2011-06-01 14:39:51

+0

它仍然不起作用 – 2011-06-01 18:01:07

1

正确的答案已经发布,但让我给你一些建议。

在未来,尝试这样做:

$result = @mysql_query($sql, $con) or die($sql.'<hr>'.mysql_error()); 

那么你可以随时看到mysql的一句话给你的问题。如果你不知道为什么它会打扰你,请复制粘贴它,然后在你的phpMyAdmin中试用它。

祝您好运与您的项目:]

3

问题是,你正在使用$ _GET [“身份证”],你的ID不是_GET方法及其在$ _ POST,所以您的查询就会有这样的事情

其中id = 后“=”因为没有价值$ _GET [“身份证”] 只是将其更改为$ _ POST

+0

+1:这是错误原因 – 2011-06-01 14:46:53

+0

接受答案然后:) – 2011-06-01 15:32:58

+0

在另一个PHP文件中,我已经使用了action =“editauthor.php?id = $ id&name = $ name&email = $ email“...但是sql结果显示id =什么都没有,并且仍然发生同样的错误。 – 2011-06-01 17:59:57

0
<?php 
    if(!empty($_POST)) 
    { 
     $con = @mysql_connect("localhost","root",""); 
     mysql_select_db("jokes",$con); 

     $name = $_POST['txtUser']; 
     $email = $_POST['txtEmail']; 
     $sql = "UPDATE authors SET name = '" . $_POST['txtUser'] . "', email = '" . $_POST['txtEmail'] . "' WHERE id = '" . $_GET['id'] ."'"; 

     $result = @mysql_query($sql, $con) or die(mysql_error()); 

     if($result) 
     { 
      echo "New Author has been edited successfully!"; 
     } 
     else 
     { 
      echo "Cannot update this kind of author into the database. " . mysql_error(); 
     } 

    } 
    else 
    { 
     $name = ''; 
     $email = ''; 
     $id = ''; 
    } 
?> 

<form action="./editauthor.php" method="post"> 
    <label>Author Name:</label> 
    <input type="text" name="txtUser" value="<?= $name ?>" /><br /><br /> 
    <label>Email:</label> 
    <input type="text" name="txtEmail" value="<?= $email ?>" /><br /><br /> 
    <input type="hidden" name="id" value="<?= $id ?>" /> <!-- use hidden to hide id for using, but not display, here id is not important --> 
    <input type="submit" value="Edit" name="submit" /> 
</form> 
+0

漂亮的代码,$ id的值在$ _POST中。它有什么作用? – 2011-06-02 03:40:52

0

最有可能的是造成了'一个语法错误,没有价值在txtUsertxtEmail字段。你的代码容易受到SQL注入攻击,现在注入“攻击”只是导致语法错误。

例如Miles O'Brien填写表单,让您的查询就会变成:

UPDATE authors 
... 
WHERE name='Miles O'Brien' 
        ^---syntax error here 

真的需要去拜访http://www.bobby-tables.com,然后rejigger你的代码看起来像这样:

  SET name = '" . mysql_real_escape_string($_POST['txtUser']) . "', 
       email = '" . mysql_real_escape_string($_POST['txtEmail']) . "' 
0

用途:

$sql = sprintf("UPDATE AUTHORS 
        SET name = '%s', 
         email = '%s' 
       WHERE id = %d ", 
       mysql_real_escape_string($_POST['txtUser']), 
       mysql_real_escape_string($_POST['txtEmail']), 
       mysql_real_escape_string($_POST['id'])); 

它会保护你免受SQL injection attacks,并纠正你用来获得身份证的方法 - 表格是pos吨,只有一个请求将被提出。