The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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>');
}

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.