用PHP存储注释

用PHP存储注释,php,mysql,Php,Mysql,朋友们好,我写了以下代码在MySQL中插入评论(比如Facebook),但我没有成功。请帮帮我 这是html页面 <html> <head><title>ABC</title> </head> <body> <form method="GET" action="try1.php"> <input type="text" name="like">

朋友们好,我写了以下代码在MySQL中插入评论(比如Facebook),但我没有成功。请帮帮我

这是html页面

    <html>
<head><title>ABC</title>
    </head>
    <body>
        <form method="GET" action="try1.php">
    <input type="text" name="like">
    <input type="submit" name="Comment" value="dislike">    
</form>
</body>

基础知识
下面是php代码

    <html>
<head><title> </title>
    </head>
    <body>


    <?

//connecting to database

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';

$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(!$conn )
{
  die('Could not connect: ' . mysql_error());
}
  mysql_select_db("try", $conn);
    $a= array('comments' =>'$_GET["like"]');

  mysql_query("INSERT INTO try1 (Comments) VALUES ('$a')",$conn);
  echo "Record Inserted in table";
mysql_close($conn);
?>
</body>

你不能这样做:

 $a= array('comments' =>'$_GET["like"]');

  mysql_query("INSERT INTO try1 (Comments) VALUES ('$a')",$conn); 
由于
$a
不是字符串,因此当您将其包含在字符串参数中时,它只是作为“Array”输出,因此您可以在数据库中看到Array

这会管用的

mysql_query("INSERT INTO try1 (Comments) VALUES ('{$a['comments']}')",$conn);
然而,我强烈建议在问题的评论中听取建议。虽然对于学习练习很有用,但您可能希望研究如何使用php web框架,如codeigniter或cakephp 您正在尝试向查询中插入数组

您需要从数组中提取值。您可以在数组中循环(这将允许多个值),也可以提取单个值

以下示例在数组中循环并生成查询:

foreach($array as $column => $value){
    // Append the columns string with the column name
    $columns .= "`$column`,";
    // Escape and append the values string
    $values .= "'".mysql_real_escape_string($value)."',";
}

// Remove the trailing commas
rtrim($columns, ",");
rtrim($values, ",");

$SQL = "INSERT INTO try1 ($columns) VALUES ($values)";

mysql_query($SQL,$conn);
或者,您可以像这样提取和转义数组值

mysql_query("INSERT INTO try1 (count) VALUES ('".mysql_real_escape_string($a['comments'])."')",$conn);
一些建议 请不要使用
mysql.*
函数,因为它们现在已被弃用!看见您应该使用PDO或Mysqli

见以下参考资料:

  • 替换

    $a= array('comments' =>'$_GET["like"]');
    mysql_query("INSERT INTO try1 (count) VALUES ('$a')",$conn);
    
    为此:

    $a= array('comments' =>$_GET["like"]);
    mysql_query("INSERT INTO try1 (count) VALUES ('".mysql_real_escape_string($a['comments'])."')",$conn);
    

    您到底需要什么帮助?值正在插入到表中???现在更改您的密码@Fasil kk yes,但刚刚插入了单词“array”。@Ashish请参阅我的编辑。您需要确保转义您的数据!此外,您不应该使用
    mysql.*
    函数!@本:你认为我能在15岁的时候学会这么多的编程语言吗?阿什,我学会了!我所知道的一切都是从9岁开始自学的。像这样的网站真的很有帮助,因为这是他们在这里的目的。。。我的答案可能看起来很复杂,但如果你把它分解,它是相当简单的,你会学到很多:-)。如果您有任何进一步的问题,请随时给我发电子邮件。我的地址在我的个人资料中:-)