所以基本上它进入并在数据库中找到一个与我输入的单词匹配的单词并吐出一个有效的回复,但它会做所有其他废话。
这是我的 PHP 代码
<?php
// Connect to database
mysql_connect("*****", "***", "****");
mysql_select_db("***");
// If something is received
if($_POST)
{
if($_POST['action'] == "ask")
{
// Filter it to prevent SQL injections
$text = mysql_real_escape_string($_POST['stringdata']);
// Search it on the database
$q = mysql_query("SELECT `reply` FROM `poka` WHERE `word` = '$text' ORDER BY RAND()");
// Show result
if($r = mysql_fetch_assoc($q))
echo $r['reply'];
else
echo "Cannot find a reply";
}
elseif($_POST['action'] == "teach")
{
// Filter it to prevent SQL injections
$word = mysql_real_escape_string($_POST['word']);
$answer = mysql_real_escape_string($_POST['answer']);
// Insert it to the database
if( mysql_query("INSERT INTO `poka` VALUES(NULL, '$word', '$answer')") )
echo "ok";
else
echo "fail";
}
}
?>