why doesn't my code work? create a form to query database

This forum is for general questions about extensions for Joomla! version 1.5.x.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Locked
cecilfang
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Sat May 19, 2012 2:57 am

why doesn't my code work? create a form to query database

Post by cecilfang » Sat May 19, 2012 1:21 pm

I have created a table in joomla database with product name und product id.

Now I want to creat an easy form for customer such that
enter a product name and then display the product id.

Code: Select all

<?php
define ('HOSTNAME', 'localhost'); 
define ('USERNAME', 'root'); 
define ('PASSWORD', 'root_password');
define ('DATABASE_NAME', 'joomla_15'); 

function query_db($qstring) {
$db = mysql_connect(HOSTNAME, USERNAME, PASSWORD) or
         die (mysql_error());
mysql_select_db(DATABASE_NAME);

$query = " SELECT * FROM product WHERE name IS "$qstring" ";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
print $row[0];
}
mysql_free_result($result);
mysql_close();
}
?>

<html>
<head>
<title>Building a Form</title>
</head>
<body>
<?php 
$search = $_GET["search"];
$self = $_SERVER['PHP_SELF'];

if ($search !=NULL) {
print "The search string is: <strong>$search</strong>.";
query_db($search);	
}
else {
print ('
         <form action="'.$self.'" method="GET">
         <label>Search:
         <input type="text" name="search" id="search" />
         </label>
         <input type="submit" value="Search Now!" />
         </form>
       ');
}
?>
</body>
</html>

And I saved this code in a .php file, and used Soucerer to embed in a article.

After click "Search Now!" I got always the frontpage of joomla.

Can anyone give me some hints?
Thx!


Or is there another easy way to bulid such a form with any extension?
I'm newbie for joomla.
Thanks for your help!

User avatar
euoceo
Joomla! Guru
Joomla! Guru
Posts: 957
Joined: Fri Sep 12, 2008 2:48 pm
Location: Sacramento
Contact:

Re: why doesn't my code work? create a form to query databas

Post by euoceo » Sat May 19, 2012 4:05 pm

Somthing to try, change your form action to:

Code: Select all

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="get">

I think your php code with the "'. is not correct, try the above and see if it works like that.
Joomla! Web Hosting, Design, and Consulting.
-Bob


Locked

Return to “Extensions for Joomla! 1.5”