Insert into mysql database through php

82 views Asked by At

I try to insert some information about user but it gives error no database selected (im using phpmyadmin and xampp btw) code:

<?php
$username = $_POST['username'];
$name = $_POST['name'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];

if($password == $cpassword)
{
    mysql_escape_string($username);
    mysql_escape_string($name);
    mysql_escape_string($password);
    mysql_escape_string($cpassword);

    $md5pass = md5($password);
    mysql_select_db("users");
    mysql_query("INSERT INTO users (id, username, name, password) VALUES (DEFAULT, '$username', '$name', '$md5pass'") or die(mysql_error());
}
else
{
    die("Passwords don't match");
}
?>
1

There are 1 answers

2
Hassaan Salik On

You haven't established connection with your mysql database.

Use following code to make connection with server.

$link = mysql_connect('your servers address', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
else
{
//rest of your code
}