Jump to content

PHP MySQL Search


hobbiton73

Recommended Posts

I wonder whether someone may be able to help me please.

 

I'm trying to put together php code and an html form that allows an administrator to search for a user via the email address, retrieving the 'first' and 'surname' from the pertinent record in a mySQL database.

 

What I would like to happen is for the retrieved 'first' and surname' values to be automatically populated into the 'first' and 'surname' text boxes on the same search form.

 

I can perform the search and retrieval of the correct record without any problem, but I can't work out how to populate the 'first' and 'surname' values into my search form. I am quote a novice with PHP so apologies if this is a really daft question, but I just windered whether somone could take a look at my code below and let me know where I've gone wrong?

 

Many thanks

 

PHP CODE

<?php 
require("phpfile.php"); 

// Opens a connection to a MySQL server 

$connection=mysql_connect ("hostname", $username, $password); 
if (!$connection) { die('Not connected : ' . mysql_error());} 

// Set the active MySQL database 

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


$email = $_POST['email']; 
$result = mysql_query("SELECT * FROM userdetails WHERE emailaddress like '%$email%'"); 

while($row = mysql_fetch_array($result)) 
{ 
echo $row['forename']; 
echo $row['surname']; 
echo "<br />"; 
} 
?> 

 

HTML FORM

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Maptitle>
        <script src="js/gen_validatorv4.js" type="text/javascript"></script>
    </head>
    <h1><span class="blue">Sign Up</span> For Map My Finds</h1>
    <form name="userpasswordreset" id="userpasswordreset" method="post" action="search.php">
        <h2>Your Details </h2>
    </div>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td width="26%" height="25"><strong>Email Address</strong></td>
                <td width="4%"> </td>
                <td width="70%"><input name="email" type="email" id="email" size="50" /></td>
            </tr>
            <tr>
                <td height="25"><strong>Confirm Email</strong></td>
                <td> </td>
                <td><input name="conf_email" type="email" id="conf_email" size="50" /></td>
            </tr>
            
            <tr>
                <td height="25"><label>
                    <input type="submit" name="Submit" value="Search" />
                    </label></td>
                <td> </td>
                <td> </td>
            </tr>
            <tr>
                <td height="25"><strong> First Name </strong></td>
                <td> </td>
                <td><input name="fname" type="text" id="fname" size="30" value="<?php echo $forename; ?>" /></td>
            </tr>
            <tr>
                <td height="25"><strong> Address Last Name </strong></td>
                <td> </td>
                <td><input name="lname" type="text" id="lname" size="30"  value="<?php echo $surname; ?>" /></td>
            </tr>
            <tr>
                <td height="25"> </td>
                <td> </td>
                <td> </td>
            </tr>
            <tr>
                <td height="25"> </td>
                <td> </td>
                <td> </td>
            </tr>
            <tr>
                <td height="25"><strong>Password</strong></td>
                <td> </td>
                <td><input name="pass" type="password" id="pass" size="30" /></td>
            </tr>
            <tr>
                <td height="25"><strong>Confirm Password </strong></td>
                <td> </td>
                <td><input name="conf_pass" type="password" id="conf_pass" size="30" /></td>
            </tr>
            <tr>
                <td height="25"> </td>
                <td> </td>
                <td> </td>
            </tr>
            <tr>
                <td height="25"><strong>Password Hint </strong></td>
                <td> </td>
                <td><input name="hint" type="text" id="hint" size="30" /></td>
            </tr>
            <tr>
                <td height="25"> </td>
                <td> </td>
                <td> </td>
            </tr>
            <tr>
                <td height="25"> </td>
                <td> </td>
                <td> </td>
            </tr>
        </table>
    </form>
    <script language="JavaScript" type="text/javascript">
        // Code for validating the form
        // Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
        // for details
        var frmvalidator = new Validator("userpasswordreset");
        frmvalidator.addValidation("email","req","Please provide your email address"); 
        frmvalidator.addValidation("email","email","Please enter a valid email address");
        frmvalidator.addValidation("conf_email","eqelmnt=email", "The confirmed email address is not the same as the email address"); 
    </body>
    </html>

Link to comment
Share on other sites

ok, you shouldn't be using SELECT * for that query, you only need to SELECT forename, surname

 

You also need to assign forename and surname to to your variables $forename and $surname.  As well as this, unless the form and the query are on the same page you will need to move the values accross the pages before you assign them. Are these on the same page or two different ones?

Link to comment
Share on other sites

To be honest, it's better to work how you feel comfortable at the moment.  If you're happy using two pages, then use two pages, if you preffer one, then use one.  Once you're more settled in the language you can worry about which is better long term. 

 

Still need you to tell us what setup you are using just now so we can help with the code :P.

Link to comment
Share on other sites

Hi, many thanks for replying to my post.

 

I'm quite happy to use one page, so I've amalgamted the two and posted the revised code below.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <?php 
        require("phpfile.php"); 
        
        // Opens a connection to a MySQL server 
        
        $connection=mysql_connect ("hostname", $username, $password); 
        if (!$connection) { die('Not connected : ' . mysql_error());} 
        
        // Set the active MySQL database 
        
        $db_selected = mysql_select_db($database, $connection); 
        if (!$db_selected) { 
        die ('Can\'t use db : ' . mysql_error()); 
        } 
        
        
        $email = $_POST['email']; 
        $result = mysql_query("SELECT forename, surname FROM userdetails WHERE emailaddress like '%$email%'"); 
        
        while($row = mysql_fetch_array($result)) 
        { 
        echo $row['forename']; 
        echo $row['surname']; 
        echo "<br />"; 
        } 
        ?> 
        
        <title>Maptitle>
        <script src="js/gen_validatorv4.js" type="text/javascript"></script>
    </head>
    <form name="userpasswordreset" id="userpasswordreset" method="post">
        <h2>User Details </h2>
    </div>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td width="26%" height="25"><strong>Email Address</strong></td>
                <td width="4%"> </td>
                <td width="70%"><input name="email" type="email" id="email" size="50" /></td>
            </tr>
            <tr>
                <td height="25"><strong>Confirm Email</strong></td>
                <td> </td>
                <td><input name="conf_email" type="email" id="conf_email" size="50" /></td>
            </tr>
            
            <tr>
                <td height="25"><label>
                    <input type="submit" name="Submit" value="Search" />
                    </label></td>
                <td> </td>
                <td> </td>
            </tr>
            <tr>
                <td height="25"><strong> First Name </strong></td>
                <td> </td>
                <td><input name="fname" type="text" id="fname" size="30" value="<?php echo $row['forename']; ?>" /></td>
            </tr>
            <tr>
                <td height="25"><strong> Address Last Name </strong></td>
                <td> </td>
                <td><input name="lname" type="text" id="lname" size="30" value="<?php echo $row['surname']; ?>" /></td>
            </tr>
            <tr>
                <td height="25"> </td>
                <td> </td>
                <td> </td>
            </tr>
            <tr>
                <td height="25"> </td>
                <td> </td>
                <td> </td>
            </tr>
            <tr>
                <td height="25"><strong>Password</strong></td>
                <td> </td>
                <td><input name="pass" type="password" id="pass" size="30" /></td>
            </tr>
            <tr>
                <td height="25"><strong>Confirm Password </strong></td>
                <td> </td>
                <td><input name="conf_pass" type="password" id="conf_pass" size="30" /></td>
            </tr>
            <tr>
                <td height="25"> </td>
                <td> </td>
                <td> </td>
            </tr>
            <tr>
                <td height="25"><strong>Password Hint </strong></td>
                <td> </td>
                <td><input name="hint" type="text" id="hint" size="30" /></td>
            </tr>
            <tr>
                <td height="25"> </td>
                <td> </td>
                <td> </td>
            </tr>
            <tr>
                <td height="25"> </td>
                <td> </td>
                <td> </td>
            </tr>
        </table>
    </form>
    <script language="JavaScript" type="text/javascript">
        // Code for validating the form
        // Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
        // for details
        var frmvalidator = new Validator("userpasswordreset");
        frmvalidator.addValidation("email","req","Please provide your email address"); 
        frmvalidator.addValidation("email","email","Please enter a valid email address");
        frmvalidator.addValidation("conf_email","eqelmnt=email", "The confirmed email address is not the same as the email address"); 
    </body>
    </html>

 

In terms of my setup, I'm not quite sure what you mean, so I hope I've not giving you a load of useless information. My server uses PHP5, my operating system is Windows XP and I'm using Dreamweaver to put my code together.

 

I hope this helps, sincere apologies if it isn't of help.

 

Kind regards

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.