Jump to content

mysql_connect() error - Help please


1acid

Recommended Posts

Hi there, I am new, like fresh meat new  :o if anyone minds helping me, Id really appreciate it.

 

I had a look around for a few similar thread i pinged my localhost through command promt and everything was fine(as far as Im aware)

 

so the error im getting is :

 

Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in D:\Program Files\EasyPHP-12.0\www\init.php on line 5

 

Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in D:\Program Files\EasyPHP-12.0\www\init.php on line 5

 

The file init.php contains the following code:

 

<?php

include_once('config.php');

 

mysql_connect('DB_HOST', 'DB_USER', 'DB_PASS');

mysql_select_db('DB_NAME');

?>

 

 

the file config.php contains the following code:

 

<?php

$config["DB_HOST"] = 'localhost';

$config["DB_HOST"] = 'root';

$config["DB_PASS"] = '';

$config["db_NAME"] = 'craig';

?>

 

This is my actual file that is meant to add data to my database:

 

<?php

include_once('init.php');

if(isset($_POST['title'],$_POST['post'])){

    mysql_query("INSERT INTO posts SET

                    title = '{$_POST['title']}',

                    contents = '{$_POST['post']}'");

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <title>Untitled Document</title>

        <style type="text/css">

            label{display:black;}

        </style>

    </head>

 

    <body>

        <div id ="main">

            <form method ="post" action="">

                <div>

                    <label for="title"><br/>Title</lable>

                    <input type ="text" name="title" id="title"/>

                </div>

                <div>

                    <lable for="post"><br/>Post</label>

                    <textarea name ="text" id="post" rows="15" cols="50"></textarea>

                </div>

               

                <div>

                    <input type="submit" value="Post"/>

                </div>

            </form>

        </div>

       

    </body>

</html>

 

Am i being stupid? Im not very experienced with this stuff and im trying to follow along to a youtube series and learn a bit more.

 

please any help is greatly appreaciated.

 

Thanks

 

1acid

 

Link to comment
Share on other sites

try this

<?php
$host = 'localhost'; // Server Host Name
$user = 'root'; // Server User Name
$password = ''; // Server Password
$db = 'test'; // Your Database

$link = mysql_connect($host,$user,$password) or die('Error in Server information');
mysql_select_db($db,$link) or die('Can not Select Databasse');
?>

include this totaly

and do your scripts between

tags

Link to comment
Share on other sites

Thanks for the reply, that has removed the connect error message but now when i click the submit button it doesnt actually dd the data to the data base? Could you or anyone assist me with that maybe?

 

Thanks again

Link to comment
Share on other sites

ok, so where i have this <form method ="post" action=""> should i be naming this form post?

 

Im sorry im kinda new and tryna wrap my head around all of this.

 

thanks for the reply again. OTherwise how would I make post exist in my $_POST array?

 

Thanks

Link to comment
Share on other sites

You should first make sure the form has been submitted with if( strtolower($_SERVER['REQUEST_METHOD']) === 'post' ) {, then validate that each field you want to require has something entered in it, by first trimming any leading/trailing whitespace from it, then making sure it's not empty.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.