Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get and show info from mysql, problems..... 1

Status
Not open for further replies.

benluke4

Technical User
Jan 27, 2005
127
GB
Hi, this is my first attempt at this but im running into problems

heres the code im using
Code:
<?php
include '../admin/db.php';
?>
<html>
<head><title>test</title></head>
<body>
<?php

/* grab the POST variable */
$category = $_POST['category'];

$location = 'london_se';

$query = "SELECT * FROM directory WHERE category = '$category' AND location = '$location' ";
$result = mysql_query($query)
or die ("Couldn't execute query.");

echo "<table>\n";

while ($row = mysql_fetch_assoc($result)) {

   echo "<tr>
            <td> $row['location'] </td>
            <td> $row['category'] </td>
            <td> $row['company'] </td>
           <tr>\n";
}
echo "</table>\n";
?>
</body>
</html>

im keep getting this error

Code:
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/godonovan/public_html/weightloss/locations/test.php on line 23

any advice is much appreciated

Thanks

Ben
 
Code:
   echo "<tr>
            <td> $row['location'] </td>
            <td> $row['category'] </td>
            <td> $row['company'] </td>
           <tr>\n";

I could be wrong, but I don't think you can have multi line strings, and even if you can, I haven't had much luck getting arrays to work without taking them out of the quotes.

Code:
   echo "<tr><td>" . $row['location'] .
        "</td><td>" . $row['category'] . 
        "</td><td>" . $row['company'] . 
        "</td><tr>\n";
 
thanks ericbrunson ,

i replaced the code with your code the error does not appear anymore but it just shows a blank screen

any ideas

Thanks

Ben
 
i doubt whether this is the issue but the last <tr> should be a </tr>

the more likely problem is that you are not returning any info from the db. test this by viewing the output of print_r($row).

 
yea your right nothing is being outputted from the db.

ummmmmmmmmmmm.

ok if i show you what im trying to do maybe that will help.

This is my db table

this is the form the users are filling in
Code:
<form action="test.php" method="post" onsubmit="return check(this)">
		  <table width="96%" border="0" align="left" cellpadding="0" cellspacing="0">
            <tr> 
            <td width="31%"> <p class="body">Search for an expert in the field 
                  of </p></td>
            <td width="30%"><select name="catagory" class="FormField">
                <option value="00" <?=$categorySelect == "" ? "selected"  : "" ?> selected>Select 
                a category 
                <option value="allergy_testing" <?=$categorySelect == "allergy_testing" ? "selected"  : "" ?> >Allergy 
                Testing 
                <option value="exercise" <?=$categorySelect == "exercise" ? "selected"  : "" ?> >Exercise 
                <option value="homeopath" <?=$categorySelect == "homeopath" ? "selected"  : "" ?> >Homeopath 
                <option value="hypnotherapist" <?=$categorySelect == "hypnotherapist" ? "selected"  : "" ?> >Hypnotherapist 
                <option value="image_consultant" <?=$categorySelect == "image_consultant" ? "selected"  : "" ?> >Image 
                Consultant 
                <option value="nutrition" <?=$categorySelect == "nutrition" ? "selected"  : "" ?> >Nutrition 
                <option value="pilates" <?=$categorySelect == "pilates" ? "selected"  : "" ?> >Pilates 
                <option value="reflexologist" <?=$categorySelect == "reflexologist" ? "selected"  : "" ?> >Reflexologist 
                <option value="relaxation" <?=$categorySelect == "relaxation" ? "selected"  : "" ?> >Relaxation 
                <option value="stress_management" <?=$categorySelect == "stress_management" ? "selected"  : "" ?> >Stress 
                Management 
                <option value="yoga" <?=$categorySelect == "yoga" ? "selected"  : "" ?> >Yoga 
                <option value="other" <?=$categorySelect == "other" ? "selected"  : "" ?> >Other 
                - Reiki, Feng Shei etc </select></td>
            <td width="39%"><input type="submit" name="add" value="Search"></td>
          </tr>
        </table>
		</form>


from the table i want to output the contact details from the
$location which in this case is londen_se and the chosen category from the user.

So it will show say all "image consultants" in london_se

I hope ive explained myself properly.

sorry if i havnt

Thanks

Ben
 
Try selecting results with no WHERE clause to see if you can get any data; then try reducing the results.

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
you've spelt "category" incorrectly in the field name of the select control on the form.
 
i took out the WHERE and get all the info from the table, ill continue to try and reduce the results

thanks

Ben
 
thanks thats now working, im not sure which bit has made it work? but its working and the code looks the same as i had it before.

Thanks everyone

Code:
<?php
 include '../admin/db.php';
?>
 <html>
<head><title>test</title></head>
<body>
<?php

/* grabs the POST variable and put it into a variable to use */ 
$category = $_POST['category']; 

$location = 'london_se';

$query = "SELECT * FROM directory WHERE category = '$category' AND location = '$location' ";
$result = mysql_query($query)
or die ("Couldn't execute query.");

echo "<table>\n";

while ($row = mysql_fetch_assoc($result)) {

   echo "<tr><td>" . $row['location'] .
        "</td><td>" . $row['category'] .
        "</td><td>" . $row['company'] .
        "</td><tr>\n";
}
echo "</table>\n";

?>
</body>
</html>

I have more question reqarding this i will post back soon

Thanks everyone
 
the $_POST variables are populated by the names on the controls in the sending forms. if there is no control called category then the value of $_POST['category'] will be null (or in the context of a database, empty). variables are case sensitive and literal. by calling the select control catagory you would have had to refer to $_POST['catagory'] in the receiving code. you were referring to $_POST['category']. by fixing the spelling you fixed the code.
 
the $_POST variables are populated by the names on the controls in the sending form (if the form has method=post). if there is no control called category then the value of $_POST['category'] will be null (or in the context of a database, empty). variables are case sensitive and literal. by calling the select control catagory you would have had to refer to $_POST['catagory'] in the receiving code. you were referring to $_POST['category']. by fixing the spelling you fixed the code.
 
Thanks jpadie, appreciate it, i prefer to know whats going on than not knowing why? how? etc.

Thank you

 
I have another question.

Instead of displaing the results in test.php.

How would i incorporate the script into the same page the form is on and show the results below the form?

This is the code for the page that the form is on
Code:
<html>
<head>

<title>Programme</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../style.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#0099CC">
[COLOR=green]<? include 'includes/inc_top.html';?>[/color]
<div id="body"> 
    
  <table align="center" cellpadding="4" cellspacing="0" class="body">
    <tr> 
      <td width="634" height="335" align="center" valign="top" bordercolor="#FFFFFF"> 
        <h1 class="register">SEARCH - london &amp; South-East</h1>
        [COLOR=red]<form action="test.php" method="post" onsubmit="return check(this)">
		  <table width="96%" border="0" align="left" cellpadding="0" cellspacing="0">
            <tr> 
            <td width="31%"> <p class="body">Search for an expert in the field 
                  of </p></td>
            <td width="30%"><select name="category" class="FormField">
                <option value="00" <?=$categorySelect == "" ? "selected"  : "" ?> selected>Select 
                a category 
                <option value="allergy_testing" <?=$categorySelect == "allergy_testing" ? "selected"  : "" ?> >Allergy 
                Testing 
                <option value="exercise" <?=$categorySelect == "exercise" ? "selected"  : "" ?> >Exercise 
                <option value="homeopath" <?=$categorySelect == "homeopath" ? "selected"  : "" ?> >Homeopath 
                <option value="hypnotherapist" <?=$categorySelect == "hypnotherapist" ? "selected"  : "" ?> >Hypnotherapist 
                <option value="image_consultant" <?=$categorySelect == "image_consultant" ? "selected"  : "" ?> >Image 
                Consultant 
                <option value="nutrition" <?=$categorySelect == "nutrition" ? "selected"  : "" ?> >Nutrition 
                <option value="pilates" <?=$categorySelect == "pilates" ? "selected"  : "" ?> >Pilates 
                <option value="reflexologist" <?=$categorySelect == "reflexologist" ? "selected"  : "" ?> >Reflexologist 
                <option value="relaxation" <?=$categorySelect == "relaxation" ? "selected"  : "" ?> >Relaxation 
                <option value="stress_management" <?=$categorySelect == "stress_management" ? "selected"  : "" ?> >Stress 
                Management 
                <option value="yoga" <?=$categorySelect == "yoga" ? "selected"  : "" ?> >Yoga 
                <option value="other" <?=$categorySelect == "other" ? "selected"  : "" ?> >Other 
                - Reiki, Feng Shei etc </select></td>
            <td width="39%"><input type="submit" name="add" value="Search"></td>
          </tr>
        </table>
		</form>[/color]</td>
      <td width="124" valign="top" bgcolor="#FFFFFF"> 
        <table height="55" border="0" align="center" cellpadding="0" cellspacing="0" class="login" id="login">
          <tr> 
            <td width="40%" height="29" align="center" valign="bottom"><img src="../images/login_inc.gif" width="82" height="29"></td>
          </tr>
          <tr> 
            <td height="22" valign="middle"> 
              <p class="logtext"><a class="loginc" href="../coaches">Coaches</a>&nbsp; 
                <font color="#FF6600">|</font>&nbsp;&nbsp;<a class="loginc" href="../clients">Clients</a></p></td>
          </tr>
        </table>
        
        <a href="[URL unfurl="true"]http://www.institute.org"><img[/URL] src="../images/eci.gif" alt="UK accredited" name="eci" width="124" height="98" border="0" id="eci"></a><br>
        <table width="124" border="1" cellpadding="0" cellspacing="0" bordercolor="#000066" bgcolor="#0099CC" >
          <tr> 
            <td height="42"> 
              <p class="logtext"><font color="#FFFFFF">FREE Newsletter</font> 
                <BR>
                <a class="loginc" href="../subscribe.html" target="_blank"  onClick="window.open(this.href,'myNewWindow','top=60,left=150,width=240,height=155'); return false;">Subcribe Today</a></p></td>
          </tr>
          <tr>
            <td height="38">
<p class="tell"><img src="../images/email.gif" width="30" height="11"> <a class="loginc" href="../tellfriend.shtml">Tell 
                a Friend</a></p></td>
          </tr>
        </table>
        <img src="../images/smileinc.jpg" width="124" height="88"></td>
    </tr>
  </table>

</div>
[COLOR=green]<? include 'includes/inc_footer.html';?>[/color]
</body>
</html>

Is it possible??
 
set the form action to the same page name as the form itself.

at the start of that file test to see whether $_POST['submit'] is set and that it's value is what you expect. if it is then run the code, if it is not then display the form

Code:
<?
if (isset($_POST['submit']) $_POST['submit'] === "save")
{
  //display data
  print_r ($_POST);
}
else
{
?>
//insert form html here
<?

}
?>
 
ok , not sure if im doing this right??

i called the file temp.php heres the code
Code:
<?
if (isset($_POST['submit']) $_POST['submit'] === "save")
{
  //display data
  print_r ($_POST);
}
else
{
?>
<form action="temp.php" method="post" onsubmit="return check(this)">
		  <table width="96%" border="0" align="left" cellpadding="0" cellspacing="0">
            <tr> 
            <td width="31%"> <p class="body">Search for an expert in the field 
                  of </p></td>
            <td width="30%"><select name="category" class="FormField">
                <option value="00" <?=$categorySelect == "" ? "selected"  : "" ?> selected>Select 
                a category 
                <option value="allergy_testing" <?=$categorySelect == "allergy_testing" ? "selected"  : "" ?> >Allergy 
                Testing 
                <option value="exercise" <?=$categorySelect == "exercise" ? "selected"  : "" ?> >Exercise 
                <option value="homeopath" <?=$categorySelect == "homeopath" ? "selected"  : "" ?> >Homeopath 
                <option value="hypnotherapist" <?=$categorySelect == "hypnotherapist" ? "selected"  : "" ?> >Hypnotherapist 
                <option value="image_consultant" <?=$categorySelect == "image_consultant" ? "selected"  : "" ?> >Image 
                Consultant 
                <option value="nutrition" <?=$categorySelect == "nutrition" ? "selected"  : "" ?> >Nutrition 
                <option value="pilates" <?=$categorySelect == "pilates" ? "selected"  : "" ?> >Pilates 
                <option value="reflexologist" <?=$categorySelect == "reflexologist" ? "selected"  : "" ?> >Reflexologist 
                <option value="relaxation" <?=$categorySelect == "relaxation" ? "selected"  : "" ?> >Relaxation 
                <option value="stress_management" <?=$categorySelect == "stress_management" ? "selected"  : "" ?> >Stress 
                Management 
                <option value="yoga" <?=$categorySelect == "yoga" ? "selected"  : "" ?> >Yoga 
                <option value="other" <?=$categorySelect == "other" ? "selected"  : "" ?> >Other 
                - Reiki, Feng Shei etc </select></td>
            <td width="39%"><input type="submit" name="add" value="Search"></td>
          </tr>
        </table>
		</form>
<?

}
?>

im getting this error
Code:
Parse error: parse error, unexpected T_VARIABLE in /home/godonovan/public_html/weightloss/locations/temp.php on line 2

thanks Ben
 
hi ,

i now have it working. Thanks alot jpadie. I loves a bit of php.

i'm just wondering now how to style my results, maybe putting a line break between the results and styling the text using something like <p class="body"> also verifying the width of the table.

Im not sure how to work it into this php code
Code:
echo "<br>";
echo "<br>";
echo "<table>\n";

while ($row = mysql_fetch_assoc($result)) {

   echo "</td><td>" . $row['category'] .
        "</td><td>" . $row['company'] .
		"</td><td>" . $row['name'] .
		"</td><td>" . $row['web_address'] .
		"</td><td>" . $row['email_address'] .
		"</td><td>" . $row['tel'] .
        "</td><tr>\n";
}
echo "</table>\n";

thanks for anymore advice

Ben
 
note that your last <tr> shoud be a </tr>

for styling i'd use css as you suggest, attach a class to each <tr> tag (<tr class="whatever"> in your code then just declare the style in a style block.

if you want alternating colours in your rows, declare two classes called row1 and row2 respectively and set them to different background-color's

then in your code, add the following inside the while statement

Code:
if ($style != "row1") {$style = "row1";} else {$style="row2";}

and in your row statement use
Code:
echo "<tr class=\"$style\">";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top