Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2012
    Posts
    2

    Question Unanswered: MySQL Database Connection

    I have just one question:
    When I make 2 text fields on a web page and a person enters their information, how do I keep the web connection on the page a person is on and not goto a php page?
    Example:
    * My HTML Form action="database.php":
    <form class="marginForm" onsubmit="return validateForm()" method="post" action="database.php" name="myform">
    <p>Enter information to win the $500 shopping spree:</p>
    <p>Name: <input name="name" type="text" style="width: 245px" /></p>
    <p>Email: <input name="email" type="text" style="width: 250px" /></p>
    <input type="submit" value="Submit" />
    </form>

    *My php file:
    <?php

    define('DB_NAME', 'my database name');
    define('DB_USER', 'my user name');
    define('DB_PASSWORD', 'my password');
    define('DB_HOST', 'my host connection string');


    $con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    $db_selected = mysql_select_db(DB_NAME, $con);
    if (!$db_selected)
    {
    die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
    }

    $value = $_POST['name'];
    $value1 = $_POST['email'];

    $sql = "INSERT INTO Table Name (name, email) VALUES ('" . $value . "', '" . $value1 ."')";

    if (!mysql_query($sql))
    {
    die('Error: ' . mysql_error());
    } else{
    echo 'Great Success!';
    }




    mysql_close();
    ?>

    Thanks for the help and I am just doing my best with having the education for this and just doing it.
    *I am new to software programming, and everything I know is by reading material online and just trying to do it*

  2. #2
    Join Date
    Mar 2012
    Posts
    2

    I found what I was looking for

    I di some research and found this book:
    Build Your Own Database-Driven Website Using PHP & MySQL, 4th Edition

    and as I read through this book i did find the answer I was looking for:
    header('Location: .');

    I put this line of code at the bottom of my php file and it works perfectly, it looks like this:

    header('Location: .');
    mysql_close();
    ?>

  3. #3
    Join Date
    Apr 2012
    Location
    karachi/Pakistan
    Posts
    2
    Hi ,
    In the my opinion, the PHP is a very easy steps to connect the database. It has the only one steps to connect the database.
    Syntax-
    <?php
    mysql_connect("server name", "username", "password") or die(mysql_error());
    echo "Connected to MySQL<br />";
    ?>
    After connecting the DSN , we use this line.
    Thanking you .

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
  •