Quick bit of PHP help

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

Quick bit of PHP help

Postposted on Wed Apr 21, 2004 2:55 pm

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

Code: Select all
<?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:

Code: Select all
<? php echo(get_combo_select("shape","shape","Shape"); ?>


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

Code: Select all
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
HowardDrake
Grand Gerbil Poohbah
 
Posts: 3568
Joined: Thu Dec 27, 2001 7:00 pm
Location: Action Jim's Rumpus Room

Re: Quick bit of PHP help

Postposted on 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... :(
lelokong
Gerbil
 
Posts: 19
Joined: Wed Jul 02, 2003 6:59 pm

Postposted on 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 :)
morphine
Gerbil Khan
Silver subscriber
 
 
Posts: 9932
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Postposted on 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
 
Posts: 3568
Joined: Thu Dec 27, 2001 7:00 pm
Location: Action Jim's Rumpus Room

Postposted on Wed Apr 21, 2004 11:32 pm

Here's the whole page:

Code: Select all
<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
HowardDrake
Grand Gerbil Poohbah
 
Posts: 3568
Joined: Thu Dec 27, 2001 7:00 pm
Location: Action Jim's Rumpus Room

Postposted on Thu Apr 22, 2004 7:28 am

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


This should read:
Code: Select all
<?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.
morphine
Gerbil Khan
Silver subscriber
 
 
Posts: 9932
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Postposted on 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:

Code: Select all
<?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
HowardDrake
Grand Gerbil Poohbah
 
Posts: 3568
Joined: Thu Dec 27, 2001 7:00 pm
Location: Action Jim's Rumpus Room

Postposted on Thu Apr 22, 2004 1:58 pm

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

Local-Lost? ;)
BigMadDrongo
Gerbil Elite
 
Posts: 913
Joined: Mon Apr 29, 2002 12:57 pm
Location: London, UK

Postposted on Thu Apr 22, 2004 2:08 pm

He seems totally lost, alright :lol:
morphine
Gerbil Khan
Silver subscriber
 
 
Posts: 9932
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Postposted on 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
HowardDrake
Grand Gerbil Poohbah
 
Posts: 3568
Joined: Thu Dec 27, 2001 7:00 pm
Location: Action Jim's Rumpus Room


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 1 guest

cron