Hi Guys as I have mentioned before I'm having problems with connecting my website to a sql database,

config.php

<?php 

ini_set('display_errors', true); // change to false when you go live
error_reporting(E_ALL);

//Mysql Connect  |   mysql_connect("host= 3306","username","");
//Below example to connect in localhost
//$a=mysql_connect("localhost","root","");
if($a == false) {
    die("Connect failed: ".mysql_error());
}
//select your database
$b=mysql_select_db("database_name",$a);
if($b == false) {
    die("DB select failed: ".mysql_error());
}
?>

submit_form.php

<?php
//select your database
//$b=mysql_select_db("database_name",$a);
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$username=$_POST['username'];
//$confirmusername=$_POST['confirmusername'];
$password=$_POST['password'];
$confirmpassword=$_POST['confirmpassword'];
$email=$_POST['email'];
$confirmemail=$_POST['confirmemail'];

//Database connection
require_once("config.php");

//mysql query to insert value to database
$query=mysql_query("INSERT INTO registration (`firstname`, `lastname`, `username`, `confirmusername`, `password`, `confirmpassword`, `email` ,`confirmemail`) VALUES ('$firstname', '$lastname', '$username', '$password', '$confirmpassword', '$email' , '$confirmemail')");



//if value inserted successyully disply success message
if($query)
{

    echo 'Registred successfully..!!</div>';
}else
{
//error message
    echo '<unable to registred !!</div>';
}
?>

I have done a few changes and now I'm getting this error message, at least it looks more prommising than a week ago where I was getting just a full blank white page

Notice: Undefined variable: a in C:\xampp\htdocs\config.php on line 9
Connect failed:

You are getting that error because $a is not declared. Uncomment line 8 on your config.php and see what happens.

the php dosent seem to like that as is that the beginning of the "if" statement?

Uncomment this line from your config.php

$a=mysql_connect("localhost","root","");

hi k99rs thanks for that! I need to change mysql to my mysqli

i got the following errors...

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\xampp\htdocs\config.php on line 8

Notice: Undefined variable: b in C:\xampp\htdocs\config.php on line 14
DB select failed:

hi k99rs thanks for this links I have followed what the first link has told me what to do and i found it much much easier

i have got the following error now

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\submit-form.php on line 17

mysqli_query do need two parameters. It needs the connection to the database and the query you would like to run on the database.

here is an example

<?PHP
    //create a connection to your database and store it
    $con = mysqli_connect("localhost", "k99rs", "password","database");

    //Check if the connection was any good
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    // now you cant run a query using mysqli_query
    // The first parameter is your connection to the database
    // the second parameter is the query you would like to run
    mysqli_query($con, "SELECT * FROM Users");  
?>

thanks for that but i dont want to select all users from the database i'm inserting data into the database because at the moment its a register form

Hi Guys I have put the following code into the "submit_form.php"

<?php
//select your database
//$b=mysql_select_db("database_name",$a);
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$username=$_POST['username'];
$confirmusername=$_POST['confirmusername'];
$password=$_POST['password'];
$confirmpassword=$_POST['confirmpassword'];
$email=$_POST['email'];
$confirmemail=$_POST['confirmemail'];
//Database connection
require_once("config.php");
//mysql query to insert value to database
$query="INSERT INTO registration (`firstname`, `lastname`, `username`, `confirmusername`, `password`, `confirmpassword`, `email` ,`confirmemail`) VALUES ('$firstname', '$lastname', '$username', '$password', '$confirmpassword', '$email' , '$confirmemail')";

$result = mysqli_query($query);
//if value inserted successyully disply success message
if(!$result) {

    die("The following SQL Failed $query";)/>/>;
}
echo 'Registred successfully..!!</div>';
?>

and I'm now getting the following error message

Parse error: syntax error, unexpected ';' in C:\xampp\htdocs\submit-form.php on line 21

die("The following SQL Failed $query";)/>/>;

should be:

die("The following SQL Failed $query");

brilliant thanks for that

I'm now getting this thank god at least it seems positive

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\submit-form.php on line 17
The following SQL Failed INSERT INTO registration (firstname, lastname, username, confirmusername, password, confirmpassword, email ,confirmemail) VALUES ('richard', 'Hemmings', 'hemmo001', 'password', 'password', 'richardgwhemmings@msn.com' , 'richardgwhemmings@msn.com')

mysqli_query needs the connection resource as parameter.

hey pritaeas sorry to sound like an idiot but how do i go about that then?

First create your connection an store it in a variable for this example i will call it $connection

$connection = mysqli_connect("localhost", "k99rs", "password", "database" )

Once you have your connection you can add it in mysqli_query as shown below. The first parameter is your connection and the second parameter is your query

mysqli_query($connection, $query)

thanks for this sorry to sound like an idiot but where do i need to put mysqli_query($connection, $query)? is it in config.php or submit_form.php?

In submit.php

Replace

$result = mysqli_query($query);

With

$result = mysqli_query($connection,$query);

let the $connection be your connection you created to connect to the database

hey thanks for this i'm now getting the following:

Notice: Undefined variable: con in C:\xampp\htdocs\config.php on line 11

Warning: mysqli_close() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\config.php on line 11
The following SQL Failed INSERT INTO registration (firstname, lastname, username, confirmusername, password, confirmpassword, email ,confirmemail) VALUES ('richard', 'Hemmings', 'hemmo001', 'password', 'password', 'richardgwhemmings@msn.com' , 'richardgwhemmings@msn.com')

I'm a little disapointed as I thought that would of done it and it would of worked :(

i've manged to sort out the above problem now the following has arisen

Warning: mysqli_query(): Couldn't fetch mysqli in C:\xampp\htdocs\submit-form.php on line 19
The following SQL Failed INSERT INTO 'registration' (firstname, lastname, username, confirmusername, password, confirmpassword, email ,confirmemail) VALUES ('richard', 'Hemmings', 'hemmo001', 'password', 'password', 'richardgwhemmings@msn.com' , 'richardgwhemmings@msn.com')

how does your code look now?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.