这是给我这个错误的代码。我已经四处搜索,发现了相关问题,但我无法应用实施。这有些不对劲,超出了我的理解......
function add_user_to_db()
{
$dbhost = "111.111.111.111";
$dbuser = "Bob";
$dbpass = "password";
$connection = mysql_connect($dbhost, $dbuser, $dbpass);
if(!$connection){die('Could not connect: '.mysql_error());}
echo 'Connected to Vikings Game DB!';
mysql_select_db('vgDB');
$tb_result = mysql_query("SHOW TABLES LIKE 'Players'");
$table_exists = mysql_num_rows($tb_result) > 0;
if($table_exists)
{
if( isset($_POST["ID"]) && isset($_POST["NAME"]) && isset($_POST["COMMENT"]) )
{
$id = $_POST['ID'];
$name = $_POST["NAME"];
$comment = $_POST["COMMENT"];
//Check if a row exists
$row_result = mysql_query("SELECT `".$id."` FROM Players");
if($row_result == FALSE)
{
$add_user_query = mysql_query( "INSERT INTO Players( ID , NAME , COMMENT )VALUES('123','Bob','Bob's comment')" );
$retval = mysql_query($add_user_query, $connection);
if(!$retval) die("Could not insert data: ".mysql_error());
echo "User '".$id."' was added successfully!";
}
else
{
$name_read = mysql_query("SELECT NAME FROM Players WHERE ID = `".$id."`");
$comment_read = mysql_query("SELECT COMMENT FROM Players WHERE ID = `".$id."`");
echo "Reading data of user (`".$id."`): Name = `".$name_read."`; Comment: `".$comment_read."`";
}
}
}
mysql_close($connection);
}
答案 0 :(得分:1)
我认为你的问题在以下几行:
$add_user_query = mysql_query( "INSERT INTO Players( ID , NAME , COMMENT )VALUES('123','Bob','Bob's comment')" );
尝试修改(仅用于测试目的),如下所示:
$add_user_query = mysql_query( "INSERT INTO Players( ID , NAME , COMMENT )VALUES('123','Bob','Bob comment')" );
如果你使用它,就意味着'鲍勃的评论'是问题,鲍勃引起它之后的引用。