Jump to content

What's wrong with this code


harlequeen

Recommended Posts

Hi

 

I am trying to teach myself php but am getting bogged down. I have some code which populates a dropdown box and I want to then pass the selected item so that the database record is recalled to the page.  Ultimately, I would like this to appear on the same page, underneath the dropdown box. 

 

I can't seem to get it to work though. I'm sure its a simple thing, but I can't do it.

 

Here's my code from the sending page..

 

$query="SELECT title FROM recipes";
$result = mysql_query ($query);

echo "<form action=dropdrip3.php method=get>
<select title=Title value=''>Title</option>";
// printing the list box select command
while($dropdown=mysql_fetch_array($result)){//Array or records stored in $dropdown
echo "<option value=$dropdown>$dropdown[title]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box 

?>
<input type="submit" name="Submit" value="Submit" /> 

 

and here's my code from the receiving page.

 

$dropdown=$_GET['title'];

$sql=mysql_query("SELECT * FROM recipes WHERE title='$title'");

while(@$row=mysql_fetch_array($sql)){
//Build formatted result here

echo("<h4>");
echo $row['title']."<br/>";
echo $row['ingredients']."<br/>";
echo $row['method']."<br/></h4>";

}

 

My grasp of php is very tenuous, so if someone could help I would be grateful

 

Thanks

 

Harlequeen

Link to comment
Share on other sites

No, that's fairly easy - detect based on if the title is present or not in the adress bar:


<?php

if(!empty($_GET['title'])){ // title is present - do search

$dropdown = htmlspecialchars($_GET['title'],ENT_QUOTES);
$sql = mysql_query("SELECT * FROM recipes WHERE title='$title'");

while($row = mysql_fetch_array($sql)){
echo <<<_HTML
  <h4>
   {$row['title']}<br />
   {$row['ingredients']}<br />
   {$row['method']}<br />
  </h4>
_HTML
}
}
else // title is absent or empty - show dropdown
{
echo <<<_HTML
<form action="this.php" method="get">
<select name="title">
_HTML;

$query = mysql_query("SELECT title FROM recipes");

while($dropdown = mysql_fetch_array($query)){
  echo "<option value=\"".$dropdown['title']."\">".$dropdown['title']."</option>";
}

echo <<<_HTML
</select>
<input type="submit" name="Submit" value="Submit" />
_HTML;
}

?>

Should do it...

Link to comment
Share on other sites

I don't think I've got the hang of this....

 

I tried combining the code above with my code, but I seem to have got it all wrong.  Here is my code, can someone tell me what I need to correct.  Apologies if its bad coding but I'm trying to learn.

 

All I get is an empty dropdown box and the submit button.  I'm confused.

 

Thanks

 

<link rel="stylesheet" type="text/css" href="apple.css">
<?php
//database connection
include'myconnect.php';

if(!empty($_GET['title']))      //if title is present do search
{

$dropdown=htmlspecialchars($_GET['title'], ENT_QUOTES);
$sql=mysql_query("SELECT * FROM recipes WHERE title='$title'");
while(@$row=mysql_fetch_array($sql))

//Build formatted result here

echo("<h4>");
echo $row['title']."<br/>";
echo $row['ingredients']."<br/>";
echo $row['method']."<br/></h4>";
//  This is here to remind me to add a link to the title for the recipe.
//echo "<a href=$url>$article_image</a>";
}


else   // title is absent or empty - show dropdown
{

echo "<form action=dropdrip3.php method=get>

<select name='title'>Title</option>";

// printing the list box select command
while($dropdown=mysql_fetch_array($result))
{


//Array or records stored in $dropdown
//echo "<option value=$dropdown>$dropdown[title]</option>";
echo "<option value=\"".$dropdown['title']."\">".$dropdown['title']."</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box 
}
?>
<input type="submit" name="Submit" value="Submit" /> 
</body>
</html>

Link to comment
Share on other sites

I cant see what you are using the $_GET variable for??

 

I would do this:

 


<link rel="stylesheet" type="text/css" href="apple.css">
<?php
//database connection
include'myconnect.php';

if(!empty($_GET['title']))      //if title is present do search
{

$dropdown=htmlspecialchars($_GET['title'], ENT_QUOTES);
$sql=mysql_query("SELECT * FROM recipes WHERE title='$title'");
while(@$row=mysql_fetch_array($sql))

//Build formatted result here

echo("<h4>");
echo $row['title']."<br/>";
echo $row['ingredients']."<br/>";
echo $row['method']."<br/></h4>";
//  This is here to remind me to add a link to the title for the recipe.
//echo "<a href=$url>$article_image</a>";
}


else   // title is absent or empty - show dropdown
{

echo "<form action=dropdrip3.php method=get>

<select name='title'>Title</option>";

// printing the list box select command
while($row2=mysql_fetch_array($result))
{


//Array or records stored in $dropdown
//echo "<option value=$row2>$row2</option>";
echo "<option value=\"".row2."\">".$row2."</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box 
}
?>
<input type="submit" name="Submit" value="Submit" /> 
</body>
</html>

 

I made chances in your last while loop...

Link to comment
Share on other sites

Well your first mistake is this:

 

include'myconnect.php';

 

It needs to be:

 

include('myconnect.php');

 

That could be why its not showing anything...you aren't connecting to your database to start with. :)

 

The empty dropdown box says you aren't connecting to your database. The smallest thing could mess up the whole script and cause it not to load at all.

 

My pages haven't even loaded because of this:

 

echo "<form action=dropdrip3.php method=get>

 

It works when i change it to this:

 

echo "<form action=\"dropdrip3.php\" method=\"get\">";

 

You are missing "echo", ";", etc.

 

If you write HTML with PHP you have to use echo " "; :)

Link to comment
Share on other sites

Try this:

 

<link rel="stylesheet" type="text/css" href="apple.css">
<?php
//database connection
include('myconnect.php');

if(isset($_GET['title']))      //if title is present do search
{

$dropdown=htmlspecialchars($_GET['title'], ENT_QUOTES);
$sql=mysql_query("SELECT * FROM recipes WHERE title='$title'");
while(@$row=mysql_fetch_array($sql))

//Build formatted result here

echo("<h4>");
echo $row['title']."<br/>";
echo $row['ingredients']."<br/>";
echo $row['method']."<br/></h4>";
//  This is here to remind me to add a link to the title for the recipe.
//echo "<a href=\"$url/$article_image\"></a>";
}


else   // title is absent or empty - show dropdown
{

echo "<form action=\"dropdrip3.php\" method=\"get\">";

echo "<select name=\"title\">Title</option>";

// ( or if you meant $title then echo "<select name=\"" .$title . "\">Title</option>"; )

// printing the list box select command
while($dropdown=mysql_fetch_array($result))
{


//Array or records stored in $dropdown
//echo "<option value=$dropdown>$dropdown[title]</option>";
echo "<option value=\"".$dropdown['title']."\">".$dropdown['title']."</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box 
}
?>
<input type="submit" name="Submit" value="Submit" /> 
</body>
</html>

Link to comment
Share on other sites

Hi everyone and thanks for the help.

 

My include statement works just as it was, as I am using it in other pages and can connect correctly and get output.  Also, my connect file includes the usual failure statements which leads me to believe that I am connecting OK.

 

JJohnsenDK, I tried your code and it didn't work, neither did the code from Archadian.  I have a page (http://www.slim-with-me.co.uk/dropdrip.php) which gets the info from the database, populates the dropdown box and allows me to select an item and then passes the item to the receiving page for output of the record from the database.  That works ok.  But I also want to be able to have the box appear on the receiving page so that if someone wants another selection they don't have to go back to the originating page. 

 

As I said before, I seem to get the box, but its empty, or I don't get anything at all.  I am not proficient at php and can't seem to get what I want.

 

Any further help would be appreciated.

 

Harlequeen

 

Link to comment
Share on other sites

You can either copy the code onto the recieving page that you want or use a variable or function for the dropdown box itself. Don't feel bad harlequeen, i still have alot to learn myself. I use the Php Bible 2nd Edition you can get from any book store. Anyways, i believe the function would look like this:

 

<?php

function dropdown() {

echo "<form action=\"dropdrip3.php\" method=\"get\">";

echo "<select name=\"title\">Title</option>";

while($dropdown=mysql_fetch_array($result)) {

//echo "<option value=$dropdown>$dropdown[title]</option>";
echo "<option value=\"".$dropdown['title']."\">".$dropdown['title']."</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box 
}
?>
<input type="submit" name="Submit" value="Submit" /> 


 

Then you just call the function "dropdown()" or whatever you name it whenever you want it to show up on one of your pages.

 

Please anyone correct me if im wrong about this code but from what i read this is how you do it. Hopefully this helps and works. :)

 

$result = mysql_query("SELECT id, sess, admsess, levelad FROM users WHERE sess = '$cookie'", $db) or die(mysql_error());
$row = mysql_fetch_array($result);

 

For some reason i have to add the $db at the end of the () in the mysql_query or it doesnt  work for me...$db being the DB i use for the info for my website.

Link to comment
Share on other sites

Sorry, Archadian.  Thats just confused me even more.  I can't get it to work even though it seems that it should be a simple matter.

 

I thought it would be easier to add to code that I know is working, check for the variable being present then if it is do the working code, if its not, do something else that I know was working, but that didn't do it.

 

Cheers

 

Harlequeen

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.