www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: PHP and mySQL connection string

  1. #1
    Join Date
    Apr 2014
    Posts
    33

    PHP and mySQL connection string

    Hi Guys I'm in the middle of creating my own website and I'm having trouble with letting users to register I have created the code but when I click on the "submit" button I'm getting a blank white screen

    config.php
    Code:
    <?php 
    //Mysql Connect  |   mysql_connect("host= 3306","username","");
    //Below example to connect in localhost
    $a=mysql_connect("localhost","root","");
    
    //select your database
    $b=mysql_select_db("database_name",$a);
    ?>
    and this is the submit_form.php
    Code:
    <?php
    $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>';
    }
    ?>
    Hope someone can help me and give me some pointers please

    Rich

  2. #2
    Join Date
    Apr 2014
    Posts
    35
    Does the sql actually execute (i.e. are the values being inserted into the database)? If they are I'm thinking that the form is executing but taking you to the 'url' of the submission. Isn't there somewhere that you need to define a return url for the php to avoid. Sorry If that is a bit vague, my php skills arent up to scratch.

    ----------------
    webdev_monkey
    www.online-webdev-tools.com

  3. #3
    Join Date
    Apr 2014
    Posts
    33
    hey nothing is been inserted into the database as yet, i'm only using XAMPP at the moment as my local host before I move it onto my hosting server. I've been trying to sort this problem out since Monday and now I'm ready to bang my head against a wall

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    I would start by adding some defensive code (i.e.: never assume anything worked). Also, you might want to turn on display of all errors for now:
    PHP Code:
    <?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());
    }
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Apr 2014
    Posts
    33
    morning guys thanks for that NogDog I did what you suggested and I'm now getting this error message:

    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
    DB select failed: Unknown database 'database_name'

  6. #6
    Join Date
    Apr 2014
    Posts
    35
    Whats the name of the database you are trying to connect to? It looks like you are trying to access a database called 'database_name' which doesn't exist.

    ----------------
    webdev_monkey
    www.online-webdev-tools.com

  7. #7
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    The mysql_ functions are going to be depreciated at some point, so you should change it to mysqli or PDO.

    Also, you shouldn't insert the values into your database without doing any input verification since that's begging for an SQL injection. For example, if a person enters their first name, then it should contain only letters, not numbers, symbols (except for cyrillic letters, - and the 2 dots), spaces, etc... . Likewise, when someone enters an email, there should be a @ and at least 1 . (dot)

  8. #8
    Join Date
    Apr 2014
    Posts
    33
    hi guys the database is called "registration"

    so i take it it will look something like this database_name = "registration"

    and yes so i belive error404 so without changing too much of the code how do i change it to either mysqli or PDO?

  9. #9
    Join Date
    Apr 2014
    Posts
    33
    i changed it to mysqli and the following error came up
    Warning: mysql_select_db() expects parameter 2 to be resource, object given in C:\xampp\htdocs\config.php on line 13
    DB select failed: Unknown database 'database_name'

  10. #10
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    For now, I would not worry about the mysql deprecation -- it's been deprecated for quite awhile now, and "fixing" that is not trivial (but a very good thing to do). Let's fix the existing problems first, e.g. making sure your call to mysql_select_db() is specifying the correct database name, or if it is, then why is it failing?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  11. #11
    Join Date
    Apr 2014
    Posts
    33
    sorry guys new problem now sorry to be such a pain! hope you can help me
    Notice: Undefined variable: a in C:\xampp\htdocs\submit-form.php on line 3

    Warning: mysql_select_db() expects parameter 2 to be resource, null given in C:\xampp\htdocs\submit-form.php on line 3

    Warning: mysql_select_db() expects parameter 2 to be resource, object given in C:\xampp\htdocs\config.php on line 13
    DB select failed:

  12. #12
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    For the notice, it appears that line 3 refers to a variable named $a that is not defined at that point.

    For the warnings, it's probably because you changed mysql_connect() to mysqli_connect(), which will break every call to mysql_*() functions -- thus my comment above to not change to mysqli until you are ready to change everything.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  13. #13
    Join Date
    Apr 2014
    Posts
    33
    right ok its been a while since i have used php and connected to a database as i used to use microsoft access and the old connection.

    sorry to be a pain but what do i need to do to make it work then

  14. #14
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    If you changed mysql_connect() to mysqli_connect(), then change it back to mysql_connect(). Then make sure the database name specified in mysql_select_db() is correct.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  15. #15
    Join Date
    Apr 2014
    Posts
    33
    i've done what you said nogdog and i've got the following message


    Notice: Undefined variable: a in C:\xampp\htdocs\submit-form.php on line 3

    Warning: mysql_select_db() expects parameter 2 to be resource, null given in C:\xampp\htdocs\submit-form.php on line 3

    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
    DB select failed: Unknown database 'database_name'

    how do i declare what the database is called?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles