Jump to content


Photo

Loosing Variable after $_POST


  • Please log in to reply
6 replies to this topic

#1 avo

avo
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 13 June 2006 - 04:50 PM

Hi all

First sorry about the lengthy post

can anyone please help me with this all my code is listed bellow ive split it up where the prob is
whats happenings is when i do a post the varible is loosing its data .
<?
include ('includes/dbconfig.php');

$no_search ="";


?>

<style type="text/css">
<!--
@import url("style.css");
</style>
<form name="form1" method="post" action="">
<table width="800" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#84ADD6" bgcolor="#FFFFFF">
  <tr>
    <th align="center" valign="middle" background="flash/header.swf" scope="col"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="800" height="120">
      <param name="movie" value="flash/header.swf" />
      <param name="quality" value="high" />
      <embed src="flash/header.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="800" height="120"></embed>
    </object></th>
  </tr>
</table>

<table width="800" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#84ADD6">
  <tr>
    <th align="left" valign="top" scope="col"><table width="800" height="27" border="0" align="center" cellpadding="0" cellspacing="0">
      
      <tr>
        <th width="168" height="20" align="left" valign="top" class="form-text"><div align="center" class="frm_header_text">Part Numbers </div></th>
        <th width="14" align="left" valign="top" class="form-text"> </th>
        <td width="618" height="25" align="right" valign="middle" class="frm_header_text style3"><span class="style3"><? echo "<u>"; echo date('h:i:s A  l dS F Y'); echo "</u>";?></span></td>
        </tr>
    </table>
      <table width="800" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <th width="181" rowspan="11" align="center" valign="top" scope="col"> <p>
            <select name="prt_list" size="25">
              <option value><><><><><><><></option>
              <?
/////////////////////////////
//Part Number Search button//    
/////////////////////////////            
/////////////////////////////////////////////////
//If when button presses no seach value entered//    
/////////////////////////////////////////////////
$search_done ="false";
if ($_POST['btn_search'] && $_POST['txt_search']==""){
$no_search_des ="";
$no_search_num ='No Search Entered .';
////////////////////////////////////////////////////
//GET ALL INFORMATION FROM DB AND LOOP ECHO VALUES//    
////////////////////////////////////////////////////            
//connect to db
$db = mysql_connect ($dbhost, $dbuser, $dbpass);
mysql_select_db ($dbname) or die ( mysql_error ());
//get data for part numbers list
$query = "SELECT part_number FROM parts_db ORDER BY part_number ASC";
$result = mysql_query ($query) or die ( mysql_error () );          
//creates array and loop part numbers
while($row=mysql_fetch_array($result)) { 
echo "<option value='{$row["part_number"]}'>{$row["part_number"]}</option>"; 
}

//////////////////////////////////////////////////////////////////////////
//IF SEARCH PRESSED SEARCH FOR INFO IN TEXT BOX AND ECHO VALUE INTO LIST//
//////////////////////////////////////////////////////////////////////////
}elseif ($_POST['btn_search'] && $_POST['txt_search']!=""){
$no_part_info_found="false";
$db = mysql_connect($dbhost, $dbuser, $dbpass)or die (mysql_error ());
mysql_select_db ($dbname) or die ( mysql_error ());
// create a simple query m =search perameter
$query = "SELECT part_number FROM parts_db WHERE part_number LIKE '%".$_POST['txt_search']."%'ORDER BY part_number ASC";    
$result = mysql_query($query, $db)or die ( mysql_error ());

while ($row = mysql_fetch_assoc($result)){
$part_number_info ="{$row["part_number"]}";
}

if($part_number_info==""){
$no_part_info_found ="true";
}

if ($no_part_info_found=="false"){
$search_done ="true";   
$result = mysql_query($query, $db)or die ( mysql_error ());
while ($row = mysql_fetch_assoc($result))
{
echo "<option value='{$row["part_number"]}'>{$row["part_number"]}</option>"; 

}}

} else {
////////////////////////////////////////////////////
//GET ALL INFORMATION FROM DB AND LOOP ECHO VALUES//    
////////////////////////////////////////////////////        
//connect to db
$db = mysql_connect ($dbhost, $dbuser, $dbpass);
mysql_select_db ($dbname) or die ( mysql_error ());
//get data for part numbers list
$query = "SELECT part_number FROM parts_db ORDER BY part_number ASC";
$result = mysql_query ($query) or die ( mysql_error () );          
//creates array and loop part numbers
while($row=mysql_fetch_array($result)) { 
echo "<option value='{$row["part_number"]}'>{$row["part_number"]}</option>"; 

} 
}
mysql_close ($db);
?>
</select>
  <br>            
</br>
<table border="0" cellspacing="0" cellpadding="0">
             <tr>
             <th width="146" align="center" valign="top" scope="col"><input type="submit" class="frm_header_text" name="sel_search" value="Select" />
                      <?
/////////////////////////////
//Part Description Search //    
/////////////////////////////            
/////////////////////////////////////////////////
//If when button presses no seach value entered//    
/////////////////////////////////////////////////
if ($_POST['prt_des_search'] && $_POST['des_search']==""){
$no_search_num ="";
$no_search_des ='No Search Entered .';
}     ?>                  </th>
                <th width="16" align="left" valign="top" scope="col"> </th>
             </tr>
            </table></th>
        </tr>
        <tr>
          <th width="292" height="20" scope="col"><span class="frm_header_text">Part Number Search </span></th>
          <th width="327" height="20" scope="col"><span class="frm_header_text">Part Description Search </span></th>
        </tr>
        <tr>
          <th height="20" class="style8" scope="col"><? echo $no_search_num ?></th>
          <th height="20" class="style8" scope="col"><? echo $no_search_des ?></th>
        </tr>
        <tr>
          <th height="20" class="style8" scope="col"></th>
          <th height="20" align="left" valign="top" class="style9" scope="col">Ticking <u>AND</u> Box Will Allow You To Search For More Than One Keyword </th>
        </tr>
        <tr>
          <th height="30" scope="col"> <input name="txt_search" type="text" id="txt_search" />
              <input name="btn_search" type="submit" class="frm_header_text" id="btn_search" value="Search" />          </th>
          <th height="30" scope="col"> <span class="style9">And</span>
              <input name="and" type="checkbox" id="and" value="checkbox" />
              <input name="des_search" type="text" id="des_search" />
              <input name="prt_des_search" type="submit" class="frm_header_text" value="Search" />          </th>
        </tr>
        <tr>
          <th height="30" scope="col"> </th>
          <th height="30" align="center" valign="top" scope="col"> <?
/////////////////////////////////////////////////////////////////////////////////////
//IF SEARCH PRESSED and DONT = "" ECHO LIST WITH DESCRIPTION IN AND A SEARCH BUTTON//
/////////////////////////////////////////////////////////////////////////////////////
if ($_POST['prt_des_search'] && $_POST['des_search']!=""){
$no_info_found ="false";

$db = mysql_connect($dbhost, $dbuser, $dbpass)or die (mysql_error ());
mysql_select_db ($dbname) or die ( mysql_error ());

$query = "SELECT part_des FROM parts_db WHERE ";
$desc_search  = $_POST['des_search'];
$search_array = explode(' ',$desc_search);
$search_array_count = count($search_array);

for($i=0; $i<$search_array_count; $i++){
    if ($_POST['and']==false){
        if ( $i > 0 ){
            $query .= " OR";
        }
    }elseif ($_POST['and']==true){
        if ( $i > 0 ){
            $query .= " AND";
        }
    }
$query .= " ( INSTR(part_des,'".$search_array[$i]."')  > 0 ) ";
}

$result = mysql_query($query, $db)or die ( mysql_error ());
while ($row = mysql_fetch_assoc($result)){
$list_information ="{$row["part_des"]}";
}

if($list_information==""){
$no_info_found ="true";
}

if ($no_info_found =="false"){
echo '<select name="drop_list" size="1">';
 
$result = mysql_query($query, $db)or die ( mysql_error ());
while ($row = mysql_fetch_assoc($result)){
echo "<option value='{$row["part_des"]}'>{$row["part_des"]}</option>"; 
}?>
              <input type="submit" class="large_button" name="description_search" value="Select" />
              <?
}
mysql_close ($db);

}

?>          </th>
        </tr>
        <?
if ($no_info_found =="true"){?>
        <tr>
          <th height="20" colspan="2" align="right" valign="top" class="style8" scope="col">No Information Found Please Try A New Search.</th>
        </tr>
        <? } 
if ($search_done =="true"){?>
        <tr>
          <th height="20" colspan="2" align="left" valign="top" class="style8" scope="col"><<<<<< List Narrowed Down</th>
        </tr>
        <? } 
if ($no_part_info_found =="true"){?>
        <tr>
          <th colspan="2" align="left" valign="top" class="style6" scope="col">Unable To Narrow Down List No Part number Starting With <? echo "<br><span class=\"large_button\">".$_POST['txt_search']."</span>"; ?><br />
              <br />
              <span class="frm_header_text">Please Click To
                <input type="submit" class="frm_header_text" name="reset_list" value="Refresh List" />
            </span></th>
        </tr>
        <? } ?>

  <th height="350" colspan="2" scope="col"> <?
$button_pressed ="no";
$select_error="no";


//select button pressed under part number box
if ($_POST['sel_search']){
        if ($_POST['prt_list']==""){
            $button_pressed ="no";
            $select_error="yes";
        }
            else{
 
                mysql_connect($dbhost, $dbuser, $dbpass)or die (mysql_error ());
                mysql_select_db ($dbname) or die ( mysql_error ());
                $query ="SELECT * FROM parts_db WHERE part_number ='".$_POST['prt_list']."'"; 
                $result = mysql_query ($query) or die ( mysql_error () );

                $button_pressed ="yes"; 

                while ($row = mysql_fetch_array($result)){
                    $p_number = $row['part_number'];
                    $q_in_stock = $row['quantity_in'];
                    $p_description = $row['part_des'];
                    $o_re_o_quantity = $row['reorder_quantity'];
                    $l_location = $row['location'];
                    $u_on = $row['used_on'];
                    $c_code = $row['code'];
                    $s_size = $row['size'];
            }
                }
}

// select button pressed under description search box
if ($_POST['description_search']) { 
        mysql_connect($dbhost, $dbuser, $dbpass)or die (mysql_error ());
        mysql_select_db ($dbname) or die ( mysql_error ());

        $query ="SELECT * FROM parts_db WHERE part_des ='".$_POST['drop_list']."'"; 
        $result = mysql_query ($query) or die ( mysql_error () );

        $button_pressed ="yes";

    while ($row = mysql_fetch_array($result)){
        $p_number = $row['part_number'];
        $q_in_stock = $row['quantity_in'];
        $p_description = $row['part_des'];
        $o_re_o_quantity = $row['reorder_quantity'];
        $l_location = $row['location'];
        $u_on = $row['used_on'];
        $c_code = $row['code'];
        $s_size = $row['size'];
    }
}
 

if ($select_error=="yes"){
  echo "<<<<<<<< Please select a part number first.";
}

    if ($button_pressed =="yes"){ ?>
          <table width="400" border="0" align="center" cellpadding="0" cellspacing="2">
            <tr>
              <th align="center" valign="middle" class="frm_header_text" scope="col"> </th>
              <th align="center" valign="middle" class="frm_header_text" scope="col"> </th>
            </tr>
            <tr>
              <th align="center" valign="middle" class="frm_header_text" scope="col"> </th>
              <th align="center" valign="middle" class="frm_header_text" scope="col"><input name="bookout" type="submit" class="frm_header_text" id="bookout" value="Booking IN/OUT" /></th>
            </tr>
            <tr>
              <th align="center" valign="middle" class="frm_header_text" scope="col"> </th>
              <th align="center" valign="middle" class="frm_header_text" scope="col"> </th>
            </tr>
            <tr>
              <th width="250" align="left" valign="middle" class="style7" scope="col">Part Number </th>
              <th width="144" align="center" valign="middle" scope="col"><input type="text" name="textfield" value="<?=$p_number?>"/></th>
            </tr>
            <tr>
              <td align="left" valign="middle" class="style7">Quantity in stock </td>
              <td align="center" valign="middle"><input type="text" name="textfield2" value="<?=$q_in_stock?>" /></td>
            </tr>
            <tr>
              <td align="left" valign="middle" class="style7">Part Description </td>
              <td align="center" valign="middle"><textarea name="textfield9" cols="15"><?=$p_description?>
               </textarea></td>
            </tr>
            <tr>
              <td align="left" valign="middle" class="style7">Re-order Quantity </td>
              <td align="center" valign="middle"><input type="text" name="textfield3" value="<?=$o_re_o_quantity?>" /></td>
            </tr>
            <tr>
              <td align="left" valign="middle" class="style7">Location</td>
              <td align="center" valign="middle"><input type="text" name="textfield4" value="<?=$l_location ?>"/></td>
            </tr>
            <tr>
              <td align="left" valign="middle" class="style7">Used In </td>
              <td align="center" valign="middle"><input type="text" name="textfield5" value="<?=$u_on ?>" /></td>
            </tr>
            <tr>
              <td align="left" valign="middle" class="style7">Code </td>
              <td align="center" valign="middle"><input type="text" name="textfield6" value="<?=$c_code ?>" /></td>
            </tr>
            <tr>
              <td align="left" valign="middle" class="style7">Size in mm </td>
              <td align="center" valign="middle"><input type="text" name="textfield7" value="<?=$s_size ?>" /></td>
</tr>
<tr>
<td align="left" valign="middle" class="style7"> </td>
<td align="center" valign="middle"> </td>
</tr>
<tr>
  <td align="left" valign="middle" class="style9">Pressing the update button will update any changes made on the form above if any. </td>
  <td align="center" valign="middle"><input name="btn_Update" type="submit" class="frm_header_text" id="btn_Update" value="Update Information" /></td>
</tr>
<tr>
  <td align="left" valign="middle" class="style7"> </td>
  <td align="center" valign="middle"> </td>
</tr>
<tr>
<td colspan="2" align="left" valign="middle"><input type="checkbox" name="add_to_order" value="checkbox" />
Tick to add information to be ordered then click ADD. </td>
</tr>
 <tr>
<td height="19" align="center" valign="middle"><input name="add" type="Submit" class="frm_header_text" id="add" value="ADD" /></td>
<td height="19" align="left" valign="middle"> </td><?php htmlspecialchars("Spaces In Between") ?>
</tr>
</table>
</th>
</tr>
<tr>
<td colspan="3"></td>
</tr>
</table>
<p> </p>    
</th>
</tr>
</table>
<? }

ok at this line im echoning my variable out in question all the data is there and showing it to me on the echo

echo $p_number;
echo $p_description;

this is where im loosing the variable is blank after this line the if post line bellow

//if box ticked and add button pressed
if ($_POST['add']&& $_POST['add_to_order']==true){
     mysql_connect($dbhost, $dbuser, $dbpass)or die (mysql_error ());
     mysql_select_db ($dbname) or die ( mysql_error ());
     $ord_query ="INSERT INTO parts_ordered (prt_number, part_description) VALUES ('$p_number','$p_description')"; 
     mysql_query ($ord_query) or die ( mysql_error () );
     $parts_ordered ="yes";
}
//if all good
  if($parts_ordered=="yes"){
    echo "$p_number Entered into part order list";
  }
  //if just add button pressed and no tick box
   elseif ($_POST['add']==true){
      echo "Please Re-select part then tick ADD box and try again";
   }

if i echo the $ord_query line this is what i get

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]INSERT INTO parts_ordered (prt_number, part_description) VALUES ('','')[/quote]

What silly mistake am i making or am i totaly missing something .
im confused to why

All help appriciated
thanks in advance.
Im loving it ........

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 13 June 2006 - 05:59 PM

Without digging into this too deeply, if those variables are coming from another page, you need to use :
$ord_query ="INSERT INTO parts_ordered (prt_number, part_description) VALUES ('" . $_REQUEST['p_number'] . "','" . $_REQUEST['p_description'] . "')";

That's assuming that your variables on the form are called by those names.

Info: PHP Manual


#3 avo

avo
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 13 June 2006 - 06:06 PM

[!--quoteo(post=383389:date=Jun 13 2006, 06:59 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Jun 13 2006, 06:59 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Without digging into this too deeply, if those variables are coming from another page, you need to use :
$ord_query ="INSERT INTO parts_ordered (prt_number, part_description) VALUES ('" . $_REQUEST['p_number'] . "','" . $_REQUEST['p_description'] . "')";

That's assuming that your variables on the form are called by those names.
[/quote]
HI Thanks

all the code above is on the same page thats why i can't understand why im loosing the variables if i add new data to the variable just where im echoing the values out replace that few lines with this

example

$p_number="test1";
echo "<br>";
$p_description="test2";

it passes the variables now if i echo the variables out after the post it works

ive also copyed and pasted the variable names to make sure that they are all correct

very straing

All help appriciated
Im loving it ........

#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 13 June 2006 - 06:28 PM

Well, here's something I don't understand: when you pull your data out of the database and stuff it into variables, you're using a while loop. The while loop is completely unnecessary when you're only grabbing one record. And if there's more than one record, you're only getting the last one.

I suggest verifying that your variables actually contain nothing before you pull it from the DB and then they do contain something afterwards..

The only other thing I can think of would be a scope issue, but you don't use any functions in there.

Info: PHP Manual


#5 avo

avo
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 13 June 2006 - 06:38 PM

[!--quoteo(post=383407:date=Jun 13 2006, 07:28 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Jun 13 2006, 07:28 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Well, here's something I don't understand: when you pull your data out of the database and stuff it into variables, you're using a while loop. The while loop is completely unnecessary when you're only grabbing one record. And if there's more than one record, you're only getting the last one.

I suggest verifying that your variables actually contain nothing before you pull it from the DB and then they do contain something afterwards..

The only other thing I can think of would be a scope issue, but you don't use any functions in there.
[/quote]

Hi THanks

i have now removed the while loop . and echoed the values out before and after

variable is none associated before but filled after

thanks.
Im loving it ........

#6 avo

avo
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 13 June 2006 - 09:08 PM

[!--quoteo(post=383412:date=Jun 13 2006, 07:38 PM:name=avo)--][div class=\'quotetop\']QUOTE(avo @ Jun 13 2006, 07:38 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hi THanks

i have now removed the while loop . and echoed the values out before and after

variable is none associated before but filled after

thanks.
[/quote]
Anyone its drivin me bannanas

all help greatly appriciated
Im loving it ........

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 June 2006 - 09:16 PM

Strip out all the code that is not associated with the problem you're having. (comment it out, or create a new script -- making sure you save your current script) It looks like you might be getting confused with where the variables are being set.

Put in debugging statements where ever you think there could be a problem.

This way you can debug a smaller piece of code.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users