SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question New to PHP. Help please!

    Hello I am new to PHP and I am reading the book Build you own database driven website using PHP and MySQL. I am trying something on my own as an exercise from the book.

    I sucessfully created a dropdown menu (using the <select><option> tags). I am pleased to see that it worked well. Now all I want is to display another field from the same record selected by the user from the list. I am sure it's simple.

    So the idea is that I have 20 bible verses in my db made of three fields (an ID, a reference and the actual verse). For example ID=10, Ref=John 3:16, Verse=For God so loved the world ... life.

    My code shows the Select box and a Show button below. When clicked I would like the complete verse to appear a few lines below the list. Choosing another verse would refresh the page and display only that other one.

    I am a VBA programmer so I can understand quickly but this is just another planet!! Here is the sample code (I removed the unnecessary XHTLM stuff)

    <body>
    <p>Select a verse from the drop down list to read it.</p>
    <?php
    $dbcnn=mysql_connect('localhost','root','mypassword');
    if (!$dbcnn) {
    exit('<p>Unable to connect to database server</p>');
    }

    if (!mysql_select_db('b_verses')) {
    exit('<p>Unable to locate Bible Verses database.</p>');
    }

    $result=mysql_query('SELECT verse_id, verse_ref FROM tblverses ORDER BY verse_ref');
    if (!$result) {
    exit('<p>Error performing query ' . mysql_error() . '.</p>');
    } else {
    echo "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">";

    echo '<select name="verses">';
    while($row = mysql_fetch_array($result)){
    echo '<option value="' . $row['verse_id'] . '"> ' .
    $row['verse_ref'] . '</option>';
    } // while $row
    echo '</select><br />';

    echo '<input type="submit" value="Show"';
    echo '<input type="hidden" value="submitted"';
    echo '</form><p></p>';
    } // end if $result
    ?>
    </body>

    Thanks so much for anyone who wants to help me with this.

    daniel

  2. #2
    SitePoint Wizard bronze trophy bigalreturns's Avatar
    Join Date
    Mar 2006
    Location
    The Wirral, England
    Posts
    1,293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK so you're form, when submitted will reload the page, sending the form variables to the PHP script in the form of the $_POST array. You need to read the value of verse_id from the array, and then use that value to select the corresponding verse from the database. Something like...

    PHP Code:
    $verse_id=mysql_real_escape_string($_POST['verse_id']);
    $result=mysql_query("SELECT verse FROM tblverses WHERE verse_id='$verse_id'"); 
    The mysql_real_escape_string part is just there to stop users from putting code into the input, so protecting you from sql injection attacks.
    Last edited by bigalreturns; Jun 22, 2007 at 13:57.
    "The proper function of man is to live - not to exist."
    Get a Free TomTom


  3. #3
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks alot bigalreturns,

    In fact I knew that at some point I would need to add code like the one you suggested except for the mysql_real_escape_string part. Thanks for that.

    It is just where to put this code that eludes me totally.

    For instance I also figure out that I will need to type something like:

    echo '<p>The verse is: <br />' . $result . '.</p>'

    Many script of the sort that I am trying to do have something like the following at the top:

    if (isset($_POST['submitted'])) {
    // Do something here
    } else {
    // Do that instead
    }

    Wouldn't that be of any use in this case? If yes I am not sure how to use that approach.

    Thanks for your reply bigalreturns

    Daniel

  4. #4
    SitePoint Wizard bronze trophy bigalreturns's Avatar
    Join Date
    Mar 2006
    Location
    The Wirral, England
    Posts
    1,293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // end if $result
    // put your verse fetching code in here
    ?>
    </body> 
    You're right about testing that $_POST['submitted'] is set before querying the database etc, the full code might look like this:

    PHP Code:
    IF (isset($_POST['submitted'])) {
    $verse_id=mysql_real_escape_string($_POST['verse_id']);
    $result=mysql_query("SELECT verse FROM tblverses WHERE verse_id='$verse_id'");
    $row=mysql_fetch_assoc($result);
    echo 
    '<p>The verse is ' $row['verse'] . '</p>';

    "The proper function of man is to live - not to exist."
    Get a Free TomTom


  5. #5
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much bigalreturns,

    Your post helped me to complete my little project and it worked perfectly.

    Also I have learned about mysql_real_escape_string and mysql_fetch_assoc. So I will be able to tackle more interesting projects soon.

    Thanks alot again for your help.

    Daniel L.


Bookmarks

Posting Permissions

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