Parse error: FETCH data from mysql and bind to html select control on form

dear advanced php coders,
thanks for your wonderful contributions to this community.
However, I have a problem and it is summarized in the codes below… but the returned error is:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\dce\province\register.php on line 130

I want to fetch records from mysql and and bind them to the <SELECT><OPTION VALUE></OPTION></SELECT> controls on HTML form.

Please help me, it’s not working as the code below specifies…

Thank you!


<?php session_start();
$server="localhost";
$server_user="root";
$server_pass="";
$db_name="dce";

if (!$con=mysql_connect($server,$server_user,$server_pass)) {
die('Could not connect: ' . mysql_error()); }
elseif(!mysql_select_db($db_name, $con)) {
die("Could not connect to the database: " . mysql_error()); }

else {

}
/*
elseif (!$load_region=mysql_fetch_array(mysql_query("SELECT region_number FROM region"))) {

	header("Location: ../Accounts/loginerror.php"); }

else {
//extract the column values of the database into variables.
$_regnum=$load_region['region_number']; }//region_number is the database field

mysql_close($con); //close connection*/
?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>DCE - New Region Registration</title>
<link href="../Style/Site.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style1 {
	color: #FFFFFF;
	font-weight: bold;
}
-->
</style>
</head>

<body>

 <form name="form1" action="confirm_province.php" method="post" style="margin-left:15px; margin-right:20px;">
	    <p><b>Select Region: </b><br />



<?php echo "<Select name='reg_name' size='1' class='passwordEntry'> ";
$load_region = mysql_query("SELECT region_number FROM region ORDER BY region_number", $con);

while ($row = mysql_fetch_array($load_region)) {
echo "<option value='<?php $row['region_number']; ?>'>" . $row['region_number']."</option></select>";
}
?>



<br /><br />
		<b>Location of Province:</b> <br />
        <input type="text" name="pro_location" value="" class="passwordEntry" />
        <br />
        <br />
          <b>Province Number:</b><br />
        <input type="text" name="pro_num" value="" class="passwordEntry" />
        <br />
        <br />
        <b>Username:</b> <br />
        <input type="text" name="pro_uname" value="" class="passwordEntry" />
        <br />
        <br />
        <b>Password:</b> <br />
        <input type="password" name="pro_pword" value="" class="passwordEntry" />
        <br />
        <br />
        <b>Confirm Password:</b> <br />
        <input type="password" name="pro_con_pword" value="" class="passwordEntry" />
        <br />
        <br />
        <!--<b>Email Address:</b><br />
<input type="text" name="email" value="" class="passwordEntry" /><br /><br />-->
        <input type="submit" name="btnRegister" value="Register" class="submitButton" />
      </p>
  </form>
 <p>&nbsp;</p>
</div>

 </div>

<div class="footer">
     <?php include("../footer.php");?>
   </div></p>
</body>
</html> 

Change your while loop code too:


while ($row = mysql_fetch_array($load_region)) {
    echo '<option value="'. $row['region_number'] .'">' . $row['region_number'].'</option>'; 
} 
?>
</select>

You dont use <?php inside an echo statement and the <select tag should be OUTSIDE the loop.

Thank you spikeZ. You are my guy as well… but let me try the code and I will get back to you for more info as regards it.

WOW! This code really did the job I’ve been pondering over for about 24hrs. Thanks to you spikeZ, God bless you!

You are welcome. :slight_smile:

Sorry SpikZ,

what do you think of this scenario?

am creating a web app for an organisation. This organisation is segmented into different sub organisations…
example:
MAIN ORGANISATION: COMPANY NAME
REGION SEGMENT

//region segment has only region_number

PROVINCE SEGMENT under the REGION SEGMENT

//province segment has both province_name and province_number

ZONE SEGMENT under the PROVINCE SEGMENT

 //zone has only zone_number

AREA SEGMENT under the ZONE SEGMENT

 //area also has only area_number and

Then, local offices under the AREA SEGMENT

local offices have only office_names 

I have created these tables and I have also created an initial user registration and user login pages.
Also, I want a scenario where on first login, start_session(), use the session_variable to access the main page.
From the main_page.php, you can create REGION data, you can also create REGION user_registration and login pages.
When you login to the REGION, you can create PROVINCES, and also create PROVINCE user_reg and user_login pages.
When you login to the PROVINCE, you can create ZONES, and also create ZONE user_reg and user_login pages.
Same thing for the AREA.
And finally, you do not the different local offices.
That is, for each SEGMENT, you have both registration and login pages.
I think this might not be the correct format.
That is why I am asking for your opinion and a possible proper way of doing this decently without data replication.

Thank you!
Please find the code below for the tables…the code works fine…no error.


<?php
$server="localhost";
$server_user="root";
$server_pass="";
$db_name="dce";

//include ("../db/connStrings.php");
#CREATING TABLES
$create_tbl_users="CREATE TABLE userslogin("."name VARCHAR(20) NOT NULL,"."uname VARCHAR(20) NOT NULL,"."pword VARCHAR(20) NOT NULL, email VARCHAR(60) NOT NULL".")";
//region has only number
$create_tbl_region="CREATE TABLE region("."region_id INT(9) NOT NULL AUTO_INCREMENT PRIMARY KEY, region_number INT(9) NOT NULL".")";
//province has both location and number
$create_tbl_province="CREATE TABLE province("."province_id INT(9) NOT NULL AUTO_INCREMENT PRIMARY KEY, province_location VARCHAR(20) NOT NULL, province_number INT(9) NOT NULL".")";
//zone has only number....use _zone for this table cos zone is a reserved word.
$create_tbl_zone="CREATE TABLE _zone("."zone_id INT(9) NOT NULL AUTO_INCREMENT PRIMARY KEY, zone_number INT(9) NOT NULL".")";
//area only has number .... user _area for the table name bcos area is a reserved word.
$create_tbl_area="CREATE TABLE _area("."area_id INT(9) NOT NULL AUTO_INCREMENT PRIMARY KEY, area_number INT(9) NOT NULL".")";
//parish only has name of church
$create_tbl_parish="CREATE TABLE parish("."parish_id INT(9) NOT NULL AUTO_INCREMENT PRIMARY KEY, parish_name VARCHAR(40) NOT NULL".")";

$connect=mysql_connect($server,$server_user,$server_pass);

if (!$connect) {  //if there is no connection
die ("There is no database present in this server."); }
else { //if there is connection 
//echo "there is a connection.<br />";
$connect_db = mysql_select_db($db_name,$connect); } //select the database.

if($connect_db) { //if there is database to select
//echZo "the database already exists.<br />"; 
die(); } 
//if there is no database to select
elseif (!mysql_query("CREATE DATABASE $db_name",$connect)) {//create a database. but if there is failure
die("Error creating Database: " . mysql_error()); 
} 
elseif (!mysql_select_db($db_name)) { //if there is no error in creating the database
die("Error selecting database: ".mysql_error()); }

//create the table usersLogin in the database.
elseif(!mysql_query($create_tbl_users)) { //if the table did not create
die("Error creating table USERS: ". mysql_error()); } //if the table was created then

//create the table region in the database.
elseif (!mysql_query($create_tbl_region)) { //if the table did not create
die("Error creating table REGION: ". mysql_error()); } 

//create province table
elseif (!mysql_query($create_tbl_province)) {//if the table was created then create another table
die("Error Creating Table PROVINCE: ".mysql_error()); } 

//create zone table
elseif(!mysql_query($create_tbl_zone)) {
die("Error Creating Table ZONE: ".mysql_error()); }

//create area table
elseif(!mysql_query($create_tbl_area)) {
die("Error Creating Table AREA: ".mysql_error()); }

#create parish table
elseif(!mysql_query($create_tbl_parish)) {
die("Error Creating Table PARISH: ".mysql_error()); }

else {

	mysql_close($connect);
exit;
	}
?>


I am trying to create tables for each segment.

I want to parse a value from the dropdown option value of html form into a php variable… It is not working. The details of the code is found below… Please see the code below:

  
<form name="form1" action="confirmReg.php" method="post" style="margin-left:15px; 
  margin-right:20px;;">
  <b>Full Name:</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#993333">
  <b>Register As:</b></font> <br />
  <input type="text" name="name" value="" class="passwordEntry" /> 
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select size="1" name="reg_right"><option 
 value="">Select Your Registration Right</option><option 
 value="Administrator">Administrator</option><option value="Province">Province</option>
<option value="Zone">Zone</option><option value="Area">Area</option><option 
value="Parish">Parish</option></select><br />
<br />
<b>Username:</b> <br />
<input type="text" name="uname" value="" class="passwordEntry" /><br /><br />
<b>Password:</b> <br />
<input type="password" name="pword" value="" class="passwordEntry" /><br /><br />
<b>Confirm Password:</b> <br />
<input type="password" name="con_pword" value="" class="passwordEntry" /><br /><br />
<b>Email Address:</b><br />
<input type="text" name="email" value="" class="passwordEntry" /><br /><br />
<input type="submit" name="btnRegister" value="Register" class="submitButton" />
</form> 

here is the php function


<?php    
 $name=($_REQUEST['name']); 
    $uname=($_REQUEST['uname']);
    if(!filter_var($name,FILTER_SANITIZE_STRING)) {
    die("Invalid Name"); }
    else{
    $pword=filter_var($_REQUEST['pword'], FILTER_SANITIZE_STRING);
    $con_pword=filter_var($_REQUEST['con_pword'], FILTER_SANITIZE_STRING);
    //$email=filter_var($_POST['email'], FILTER_SANITIZE_EMAIL); 

    $to=$_REQUEST['email'] ;
    $from="info@dce.com";
    $subject="User Registration Confirmation!";
    $body=
    "You have received this mail as a result of the registration process you initiated 
    on our webpage. <br>
    Kindly activate your email to start using your account. This activation link <a 
    href='http://dce.com/user/email_activation.php'> " .session_id(). "</a> will 
    expire in 15 minutes.";

    }

    #parse the form select value to a variable.
    $reg_right=($_POST['reg_right']);
    #check whether it is the default value.
    if($reg_right="Select Your Registration Right") {
    die("Invalid Registration right."); } else {
    echo $reg_right; } ?> 


Hi Easytime

The HTML is ok, just not ‘tidy’!
If you want it to be set out in tidy HTML, add a little bit to the end of the line.


 echo '<option value="'. $row['region_number'] .'">' . $row['region_number'].'</option>'."/r/n"; 

See if that helps.

As for your logic, give me a little time to go through it and I will respond!

Oh SPikZ, thanks very much for your concern…I was able to get the error fixed before you could respond to my post. Although you have posted the same thing…I am so much grateful. You know it’s nice knowing you as a brother over there in Man Utd, UK? However, am still expecting your own side of reasoning concerning the logic. Although I have decided to move on with something else.

There are many tables in the app and it’s a very wide project. I want to build this project for a church FREE OF CHARGE. It’s a large organisation which has different headquarters like REGION, PROVINCE, ZONE, AREA and the parishes.
Take the UK for instance:: Microsoft Corporation is the company. It has offices in different states of England. In each state, there is a REGION headquarter, under each region, there are PROVINCES, under each province, there are ZONES, under each zone, there are AREAS, and under each area, there are the OFFICES. The region controls the provinces who control the zones that control the areas. The offices report to their respective AREAS. For example, Microsoft Corp Engineering office is under Area1-Zone 6-Province 20-Region 1. That’s it.

Also, in the offices, there are operations like staff training etc… so, I will have tables for staff, training dept, etc.
This is how large this project entails.

dear spikeZ, please kindly help me with this bug.
from the code below, I am reading 2 columns from mysql, and I want to concatenate them and populate a select dropdown box in the html form. I am having serious problem here. It works, but it does not populate the select dropdown box…rather, it prints out the output on screen.

Please help me.
Thank you.


<?php echo "<Select name='province_number' size='1'> ";
$load_province = mysql_query("SELECT province_number, province_location FROM province ORDER BY province_number");

while ($row = mysql_fetch_array($load_province)) {
//trying to concatenate them manually
#$province=$row['province_location']." ".$row['province_number'];
#echo '<option value="'. $province .'">' . $province .'</option>';

echo '<option value="'. $row['province_location']." ". $row['province_number'] .'">' . $row['province_location']." ". $row['province_number'].'</option>';

echo "</select> ";
}
?>


Not quite the same thing! Look at the end of the line I posted, it has \r
on the end. This tells the browser to start a new line and keeps your source code tidy.

When you concatenate a value you use . (period)
eg:


$var_1 = 'myvar_1';
$var_2 = 'myvar_2';

$var_3 = $var_1 . $var_2;

In your case you need to respect the quotes ’ and " when putting the values together.


echo '<option value="'. $row['province_location'] . $row['province_number'] .'">' . $row['province_location'] .' - '.   $row['province_number']. '</option>'; 


Oh, my guy, this code is not working well. It is not populating the comboBox… it is rather writing the output to the page. as in:::

Lagos - 1
Ogun - 2
Value - 3
etc…

I do not know what to do here…please try this code in your browser first. Thank you!


<?php echo "<Select name='province_number' size='1'> ";
$load_province = mysql_query("SELECT province_number, province_location FROM province ORDER BY province_number");

while ($row = mysql_fetch_array($load_province)) {
$province=$row['province_location']." ".$row['province_number'];
echo '<option value="'. $row['province_location'] . $row['province_number'] .'">' . $row['province_location'] .' - '.   $row['province_number']. '</option>';
echo "</select> ";
}?>

I have solved the problem… Thank you!

Dear pros, I have 2 select option comboboxes on the form. One combo box reads data from mysql on pageLoad event.
OnChange of the selected option, php should read another data from mysql into the second combo box.
The second read-data should be in a function (not necessarily, just if there is a simpler way to do it). But please check the code below: Thank you!


<form name="form1" action="confirm_zone.php" method="post" style="margin-left:15px; margin-right:20px;">
	    <p><b style="color:#990000;">Select / Choose your Region:</b>


<?php
//this code reads first data from mysql and populate 
//combo box 1.
//this is where the onChange event will come in.

$load_region = mysql_query("SELECT region_number FROM region ORDER BY region_number");
echo "<Select name='region' size='1' onChange='". read($data)  ."'> ";//read($data) is a presumed function

while ($row = mysql_fetch_array($load_region)) {
  echo '<option value="'. $row['region_number'] .'">' . $row['region_number'].'</option>'; } 
echo "</select> ";
?>


&nbsp;&nbsp;&nbsp;&nbsp;
<b style="color:#990000;">Select your Province:</b>


<?php
//this should be the function.
function read($data) {

$dropdown ="<Select name='province' size='1'> ";
$load_province = mysql_query("SELECT province_number, province_location FROM province ORDER BY province_number");

while ($row = mysql_fetch_array($load_province)) {
$dropdown .= "\\r\
<option value='{$row['province_location']}'" . "-{$row['province_number']}>" ."{$row['province_location']}" . " {$row['province_number']}".  "</option>"; }

$dropdown .= "\\r\
</select>";
echo $dropdown."<br>";
}
?> <!-- end of function -->

Thank you!