#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Thumbs up PHP connection to Database


    Hi,

    My name is Aaron Boyse and I am having trouble linking a Database and HTML form using PHP.

    I am new to PHP so, would love some help on the subject.

    It would be much appreciated.

    Thank you
    Last edited by aaronboyse1990; December 21st, 2012 at 06:09 AM. Reason: Spelling error
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    what kind of help are you seeking? We obviously can't help you with your code if you don't post it.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Thumbs up


    <?php

    //connection to the database
    $C:/Users/Desktop/Aaron/1916 Prisoners = mysql_connect($hostname, $username, $password)
    or die("Unable to connect to MySQL");
    echo "Connected to MySQL<br>";

    //select a database to work with
    $selected = mysql_select_db("Table",$C:/Users/Desktop/Aaron/1916 Prisoners)
    or die("Could not select Table");

    //execute the SQL query and return records
    $result = mysql_query("SELECT Surname, First Name, Address, Date FROM C:/Users/Desktop/Aaron/1916 Prisoners");

    //fetch tha data from the database
    while ($row = mysql_fetch_array($result)) {
    echo "Surname:".$row{'Surname'}." Name:".$row{'First Name'}."Address: ". //display the results
    $row{'Address'}."<br>";
    }
    //close the connection
    mysql_close($dbhandle);
    ?>

    This is the code but it wont connect.

    and this is the code to the html form

    <html>
    <div align="right"><script language="javascript">
    <!--
    today = new Date();
    document.write("<BR>The time now is: ", today.getHours(),":",today.getMinutes());
    document.write("<BR>The date is: ", today.getDate(),"/",today.getMonth()+1,"/",today.getYear());
    //-->
    </script> </div>

    <img src="C:/Users/Castle Museum/Desktop/Aaron/Banner-Knights-and-Rebels-v01.png" alt="Knights and Rebels Logo" width="400">
    <br><br>
    <br>
    <a href="C:/Users/Castle Museum/Desktop/Aaron/homepage.html">Home</a>&nbsp;<a href=C:/Users/Castle Museum/Desktop/Aaron/employee upload.html">Employee Upload</a>
    <br>
    <hr align="center" size="4" color="black"</hr>
    <h2>Search</h2>
    <p><i>Ever wanted to know if any of your Ancestors were part of 1798, 1916 or 1922? Well now you can, with Enniscorthy Castle's new Database Search Engine</i></p>
    <br>
    <form>
    First name: <input type="text" name="firstname"><br>
    Surname: <input type="text" name="surname"><br>
    </form>
    <form>
    Gender: <input type="radio" name="sex" value="male">Male<input type="radio" name="sex" value=female>Female

    </form>

    <select>
    <option>1798 Those Involved</option>
    <option>1916 Prisoners List</option>
    <option>1922 Prisoners List</option>
    </select>
    <br>
    <br>
    Town <input type="text" name="town"><br>
    <select name="county" id="county"><option>Antrim</option><option>Armagh</option><option>Carlow</option><option>Cavan</option><option>Clare</option><option>Cork</option><option>Donegal</option><option>Down</option><option>Dublin</option><option>Fermanagh</option><option>Galway</option><option>Kerry</option><option>Kildare</option><option>Kilkenny</option><option>King&#039;s Co.</option><option>Leitrim</option><option>Limerick</option><option>Londonderry</option><option>Longford</option><option>Louth</option><option>Mayo</option><option>Meath</option><option>Monaghan</option><option>Queen&#039;s Co.</option><option>Roscommon</option><option>Sligo</option><option>Tipperary</option><option>Tyrone</option><option>Waterford</option><option>Westmeath</option><option>Wexford</option><option>Wicklow</option><option value="" selected="selected">All Counties</option></select></p><p><label for="address">Townland/street</label><input name="townland" id="address" type="text"/></p><p class="submit">
    <br>
    <br>
    <form>
    <input value="1" name="exact" id="match" type="checkbox"/><label class="option" for="match">Exact matches only</label></p>
    <br>
    <input type="button" name="submit" value="Submit">
    </form>
    <br>
    <br>
    <hr align="center" size="4" color="black"</hr>
    <br>
    <br>
    <img src="C:/Users/Castle Museum/Desktop/Aaron/Vinegar Hill logo.png" alt"Vinegar Hill Logo"><img src="C:/Users/Castle Museum/Desktop/Aaron/1798 logo.png" alt"1798 Centre Logo"><img src="C:/Users/Castle Museum/Desktop/Aaron/castle logo.png" alt="Enniscorthy Castle Logo" width="425" align="right">
    </html>
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Please wrap your code in [ PHP ] tags next time to make it readable.

    What is this "$C:/Users/Desktop/Aaron/1916 Prisoners"?? That's not even close to a valid PHP variable, and I can't think of any reason why you would use Windows paths as variable names.

    Also note that the "mysql_" functions are obsolete and will die out sooner or later, even though they're still being used in bad tutorials. PHP has several new database extensions. In your case, you can either use the MySQLi extension or the PDO interface, which works with many database systems (not just MySQL).

    If you choose PDO, it works like this:
    PHP Code:
    <?php

    // set data for connection
    define('DB_CONNECTION''mysql:host=127.0.0.1;dbname=your_db');
    define('DB_USER''your_user');
    define('DB_PASS''your_password');

    try {
        
    $database = new PDO(DB_CONNECTIONDB_USERDB_PASS);
    } catch (
    PDOException $e) {
        
    // leave that out when putting the page online; internal errors are not supposed to be seen by users
        
    echo 'Could not connect to database';
    }

    // a simple query without parameters (the query won't actually work, it's just random SQL)
    $user_query $database->query('
        SELECT
            user_id
            , name
            , last_login
        FROM
            users
        LIMIT 10
    '
    );
    if (
    $user_query) {
        
    // loop through the rows
        
    foreach ($user_query as $user) {
            
    var_dump($user);
        }
    } else {
        
    // leave that out when putting the page online; internal errors are not supposed to be seen by users
        
    echo 'first user query failed';
    }

    // a query with parameters (using a prepared statement)
    $user_stmt $database->prepare('
        SELECT
            name
            , last_login
        FROM
            users
        WHERE
            user_id = :user_id
    '
    );
    // pass value to :user_id parameter and execute statement
    $user_retval $user_stmt->execute(array(
        
    'user_id' => 1
    ));
    if (
    $user_retval) {
        foreach (
    $user_stmt as $user) {
            
    var_dump($user);
        }
    } else {
        
    // leave that out when putting the page online; internal errors are not supposed to be seen by users
        
    echo 'second user query failed';
    }
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    Will that work do you know or will I have to change a couple of variables...
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    You should rewrite your whole database code. Use the above example code as a template.

    It's a bit difficult to help you with your own code, because some things I simply don't understand (like the ever-present "C:/Users/Desktop/Aaron/1916 Prisoners"). May I ask which resource you used to learn PHP (or copy code from)? Because whatever tutorial or book you were reading, you should throw it away.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    I wasn't using a book, this is my first try at PHP.
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Yes, but what did you use for learning? Where do you have the PHP functions and code patterns from?

    Because a lot of what you're using in your PHP code, the HTML markup and the JavaScript is either long obsolete or considered bad. To put it more bluntly (I hope you don't mind): This code is from the 90s, not the year 2012.

    So I'm suggesting that you should use a difference resource for learning. Either a good book or good tutorials like Quakenet/#php Tutorial.

IMN logo majestic logo threadwatch logo seochat tools logo