Jump to content

trying to code search based on user input but only includes are displaying


cosmo

Recommended Posts

Hi all

i'm still learning php /mysql and am messing around with queries and forms and i can search a database with

 

  require ('./includes/dbconfig.php');

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
mysql_select_db($dbname);
$query  = "SELECT  pm_name, recreation FROM `pm_recreation` where pm_name like 'm%' LIMIT 0, 30 ";
$result = mysql_query($query)or die (mysql_error());

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    
echo "Name :{$row['pm_name']} <br>" .
         "Recreation : {$row['recreation']} <br><br>" ;

}

and can user php/html to create form validate it

 

<form action="search.php" method="post">
    <p>Name: <input type="text" name="name" size="20"
        maxlength="40" /></p>
   
    <p><input type="submit" name="submit" value="Submit" /></p>
    <input type="hidden" name="submitted" value="TRUE" />
</form>

 

but when i try validating user input and using it in a where clause to show data

 

if (isset($_POST['submitted'])) {

    $errors = array(); // Initialize error array.
$search =$_POST['name'];
}
    // Check for a name.
    if (empty($_POST['name'])) {
        $errors[] = 'You forgot to enter your name.';
    }

    if (empty($errors)) { 

(i have the sql query part of the code here)

 

all that happens is the includes display and thats it, no error nothing. after the query i have an else display form code then i end the else

 

sorry to be long-winded but i just tried to give as much detail as possible

 

thank you

 

 

Sorry I can't figure out what you are trying to say, can we see the part that you seem to have left out? the most important part.. where you said: "(i have the sql query part of the code here)"

Also, use the [ php][/php ] bbcode instead of [ code][/code ] so it's nicely highlighted :D

(remember to have <?php and ?> in it or it wont be highlighted - also, I had to put spaces to break the bbcode I entered above ^)

I will track this topic and return to help you soon

Hi Bozebo

 

sorry bout leaving that code out .. i have posted the entire code

 

thank you for your help

 

<?php # Search.php
$page_title = 'Search';
include ('./includes/head.html');

if (isset($_POST['submitted'])) {
$search = $_POST['name'];
$errors = array(); // Initialize error array.

// if I put a } here ...the result is only the includes get processed

if (empty($_POST['name'])) {
$errors[] = "You forgot to enter Prime Minister's name.";
}

if (empty($errors)) {
/*connect to mysql */
require ('./includes/dbconfig.php');
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');



mysql_select_db($dbname);
$query = "SELECT pm_name, recreation FROM pm_recreation where pm_name like '%'".trim(mysql_real_escape_string($search)) . "%' LIMIT 30 OFFSET 0";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{

echo "Name :{$row['pm_name']} <br>" .
"Recreation : {$row['recreation']} <br><br>" ;


} else {

echo '<h1>Error!</h1>
<p class="error">The following error(s) occurred:<br />';
foreach ($errors as $msg) { // Print each error.
echo " - $msg<br />\n";
}
echo '</p><p>Please <a href ="contact.php"> go back and try again.</a></p><p><br /></p>';

} /

} else { ?>
<h2>Search Prime Minister's Recreations</h2>
<form action="mysearch.php" method="POST">
<p>Prime Minister's Name: <input type="text" name="name" size="20" maxlength="40" /></p>
<p><input type="submit" name="submit" value="Find" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
}
include ('./includes/foot.html');
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.