Problem in PHP programming. Please help

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Tue Oct 02, 2012 11:49 pm

Problem in PHP programming. Please help

Postby merahul63 » Wed Oct 03, 2012 12:05 am

Hi, I am new to PHP.

I am want to create a page on my website where my client can register with his information and the same should be added to the database directly, like name, contact number. I have already prepared my database and table in myPHP admin. Since I am new to PHP (only basic information I am aware about) I referred to youtube videos, to connect my page with the database with the help of dreamweaver, I did it correctly but I was unable to connect. I contacted my web hosting provider ( who said direct connection is not possible, I have to create a page with the following code and add it via require on my page

$db=mysql_connect("", "username", "password");
mysql_select_db("database", $db);

I am new to it and I do not know how to make this page, according to web hosting provider this is easy as the values can be directly added to the database without exrernal connection.

Can anybody spoon feed me on this, I do not know coding, whatever help you will provide I will cut copy it (though I can apply some basic understanding as I learnt basic PHP in school)

New php-forum User
New php-forum User
Posts: 2
Joined: Wed Oct 03, 2012 6:17 am

Re: Problem in PHP programming. Please help

Postby slice » Wed Oct 03, 2012 6:39 am

You're going to want to create a new page with a .php extension. So open 'notepad' on windows or another basic text editor. Save it as register.php.

Now you're going to need to go into phpMyAdmin and create a user for your database. It's best not to use your "root" account because if you are hacked they will be able to drop all your database tables.

Now this example is in PDO. Old mysql_ commands are depreciated and do no support prepared statements.

Code: Select all

$user = "username"; //database usersname
$password = "password"; //database password
$dsn = "mysql:host=localhost;dbname=database"; //change 'database' to your database name

    $pdo = new PDO($dsn, $user, $pass); // $pdo is now an object that can interact with your database
catch(PDOException $e)
    echo "Connection failed: " . $e->getMessage(); //print out error for why connection failed

$form = <<<EOL
<h2>Please enter your details:</h2>
<form action="" method="POST">
    <span>Full Name: </span><input type="text" name="reg_name" /><br/>
    <span>Email: </span><input type="text" name="reg_email" /><br/>
    <span>Contact number:</span><input type="text" name="reg_contact" /><br/>
    <input type="submit" name="reg_submit" value="Register Details" />

    $query = "INSERT INTO clients (name, email, contactNumber) VALUES (:name, :email, :contactNumber)"; // query using prepared statements
    $info = array(
        'name' => $_POST['reg_name'],
        'email' => $_POST['reg_email'],
        'contactNumber' => $_POST['reg_contact']

    $insert = $pdo->prepare($query); //query prepared in the $insert object
        echo "Information successfully saved to database";
        echo "Error adding information to database.";
    echo $form;

Using PDO and prepared statements, you are protected from attacks such as SQL injection.

Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 2 guests