Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
HowardDrake
Grand Gerbil Poohbah
Topic Author
Posts: 3523
Joined: Thu Dec 27, 2001 7:00 pm
Location: Action Jim's Rumpus Room
Contact:

Quick bit of PHP help

Wed Apr 21, 2004 2:55 pm

I've written this function in PHP to populate a combo box in a form:

<?php
   function get_combo_select($name, $source, $label)
       {
           $res = db_query("select * from " . $source);
           $work = "<select name=\"$name\">\n";
           while ($row = mysql_fetch_array($res))
            {
               $work .= '    <option value="' . $row['code'] . '">' . $row['description'];
           }
        return $work . "</select>\n";
    }

?>


I'm trying to call it like this:

<? php echo(get_combo_select("shape","shape","Shape"); ?>


What am I doing wrong? This is the error I get:

Parse error: parse error, unexpected T_ECHO in C:\Apache\htdocs\add_record.php on line 31


Thanks in advance :)
No wonder television's a medium. It's so seldom rare or well done. -Mighty Mouse
Image
 
lelokong
Gerbil
Posts: 19
Joined: Wed Jul 02, 2003 6:59 pm
Contact:

Re: Quick bit of PHP help

Wed Apr 21, 2004 3:52 pm

unexpected T_ECHO usually mean theres something wrong before the echo, like a missing semicolon. but how u posted the script, everything seem good... :(
 
morphine
TR Staff
Posts: 11600
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Wed Apr 21, 2004 5:20 pm

Debugging would be a whole lot easier if you told us what's before and in line 31 :)
 
HowardDrake
Grand Gerbil Poohbah
Topic Author
Posts: 3523
Joined: Thu Dec 27, 2001 7:00 pm
Location: Action Jim's Rumpus Room
Contact:

Wed Apr 21, 2004 6:25 pm

Ok, let me grab and post the whole php file :)
No wonder television's a medium. It's so seldom rare or well done. -Mighty Mouse

Image
 
HowardDrake
Grand Gerbil Poohbah
Topic Author
Posts: 3523
Joined: Thu Dec 27, 2001 7:00 pm
Location: Action Jim's Rumpus Room
Contact:

Wed Apr 21, 2004 11:32 pm

Here's the whole page:

<html><head>
<title>Add A Record</title></head>
<body>
<?php
#connect to mySQL
$conn = @mysql_connect("localhost","root","") or die("Could not connect");

#select the proper database
$rs = @mysql_select_db("diamond", $conn) or die ("Could not select database");
?>

<?php
   function get_combo_select($name, $source, $label)
       {
           $res = db_query("select * from " . $source);
           $work = "<select name=\"$name\">\n";
           while ($row = mysql_fetch_array($res))
            {
               $work .= '    <option value="' . $row['code'] . '">' . $row['description'];
           }
        return $work . "</select>\n";
    }

?>

<form action="<?php echo($PHP_SELF); ?>" method="post">

<? php echo(get_combo_select("shape","shape","Shape"); ?>

<input type="submit" value="Submit">
<input type="reset" value="Reset Form">

</form>

<?php

if( $stone)
{
   $conn = @mysql_connect("localhost","root","") or die("Could not connect");

   #select the proper database
   $rs = @mysql_select_db("diamond", $conn) or die ("Could not select database");

   $sql="insert into stones (shape) values ( \"$shape\" )";
#execute query
   $rs=mysql_query($sql,$conn);
   if($rs)
      {
         echo("Record added:$shape");
      }
}
?>

</body></html>
No wonder television's a medium. It's so seldom rare or well done. -Mighty Mouse

Image
 
morphine
TR Staff
Posts: 11600
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Thu Apr 22, 2004 7:28 am

[snip snip snip...]
<? php echo(get_combo_select("shape","shape","Shape"); ?>


This should read:
<?php echo(get_combo_select("shape","shape","Shape")); ?>

You had an extra space between "<?" and "php" and you were missing a closing parenthesis at the end of the line. By the way, you only need to do "<?php" instead of just "<?" if you want your files to be 100% XML-parser compliant, which probably won't be of much interest.
 
HowardDrake
Grand Gerbil Poohbah
Topic Author
Posts: 3523
Joined: Thu Dec 27, 2001 7:00 pm
Location: Action Jim's Rumpus Room
Contact:

Thu Apr 22, 2004 1:24 pm

Can't believe I missed something as simple as a paren :-? Thanks for the help :)

But of course, I have another small booboo here :lol:

<?php
   if( ($user != null))
    {
      setcookie("user","root","","/","",0);
      setcookie("pass","","","/","",0);
      header("Location: http://locallost/get_stones.php");
      exit();
    }
?>

<html><head><title>Log Into Diamond Database</title></head>
<body>
<form action ="<?php echo($PHP_SELF); ?>" method="post">
<h3>Please enter your login information:</h3><br><br>
Login Name:
<input type="text" name="name"><br><br>
Password:
<input type="text" name="pass"><br><br>
<input type="submit" value="Log In">
</form>
</body>

</html>


The header() command doesn't seem to redirect properly :-? When I type in that URL directly into the page it works perfectly :(
No wonder television's a medium. It's so seldom rare or well done. -Mighty Mouse

Image
 
BigMadDrongo
Gerbil Elite
Posts: 909
Joined: Mon Apr 29, 2002 12:57 pm
Location: London, UK
Contact:

Thu Apr 22, 2004 1:58 pm

HowardDrake wrote:
header("Location: http://locallost/get_stones.php");

Local-Lost? ;)
 
morphine
TR Staff
Posts: 11600
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Thu Apr 22, 2004 2:08 pm

He seems totally lost, alright :lol:
 
HowardDrake
Grand Gerbil Poohbah
Topic Author
Posts: 3523
Joined: Thu Dec 27, 2001 7:00 pm
Location: Action Jim's Rumpus Room
Contact:

Thu Apr 22, 2004 2:15 pm

Argh, I hate typos :evil:
No wonder television's a medium. It's so seldom rare or well done. -Mighty Mouse

Image

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On