-1

I have an error in the php code for submitting a comment, the problem is the 4th line of code:

php_network_getaddresses: getaddrinfo failed: the requested name is valid, but no data of the requested type was found.

Any ideas?

    <?php
if( $_POST )
{
  $con = mysql_connect('%', 'myuser', 'mypassword');

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

  mysql_select_db("_mysite", $link);

  $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 `_mysite`.`comments` (`id`, `name`, `email`, `website`,
        `comment`, `timestamp`, `articleid`) VALUES (NULL, '$users_name',
        '$users_email', '$users_website', '$users_comment',
        CURRENT_TIMESTAMP, '$articleid');";

  mysql_query($query);

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

  mysql_close($con);
}
?>
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Jan 22 '16 at 17:46
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jan 22 '16 at 17:46
  • What's unclear about the error message in specific? – PeeHaa Jan 22 '16 at 17:47
  • This is the first time i have ever seen `%` host. `mysql_connect('%'` – Hanky Panky Jan 22 '16 at 17:50

2 Answers2

0

The % is mysql's wildcard, when you use it for your user's allowed host, it means 'any host', in your case here it should probably be localhost.

Not related to your question, but if you're not aware of it you should consider moving away from mysql_ functions which are deprecated, mysqli is the recommended replacement.

Meroje
  • 344
  • 2
  • 5
  • You have a typo in "mysqli is the recommended replacement" you probably meant to type PDO – PeeHaa Jan 22 '16 at 17:52
  • meant the direct replacement as it is almost plug and play. I do prefer PDO though don't worry – Meroje Jan 22 '16 at 17:54
0

Replace $link with $con in this line mysql_select_db("_mysite", $link);. This is the first time i am seeing any server name as %. I don't have much idea about it. But, you can replace % with localhost or port number or IP Address

<?php
if($_POST)
{
  $con = mysql_connect('localhost', 'myuser', 'mypassword');

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

  mysql_select_db("_mysite", $con);

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

  $articleid = $_GET['id'];

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

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

  mysql_query($query);

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

  mysql_close($con);
}
?>

Please stop using mysql* functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi.

Using MySqli

<?php
if($_POST)
{
  $con = mysqli_connect("localhost", "myuser", "mypassword", "_mysite");
  if (!$con) {
    die('Could not connect: ' . mysqli_connect_error());
  }

  $users_name = mysqli_real_escape_string($con, $_POST['name']);
  $users_email = mysqli_real_escape_string($con, $_POST['email']);
  $users_website = mysqli_real_escape_string($con, $_POST['website']);
  $users_comment = mysqli_real_escape_string($con, $_POST['comment']);

  $articleid = $_GET['id'];

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

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

  mysqli_query($con,$query)

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

  mysqli_close($con);
}
?>

Refer php_network_getaddresses: getaddrinfo failed: for more details.

Community
  • 1
  • 1
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77