Mysql_query插入

时间:2014-06-24 20:48:17

标签: php mysql

我在插入mysql查询时遇到问题。如果我在localhost上执行它,一切正常。但是,在我的网络服务器上,它不想工作,我也不知道为什么。我尝试以多种方式更改语法。

<?

if( $_POST )
{
 $dbhost ='ip address';
 $dbuser = 'username';
 $dbpassword = 'password';
 $database = 'db1214492_davidmszabo';

 $con = mysql_connect($dbhost, $dbuser, $dbpassword);

if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db($database, $con);


 $users_name = $_POST['name'];
 $users_email = $_POST['email'];
 $users_website = $_POST['website'];
 $users_comment = $_POST['comment'];

 $users_name = mysql_real_escape_string($users_name);
 $users_email = mysql_real_escape_string($users_email);
 $users_website = mysql_real_escape_string($users_website);
 $users_comment = mysql_real_escape_string($users_comment);


 $articleid = $_GET['id'];


if( !is_numeric($articleid) )
die('invalid article id');

      $query = "
  INSERT INTO `db1214492_davidmszabo`.`comments` (`id`, `name`, `email`, `website`,
    `comment`, `timestamp`, `articleid`) VALUES (NULL, '$users_name',        '$users_email', '$users_website', '$users_comment',
    CURRENT_TIMESTAMP, '$articleid');";

/* INSERT INTO `inmoti6_mysite`.`comments` (`id`, `name`, `email`, `website`,
    `comment`, `timestamp`, `articleid`) VALUES (NULL, '$users_name',
    '$users_email', '$users_website', '$users_comment',
    CURRENT_TIMESTAMP, '$articleid');";*/

 $sql = "INSERT INTO `db1214492_davidmszabo`.`comments` (`id`, `name`, `email`,      `website`, `comment`, `timestamp`,
  `articleid`) 
 VALUES (NULL , '$users_name', '$users_email', '$users_website', '$users_comment',      CURRENT_TIMESTAMP, '$articleid')";

 /*    $sqlTwo = 'INSERT INTO `db1214492_davidmszabo`.`comments` (`id`, `name`, `email`,     `website`, `comment`, `timestamp`,
  `articleid`) VALUES (\'\', \'cd\', \'ddsa@enauk.com\', \'dda.com\', \'dsaf\',     CURRENT_TIMESTAMP, \'1\');'; -  this is the code copied from the phpmyadmin when I insert something in the database */

 mysql_query($query);

 echo "<h2>Thank you for your Comment!</h2>";

  mysql_close($con);
}

?>

//编辑后 - 我不会尝试连接到localhost。我在$ dbhost =&#34;&#34;获得了一个IP地址。 - 这是我通过添加两个错误得到的错误:行error_reporting(E_ALL);和 ini_set(&#39; display_errors&#39;,1); - 并更改了与mysqli_connect();

的连接

警告:mysql_real_escape_string():无法通过socket&#39; /var/lib/mysql/mysql.sock'连接到本地MySQL服务器; (2)在第105行的/usr/local/pem/vhosts/479307/webspace/httpdocs/davidmszabo.com/articles/manage_comments.php中警告:mysql_real_escape_string():无法在/ usr /中建立到服务器的链接第105行的local / pem / vhosts / 479307 / webspace / httpdocs / davidmszabo.com / articles / manage_comments.php警告:mysql_real_escape_string():无法通过socket&#39; / var / lib连接到本地MySQL服务器/mysql/mysql.sock' (2)在第106行的/usr/local/pem/vhosts/479307/webspace/httpdocs/davidmszabo.com/articles/manage_comments.php中警告:mysql_real_escape_string():无法在/ usr /中建立到服务器的链接第106行的local / pem / vhosts / 479307 / webspace / httpdocs / davidmszabo.com / articles / manage_comments.php警告:mysql_real_escape_string():无法通过socket&#39; / var / lib连接到本地MySQL服务器/mysql/mysql.sock' (2)在第107行的/usr/local/pem/vhosts/479307/webspace/httpdocs/davidmszabo.com/articles/manage_comments.php中警告:mysql_real_escape_string():无法在/ usr /中建立到服务器的链接第107行的local / pem / vhosts / 479307 / webspace / httpdocs / davidmszabo.com / articles / manage_comments.php警告:mysql_real_escape_string():无法通过socket&#39; / var / lib连接到本地MySQL服务器/mysql/mysql.sock' (2)在第108行的/usr/local/pem/vhosts/479307/webspace/httpdocs/davidmszabo.com/articles/manage_comments.php中警告:mysql_real_escape_string():无法在/ usr /中建立到服务器的链接第108行的local / pem / vhosts / 479307 / webspace / httpdocs / davidmszabo.com / articles / manage_comments.php警告:mysql_query():无法通过socket&#39; / var / lib连接到本地MySQL服务器/mysql/mysql.sock' (2)在第155行的/usr/local/pem/vhosts/479307/webspace/httpdocs/davidmszabo.com/articles/manage_comments.php中警告:mysql_query():无法在/ usr /中建立到服务器的链接第155行的local / pem / vhosts / 479307 / webspace / httpdocs / davidmszabo.com / articles / manage_comments.php

然后在我的评论之后:

感谢您的评论

错误是这样的:

警告:mysql_close()要求参数1为资源,对象在第165行的/usr/local/pem/vhosts/479307/webspace/httpdocs/davidmszabo.com/articles/manage_comments.php中给出

1 个答案:

答案 0 :(得分:0)

您要插入的表的结构是什么?如果id列为AUTO_INCREMENT,则会尝试插入。

此外,我不确定CURRENT_TIMESTAMP是否为有效值(特别是如果列为DATETIME类型)。也许更有经验的MySQL大师可以纠正我。我总是使用NOW()代替

试试这个:

$sql = "INSERT INTO `$database`.`comments` (`name`, `email`, `website`, `comment`, `timestamp`, `articleid`) 
        VALUES ('$users_name', '$users_email', '$users_website', '$users_comment', NOW(), '$articleid')";