0

stackoverflow 这是我的第一篇文章:D。现在开始做生意,我正在创建一个私人游戏网站,它将提取 IP 地址(不确定这是否合法,但我希望代码完成以防万一,所以我可以建立我的网站)并存储它们在 mysql 数据库中,因为名称已经在表中。我有一个获取用户输入的表单,即人名:

                <form action="/PHP/MembersIP.php" method="post">
                In Game Name: <input type="text" name="Name">
                <input type="submit">

我有我的外部 php 文件:

            //the queries are written into their values
            $selectname = "SELECT * from Members WHERE Name = " . $_POST['Name'];
            $updateIP = "UPDATE Members SET IP = " . $_SERVER['REMOTE_ADDR'] . "WHERE Name = " . $_POST['Name'];

            //check if the name is valid, if so add ip address in the row of the name given
            if (!mysql_query($selectname) ) {
            die('Your in game name was not found inside the database. Please check for misspellings, and try again. If problem persists please contact a guild official, or site manager. Inform them of the error: ' . mysql_error());
            } else {mysql_query($updateIP);}

当为名称输入假值时,在测试时我得到了正确的字符串:

 Your in game name was not found inside the database. Please check for misspellings, and try again.If problem persists please contact a guild official, or site manager. Inform them of the error: Unknown column 'lol' in 'where clause'

但是由于错误代码看起来很奇怪,我在我的数据库中输入了一个真实的姓名,并且出现了同样的事情:

 Your in game name was not found inside the database. Please check for misspellings, and try again. If problem persists please contact a guild official, or site manager. Inform them of the error: Unknown column 'name' in 'where clause'

所以我知道这是我的第一个查询(第二个查询也会失败,因为我也是这样做的)调用列“名称”并找到值有问题$_POST["Name"]。我只是想不通,所以有人可以帮助一下,并解释一下。对于任何提示,请等到我得到解释后再给我您的其他信息。

谢谢

编辑:

谢谢大家,我解决了这个问题。当我最初编写脚本时,它给了我错误,我将其更改为:

SELECT * from Members WHERE Name = '" . $_POST['Name'] . "';"; 

由于我没有成功条款,它给了我一个空白页,所以我认为它有效。查了下自己的表,ip单元格还是空白,以为根本没有运行,于是带着错误码回到root的base,搜索代码的问题,但还是不行找到任何问题,除了我刚刚修复的那个没有“工作”的问题。那是所有代码对我失去意义的那一刻,我来到这里发布。但是在查看其他答案后,我注意到我需要一个脚本来测试是否有任何代码运行,所以我添加了一个成功脚本,然后在我重新测试之前:

SELECT * from Members WHERE Name = '" . $_POST['Name'] . "';";

我注意到我编写的其他代码与弹出错误的方式相同:

UPDATE Members SET IP = " . $_SERVER['REMOTE_ADDR'] . "WHERE Name = " . $_POST['Name'];

最后我添加了成功脚本并编辑了两个查询,并且成功了。这是更新的php:

$selectname = "SELECT * from Members WHERE Name = '" . $_POST['Name'] . "';" ;
$updateIP = "UPDATE Members SET IP = '" . $_SERVER['REMOTE_ADDR'] . "' WHERE Name = '" . $_POST['Name'] . "';";
if (!mysql_query($selectname) )
{
    die('Your in game name was not found inside the database. Please check for misspellings, and try again. If problem persists please contact a guild official, or site manager. Inform them of the error: ' . mysql_error());
}
else
{
    mysql_query($updateIP); 
    echo "Thank you " . $_POST['Name'] . ", this device's Ip Address has been saved. You may now edit your account, and use guild resources from this device.";
}

故事的寓意总是在测试代码中添加字符串。大家

4

1 回答 1

-2

如何将值放在引号中,例如Name = 'test'

在您的情况下,查询将是...

"SELECT * from Members WHERE Name = '" . $_POST['Name'] . "'";
于 2013-09-03T01:11:16.443 回答