antbates1991 Posted February 25, 2014 Share Posted February 25, 2014 I'm trying to reuse a variable. For example a query will echo out a row from the database. This is then echoed into a form. The values from the form are then stored into variables. When I echo out the variables in the same if statement as they are stored in, everything is fine. But when I try to use them outside of the if statement they are blank. Any ideas? Here is the code. <?php $newrecord = "Your viewing was added."; if (isset($_POST['submitpostcode'])) { $SearchPostCode = $_POST['SearchPostCode']; $error; $checkpostcode = "SELECT * from PropertyTable WHERE PostCode = '$SearchPostCode'"; $resultpostcode = mysql_query($checkpostcode); $count = mysql_num_rows($resultpostcode); if ($count == 0) { $_POST["SearchPostCode"] = $SearchPostCode; echo 'Sorry no properties were found with this Post Code. Please check it is entered correctly, then try again.'; } else { //show options $selectproperty ="SELECT * from PropertyTable WHERE PostCode = '$SearchPostCode'"; $propertyresults = mysql_query($selectproperty); echo"<div class='page-restrict-output'>"; echo "<form method='post' action='add-viewing'>"; echo "<input id='selectpropertybtn' type='Submit' name='selectpropertybtn' value='Select Property' /><br><br /></td></tr>"; echo "Here are the results, Select only ONE property or the operation will FAIL. <br>"; echo "<br>"; echo"<table id='selectviewingtable' border='1'>"; echo "<tr><th>Property Name</th><th>Post Code</th><th>Property ID</th><th>Add Viewing</th></tr>"; while($row = mysql_fetch_array($propertyresults)) { echo "<tr><td>"; echo $row['PropertyName']; echo "</td><td>"; echo $row['PostCode']; echo "</td><td>"; echo $row['ProductID']; echo "</td><td>"; echo $row['SelectViewing']; echo "<input id='selectviewingbutton' type='checkbox' name='selectedProductIDS[]' value='$row[ProductID]' /></td></tr>"; echo" </form>"; mysql_real_escape_string($PostCode); mysql_real_escape_string($newrecord); } } } if (isset($_POST['selectpropertybtn'])) { $_POST['selectedProductIDS']; { echo "<form action='add-viewing' autocomplete='on' method='post'>"; foreach($_POST['selectedProductIDS'] as $PropertyIDforviewing){ echo "<br>"; echo "You have Selected the following<br>"; echo "<br>"; $getpropertydetails = "SELECT * FROM PropertyTable Where ProductID = '$PropertyIDforviewing'"; $resultgetpropertydetails = mysql_query($getpropertydetails); $rowproperty = mysql_fetch_assoc($resultgetpropertydetails); $count = mysql_num_rows($resultgetpropertydetails); $ProductID = $_GET['ProductID']; $PostCode = $_GET['PostCode']; $PropertyName = $_GET['PropertyName']; } echo "<label for='ProductID'>Property ID*:</label><br>"; echo "<input id='ProductID' type='text' maxlength='20' name='ProductID' value='$rowproperty[ProductID]' readonly /><br>"; echo "<label for='PropertyName'>Property Name*:</label><br>"; echo "<input id='PropertyName' type='text' maxlength='20' name='PropertyName' value='$rowproperty[PropertyName]' readonly /><br>"; echo "<label for='PostCode'>Post Code*:</label><br>"; echo "<input id='PostCode' type='text' maxlength='20' name='PostCode' value='$rowproperty[PostCode]' readonly /><br>"; echo "<br>"; echo "Please Enter Your Employee ID Number."; echo "<br>"; echo "<br>"; echo "<label for='addEmployeeID'>Employee ID*:</label><br>"; echo "<input id='addEmployeeID' type='number' name='addEmployeeID' required /><br>"; echo "<br>"; echo "<input id='addemployeebtn' type='submit' name='addemployeebtn' value='Add Employee' /><br>"; echo "</form>"; $ProductID ='$rowproperty[ProductID]' ; $PropertyName = '$rowproperty[PropertyName]'; $PostCode = '$rowproperty[PostCode]'; } } if (isset($_POST['addemployeebtn'])) { $addEmployeeID = $_POST['addEmployeeID']; { echo "<form action='add-viewing' autocomplete='on' method='post'>"; echo "You have Selected Employee Number $addEmployeeID<br>"; $selectemployee = "SELECT * from EmployeeTable WHERE EmployeeID = '$addEmployeeID'"; $resultselectemployee = mysql_query($selectemployee); $rowemployee = mysql_fetch_assoc($resultselectemployee); $count = mysql_num_rows($resultselectemployee); echo "</form>"; if ($count == 0) { $_POST["addEmployeeID"] = $addEmployeeID; echo 'Sorry no employees were found with this ID Number. Please check it is entered correctly, then try again.'; } else { $EmployeeID = $_GET['EmployeeID']; $EmpFirstName = $_GET['FirstName']; $EmpSecondName = $_GET['SecondName']; { echo "<label for='EmployeeID'>Employee ID*:</label><br>"; echo "<input id='EmployeeID' type='text' maxlength='20' name='ProductID' value='$rowemployee[EmployeeID]' readonly /><br>"; echo "<label for='FirstName'>First Name*:</label><br>"; echo "<input id='FirstName' type='text' maxlength='20' name='FirstName' value='$rowemployee[FirstName]' readonly /><br>"; echo "<label for='SecondName'>Second Name*:</label><br>"; echo "<input id='SecondName' type='text' maxlength='20' name='SecondName' value='$rowemployee[SecondName]' readonly /><br>"; echo "<br>"; echo "<form action='add-viewing' autocomplete='on' method='post'>"; echo "<br>"; echo "Please enter a Customers First Name and Second Name to search for them, alternatively press the Show All Customers button to view the whole customer base across the system. <br>"; echo "<br>"; echo "<label for='SearchFirstName'>First Name*:</label><br>"; echo "<input id='SearchFirstName' type='text' maxlength='20' name='SearchFirstName' /><br>"; echo "<br>"; echo "<label for='SearchSecondName'>Second Name*:</label><br>"; echo "<input id='SearchSecondName' type='text' maxlength='20' name='SearchSecondName'/><br>"; echo "<br>"; echo "<input id='Submit' type='submit' name='searchcustomerbtn' value='Search' /><br>"; echo "<br>"; echo "<input id='Submit' type='submit' name='showcataloguecustomers' value='Show All Customers' /><br>"; $EmployeeID = '$rowemployee[EmployeeID]'; $EmpFirstName = '$rowemployee[FirstName]'; $EmpSecondName ='$rowemployee[SecondName]'; } } } } if (isset($_POST['searchcustomerbtn'])) { $SearchFirstName = $_POST['SearchFirstName']; $SearchSecondName = $_POST['SearchSecondName']; $error; $checkcustomer = "SELECT * from CustomerTable WHERE FirstName = '$SearchFirstName' AND SecondName = '$SearchSecondName'"; $resultcustomer = mysql_query($checkcustomer); $count = mysql_num_rows($resultcustomer); if ($count == 0) { $_POST["SearchFirstName"] = $SearchFirstName; $_POST["SearchSecondName"] = $SearchSecondName; echo 'Sorry no customers were found with these combinations of names. Please check they are entered correctly, then try again.'; } else { //show options $selectcustomer = "SELECT * from CustomerTable WHERE FirstName = '$SearchFirstName' AND SecondName = '$SearchSecondName'"; $customerresults = mysql_query($selectcustomer); echo"<div class='page-restrict-output'>"; echo "<form method='post' action='add-viewing'>"; echo "<input id='editcustomerbutton' type='Submit' name='editcustomerbtn' value='Select Customer' /><br><br /></td></tr>"; echo "Here are the results, Select only ONE Customer or the operation will FAIL. <br>"; echo "<br>"; echo"<table id='selectcustomertable' border='1'>"; echo "<tr><th>FirstName</th><th>SecondName</th><th>Address Line 1</th><th>Customer ID</th><th>Phone Number</th><th>E-Mail</th><th>Edit Customer</th></tr>"; while($rowcustomer = mysql_fetch_array($customerresults)) { echo "<tr><td>"; echo $rowcustomer['FirstName']; echo "</td><td>"; echo $rowcustomer['SecondName']; echo "</td><td>"; echo $rowcustomer['AddressLine1']; echo "</td><td>"; echo $rowcustomer['CustomerID']; echo "</td><td>"; echo $rowcustomer['PhoneNumber']; echo "</td><td>"; echo $rowcustomer['EMail']; echo "</td><td>"; echo $rowcustomer['EditCustomer']; echo "<input id='selectcustomerbutton' type='checkbox' name='selectedCustomerIDS[]' value='$rowcustomer[CustomerID]' /></td></tr>"; echo "</form>"; echo "</div>"; } } } if (isset($_POST['showcataloguecustomers'])) { $showallcustomers = "SELECT * from CustomerTable"; $allcustomerresults = mysql_query($showallcustomers); echo"<div class='page-restrict-output'>"; echo "<form method='post' action='add-viewing'>"; echo "<input id='editcustomerbutton' type='Submit' name='editcustomerbtn' value='Select Customer' /><br><br /></td></tr>"; echo "Here are the results, Select only ONE Customer to edit or the operation will FAIL. <br>"; echo "<br>"; echo"<table id='selectcustomertable' border='1'>"; echo "<tr><th>FirstName</th><th>SecondName</th><th>Address Line 1</th><th>Address Line 2</th><th>Address Line 3</th><th>Post Code</th><th>Customer ID</th><th>Phone Number</th><th>E-Mail</th><th>Edit Customer</th></tr>"; while($rowcustomer = mysql_fetch_array($allcustomerresults)) { echo "<tr><td>"; echo $rowcustomer['FirstName']; echo "</td><td>"; echo $rowcustomer['SecondName']; echo "</td><td>"; echo $rowcustomer['AddressLine1']; echo "</td><td>"; echo $rowcustomer['AddressLine2']; echo "</td><td>"; echo $rowcustomer['AddressLine3']; echo "</td><td>"; echo $rowcustomer['PostCode']; echo "</td><td>"; echo $rowcustomer['CustomerID']; echo "</td><td>"; echo $rowcustomer['PhoneNumber']; echo "</td><td>"; echo $rowcustomer['EMail']; echo "</td><td>"; echo $rowcustomer['EditCustomer']; echo "<input id='selectcustomerbutton' type='checkbox' name='selectedCustomerIDS[]' value='$rowcustomer[CustomerID]' /></td></tr>"; echo "</form>"; echo "</div>"; } } if (isset($_POST['editcustomerbtn'])) { $_POST['selectedCustomerIDS']; echo "<form id=alleditcustomerform action='add-viewing' autocomplete='on' method='post' >"; echo "<br>"; foreach($_POST['selectedCustomerIDS'] as $CustomerIDforedit){ $theselectedrowcustomer = "SELECT * from CustomerTable WHERE CustomerID = '$CustomerIDforedit'"; $theselectedrowcustomerresults = mysql_query($theselectedrowcustomer); $rowcustomer = mysql_fetch_assoc($theselectedrowcustomerresults); $count = mysql_num_rows($theselectedrowcustomerresults); } if ($count == 1) { $CustomerID = $_GET['CustomerID']; $FirstName = $_GET['FirstName']; $SecondName = $_GET['SecondName']; $PhoneNumber = $_GET['PhoneNumber']; echo "<form id=alleditcustomerform action='add-viewing' autocomplete='on' method='post' >"; echo "You have selected the following details.<br>"; echo "<br>"; echo "<label for='CustomerID'>Customer ID*:</label><br>"; echo "<input id='CustomerID' type='text' maxlength='20' name='CustomerID' value='$rowcustomer[CustomerID]' readonly /><br>"; echo "<label for='FirstName'>First Name*:</label><br>"; echo "<input id='FirstName' type='text' maxlength='20' name='FirstName' value='$rowcustomer[FirstName]' pattern='^[a-zA-Z]{2,20}$' required readonly/><br>"; echo "<label for='SecondName'>Second Name*:</label><br>"; echo "<input id='SecondName' type='text' maxlength='20' name='SecondName' value='$rowcustomer[SecondName]' pattern='^[a-zA-Z]{2,20}$' required readonly/><br>"; echo "<label for='PhoneNumber'>Phone Number*:</label><br>"; echo "<input id='PhoneNumber' type='tel' maxlength='11' name='PhoneNumber' value='$rowcustomer[PhoneNumber]' pattern='^\s*\(?(020[7,8]{1}\)?[ ]?[1-9]{1}[0-9{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3}\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\s*$' required readonly/><br>"; echo "<br>"; echo "<input id='addcustomertoviewing' type='submit' name='addcustomertoviewing' value='Add Customer' /><br>"; $CustomerID = '$rowcustomer[CustomerID]'; $FirstName = '$rowcustomer[FirstName]'; $SecondName = '$rowcustomer[SecondName]'; $PhoneNumber = '$rowcustomer[PhoneNumber]'; echo "</form>"; } } if (isset($_POST['addcustomertoviewing'])) { $CustomerID = $_GET['CustomerID']; $FirstName = $_GET['FirstName']; $SecondName = $_GET['SecondName']; $PhoneNumber = $_GET['PhoneNumber']; $EmployeeID = $_GET['EmployeeID']; $EmpFirstName = $_GET['FirstName']; $EmpSecondName = $_GET['SecondName']; $ProductID = $_GET['ProductID']; $PostCode = $_GET['PostCode']; $PropertyName = $_GET['PropertyName']; { echo "Please add a date and time for the viewing.<br>"; echo "<form id=alleditcustomerform action='add-viewing' autocomplete='on' method='post' >"; echo "<label for='CustomerID'>Customer ID*:</label><br>"; echo "<input id='CustomerID' type='text' maxlength='20' name='CustomerID' value='$CustomerID' readonly /><br>"; echo "<label for='FirstName'>First Name*:</label><br>"; echo "<input id='FirstName' type='text' maxlength='20' name='FirstName' value='$FirstName' pattern='^[a-zA-Z]{2,20}$' required readonly/><br>"; echo "<label for='SecondName'>Second Name*:</label><br>"; echo "<input id='SecondName' type='text' maxlength='20' name='SecondName' value='$SecondName' pattern='^[a-zA-Z]{2,20}$' required readonly/><br>"; echo "<label for='PhoneNumber'>Phone Number*:</label><br>"; echo "<input id='PhoneNumber' type='tel' maxlength='11' name='PhoneNumber' value='$PhoneNumber' pattern='^\s*\(?(020[7,8]{1}\)?[ ]?[1-9]{1}[0-9{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3}\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\s*$' required readonly/><br>"; echo "<label for='EmployeeID'>Employee ID*:</label><br>"; echo "<input id='EmployeeID' type='text' maxlength='20' name='ProductID' value='$EmployeeID' readonly /><br>"; echo "<label for='FirstName'>First Name*:</label><br>"; echo "<input id='FirstName' type='text' maxlength='20' name='FirstName' value='$EmpFirstName' readonly /><br>"; echo "<label for='SecondName'>Second Name*:</label><br>"; echo "<input id='SecondName' type='text' maxlength='20' name='SecondName' value='$EmpSecondName' readonly /><br>"; echo "<label for='ProductID'>Property ID*:</label><br>"; echo "<input id='ProductID' type='text' maxlength='20' name='ProductID' value='$ProductID' readonly /><br>"; echo "<label for='PropertyName'>Property Name*:</label><br>"; echo "<input id='PropertyName' type='text' maxlength='20' name='PropertyName' value='$PropertyName' readonly /><br>"; echo "<label for='PostCode'>Post Code*:</label><br>"; echo "<input id='PostCode' type='text' maxlength='20' name='PostCode' value='$PostCode' readonly /><br>"; echo "<label for='Date'>Date*:</label><br>"; echo "<input id='Date' type='date' maxlength='10' name='Date' value='Enter a Date' required /><br>"; echo "<label for='Time'>Time*:</label><br>"; echo "<input id='Time' type='time' maxlength='6' name='PostCode' value='Enter a Time' required /><br>"; echo "<br>"; echo "<input id='addcompletedviewing' type='submit' name='addcompletedviewing' value='Store Viewing' /><br>"; echo "</form>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/286520-reusing-a-variable-but-the-variable-is-empty/ Share on other sites More sharing options...
ginerjm Posted February 25, 2014 Share Posted February 25, 2014 Way too much code to go thru. Besides - you have some very strange php lines in this. What does this line do: $error And what do these lines do: mysql_real_escape_string($PostCode); mysql_real_escape_string($newrecord); That is certainly the syntax for that function. How about posting the part that is giving you the problem? Do you think a lot of people are going to wade thru your complete set of code to help you find one little problem? Quote Link to comment https://forums.phpfreaks.com/topic/286520-reusing-a-variable-but-the-variable-is-empty/#findComment-1470624 Share on other sites More sharing options...
antbates1991 Posted February 25, 2014 Author Share Posted February 25, 2014 (edited) Way too much code to go thru. Besides - you have some very strange php lines in this. What does this line do: $error And what do these lines do: mysql_real_escape_string($PostCode); mysql_real_escape_string($newrecord); That is certainly the syntax for that function. How about posting the part that is giving you the problem? Do you think a lot of people are going to wade thru your complete set of code to help you find one little problem? The strange lines are nothing I've deleted them. Just little bits left over from messing around with a few things. Ok its this bit that is giving me the problem. if (isset($_POST['addcustomertoviewing'])) { $CustomerID = $_GET['CustomerID']; $FirstName = $_GET['FirstName']; $SecondName = $_GET['SecondName']; $PhoneNumber = $_GET['PhoneNumber']; $EmployeeID = $_GET['EmployeeID']; $EmpFirstName = $_GET['FirstName']; $EmpSecondName = $_GET['SecondName']; $ProductID = $_GET['ProductID']; $PostCode = $_GET['PostCode']; $PropertyName = $_GET['PropertyName']; { echo "Please add a date and time for the viewing.<br>"; echo "<br>"; echo "<form id=alleditcustomerform action='add-viewing' autocomplete='on' method='post' >"; echo "<label for='CustomerID'>Customer ID*:</label><br>"; echo "<input id='CustomerID' type='text' maxlength='20' name='CustomerID' value='$CustomerID' readonly /><br>"; echo "<label for='FirstName'>First Name*:</label><br>"; echo "<input id='FirstName' type='text' maxlength='20' name='FirstName' value='$FirstName' pattern='^[a-zA-Z]{2,20}$' required readonly/><br>"; echo "<label for='SecondName'>Second Name*:</label><br>"; echo "<input id='SecondName' type='text' maxlength='20' name='SecondName' value='$SecondName' pattern='^[a-zA-Z]{2,20}$' required readonly/><br>"; echo "<label for='PhoneNumber'>Phone Number*:</label><br>"; echo "<input id='PhoneNumber' type='tel' maxlength='11' name='PhoneNumber' value='$PhoneNumber' pattern='^\s*\(?(020[7,8]{1}\)?[ ]?[1-9]{1}[0-9{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3}\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\s*$' required readonly/><br>"; echo "<label for='EmployeeID'>Employee ID*:</label><br>"; echo "<input id='EmployeeID' type='text' maxlength='20' name='ProductID' value='$EmployeeID' readonly /><br>"; echo "<label for='FirstName'>First Name*:</label><br>"; echo "<input id='FirstName' type='text' maxlength='20' name='FirstName' value='$EmpFirstName' readonly /><br>"; echo "<label for='SecondName'>Second Name*:</label><br>"; echo "<input id='SecondName' type='text' maxlength='20' name='SecondName' value='$EmpSecondName' readonly /><br>"; echo "<label for='ProductID'>Property ID*:</label><br>"; echo "<input id='ProductID' type='text' maxlength='20' name='ProductID' value='$ProductID' readonly /><br>"; echo "<label for='PropertyName'>Property Name*:</label><br>"; echo "<input id='PropertyName' type='text' maxlength='20' name='PropertyName' value='$PropertyName' readonly /><br>"; echo "<label for='PostCode'>Post Code*:</label><br>"; echo "<input id='PostCode' type='text' maxlength='20' name='PostCode' value='$PostCode' readonly /><br>"; echo "<label for='Date'>Date*:</label><br>"; echo "<input id='Date' type='date' maxlength='10' name='Date' value='Enter a Date' required /><br>"; echo "<label for='Time'>Time*:</label><br>"; echo "<input id='Time' type='time' maxlength='6' name='PostCode' value='Enter a Time' required /><br>"; echo "<br>"; echo "<input id='addcompletedviewing' type='submit' name='addcompletedviewing' value='Store Viewing' /><br>"; echo "</form>"; } } The variables aren't being echoed in the form despite them being set like this. $CustomerID = '$rowcustomer[CustomerID]'; $FirstName = '$rowcustomer[FirstName]'; $SecondName = '$rowcustomer[SecondName]'; $PhoneNumber = '$rowcustomer[PhoneNumber]'; $EmployeeID = '$rowemployee[EmployeeID]'; $EmpFirstName = '$rowemployee[FirstName]'; $EmpSecondName ='$rowemployee[SecondName]'; $ProductID ='$rowproperty[ProductID]' ; $PropertyName = '$rowproperty[PropertyName]'; $PostCode = '$rowproperty[PostCode]'; Edited February 25, 2014 by antbates1991 Quote Link to comment https://forums.phpfreaks.com/topic/286520-reusing-a-variable-but-the-variable-is-empty/#findComment-1470628 Share on other sites More sharing options...
ginerjm Posted February 25, 2014 Share Posted February 25, 2014 Your assignment of those variables seems flawed to me. $ProductID ='$rowproperty[ProductID]' ; is not going to assign the value of the row var, but rather the string: '$rowproperty[ProductID]' which is itself a flawed array reference. The proper form is $ProductID = $row[roperty['ProductID']; I noticed that your code begins by retrieving an awful lot of GET vars, but then your form clearly uses the POST method. Could this be part of your problem? Plus - using a GET for all that data is kind of risky. Quote Link to comment https://forums.phpfreaks.com/topic/286520-reusing-a-variable-but-the-variable-is-empty/#findComment-1470629 Share on other sites More sharing options...
Ch0cu3r Posted February 25, 2014 Share Posted February 25, 2014 Remove the single quotes. Variables are not expanded in single quotes. $CustomerID = $rowcustomer['CustomerID']; $FirstName = $rowcustomer['FirstName']; $SecondName = $rowcustomer['SecondName']; $PhoneNumber = $rowcustomer['PhoneNumber]; Quote Link to comment https://forums.phpfreaks.com/topic/286520-reusing-a-variable-but-the-variable-is-empty/#findComment-1470630 Share on other sites More sharing options...
antbates1991 Posted February 25, 2014 Author Share Posted February 25, 2014 The code now looks like this $CustomerID = $rowcustomer['CustomerID']; $FirstName = $rowcustomer['FirstName']; $SecondName = $rowcustomer['SecondName']; $PhoneNumber = $rowcustomer['PhoneNumber']; $EmployeeID = $rowemployee['EmployeeID']; $EmpFirstName = $rowemployee['FirstName']; $EmpSecondName =$rowemployee['SecondName']; $ProductID = $rowproperty['ProductID']; $PropertyName = $rowproperty['PropertyName']; $PostCode = $rowproperty['PostCode']; But the form values are still empty. Quote Link to comment https://forums.phpfreaks.com/topic/286520-reusing-a-variable-but-the-variable-is-empty/#findComment-1470636 Share on other sites More sharing options...
ginerjm Posted February 25, 2014 Share Posted February 25, 2014 Can we see the code that follows those corrected assignments? Quote Link to comment https://forums.phpfreaks.com/topic/286520-reusing-a-variable-but-the-variable-is-empty/#findComment-1470637 Share on other sites More sharing options...
antbates1991 Posted February 25, 2014 Author Share Posted February 25, 2014 (edited) Yeah here is the one for customer. They are all identical. foreach($_POST['selectedCustomerIDS'] as $CustomerIDforedit){ $theselectedrowcustomer = "SELECT * from CustomerTable WHERE CustomerID = '$CustomerIDforedit'"; $theselectedrowcustomerresults = mysql_query($theselectedrowcustomer); $rowcustomer = mysql_fetch_assoc($theselectedrowcustomerresults); $count = mysql_num_rows($theselectedrowcustomerresults); } if ($count == 1) { $CustomerID = $_GET['CustomerID']; $FirstName = $_GET['FirstName']; $SecondName = $_GET['SecondName']; $PhoneNumber = $_GET['PhoneNumber']; echo "<form id=alleditcustomerform action='add-viewing' autocomplete='on' method='post' >"; echo "You have selected the following details.<br>"; echo "<br>"; echo "<label for='CustomerID'>Customer ID*:</label><br>"; echo "<input id='CustomerID' type='text' maxlength='20' name='CustomerID' value='$rowcustomer[CustomerID]' readonly /><br>"; echo "<label for='FirstName'>First Name*:</label><br>"; echo "<input id='FirstName' type='text' maxlength='20' name='FirstName' value='$rowcustomer[FirstName]' pattern='^[a-zA-Z]{2,20}$' required readonly/><br>"; echo "<label for='SecondName'>Second Name*:</label><br>"; echo "<input id='SecondName' type='text' maxlength='20' name='SecondName' value='$rowcustomer[SecondName]' pattern='^[a-zA-Z]{2,20}$' required readonly/><br>"; echo "<label for='PhoneNumber'>Phone Number*:</label><br>"; echo "<input id='PhoneNumber' type='tel' maxlength='11' name='PhoneNumber' value='$rowcustomer[PhoneNumber]' pattern='^\s*\(?(020[7,8]{1}\)?[ ]?[1-9]{1}[0-9{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3}\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\s*$' required readonly/><br>"; echo "<br>"; echo "<input id='addcustomertoviewing' type='submit' name='addcustomertoviewing' value='Add Customer' /><br>"; $CustomerID = $rowcustomer['CustomerID']; $FirstName = $rowcustomer['FirstName']; $SecondName = $rowcustomer['SecondName']; $PhoneNumber = $rowcustomer['PhoneNumber']; echo "</form>"; } } if (isset($_POST['addcustomertoviewing'])) { $CustomerID = $_GET['CustomerID']; $FirstName = $_GET['FirstName']; $SecondName = $_GET['SecondName']; $PhoneNumber = $_GET['PhoneNumber']; $EmployeeID = $_GET['EmployeeID']; $EmpFirstName = $_GET['FirstName']; $EmpSecondName = $_GET['SecondName']; $ProductID = $_GET['ProductID']; $PostCode = $_GET['PostCode']; $PropertyName = $_GET['PropertyName']; { echo "Please add a date and time for the viewing.<br>"; echo "<br>"; echo "<form id=alleditcustomerform action='add-viewing' autocomplete='on' method='post' >"; echo "<label for='CustomerID'>Customer ID*:</label><br>"; echo "<input id='CustomerID' type='text' maxlength='20' name='CustomerID' value='$CustomerID' readonly /><br>"; echo "<label for='FirstName'>First Name*:</label><br>"; echo "<input id='FirstName' type='text' maxlength='20' name='FirstName' value='$FirstName' pattern='^[a-zA-Z]{2,20}$' required readonly/><br>"; echo "<label for='SecondName'>Second Name*:</label><br>"; echo "<input id='SecondName' type='text' maxlength='20' name='SecondName' value='$SecondName' pattern='^[a-zA-Z]{2,20}$' required readonly/><br>"; echo "<label for='PhoneNumber'>Phone Number*:</label><br>"; echo "<input id='PhoneNumber' type='tel' maxlength='11' name='PhoneNumber' value='$PhoneNumber' pattern='^\s*\(?(020[7,8]{1}\)?[ ]?[1-9]{1}[0-9{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3}\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\s*$' required readonly/><br>"; echo "<label for='EmployeeID'>Employee ID*:</label><br>"; echo "<input id='EmployeeID' type='text' maxlength='20' name='ProductID' value='$EmployeeID' readonly /><br>"; echo "<label for='FirstName'>First Name*:</label><br>"; echo "<input id='FirstName' type='text' maxlength='20' name='FirstName' value='$EmpFirstName' readonly /><br>"; echo "<label for='SecondName'>Second Name*:</label><br>"; echo "<input id='SecondName' type='text' maxlength='20' name='SecondName' value='$EmpSecondName' readonly /><br>"; echo "<label for='ProductID'>Property ID*:</label><br>"; echo "<input id='ProductID' type='text' maxlength='20' name='ProductID' value='$rowproperty[ProductID]' readonly /><br>"; echo "<label for='PropertyName'>Property Name*:</label><br>"; echo "<input id='PropertyName' type='text' maxlength='20' name='PropertyName' value='$PropertyName' readonly /><br>"; echo "<label for='PostCode'>Post Code*:</label><br>"; echo "<input id='PostCode' type='text' maxlength='20' name='PostCode' value='$PostCode' readonly /><br>"; echo "<label for='Date'>Date*:</label><br>"; echo "<input id='Date' type='date' maxlength='10' name='Date' value='Enter a Date' required /><br>"; echo "<label for='Time'>Time*:</label><br>"; echo "<input id='Time' type='time' maxlength='6' name='PostCode' value='Enter a Time' required /><br>"; echo "<br>"; echo "<input id='addcompletedviewing' type='submit' name='addcompletedviewing' value='Store Viewing' /><br>"; echo "</form>"; } } Edited February 25, 2014 by antbates1991 Quote Link to comment https://forums.phpfreaks.com/topic/286520-reusing-a-variable-but-the-variable-is-empty/#findComment-1470638 Share on other sites More sharing options...
ginerjm Posted February 25, 2014 Share Posted February 25, 2014 While this code doesn't appear to be what I asked for, making it hard to piece together what is going on, my guess is that you do a query to populate $rowcustomer but then you set those vars using the GET array which may or may not have data and then you output those vars which may not have anything in them. Why are you using the GET array? Quote Link to comment https://forums.phpfreaks.com/topic/286520-reusing-a-variable-but-the-variable-is-empty/#findComment-1470639 Share on other sites More sharing options...
antbates1991 Posted February 25, 2014 Author Share Posted February 25, 2014 (edited) You're right I populate $rowcustomer using a query. The values are then placed into a form like this. This value shows up as it should. value='$rowcustomer[CustomerID]' I then take this value and place it into a variable using $CustomerID = $rowcustomer['CustomerID']; Or at least thats what I'm trying to do. Once I've done this I can then use the variable later on to echo the value out into the other form. What this the best way to do this as obviously something is wrong somewhere. Edited February 25, 2014 by antbates1991 Quote Link to comment https://forums.phpfreaks.com/topic/286520-reusing-a-variable-but-the-variable-is-empty/#findComment-1470641 Share on other sites More sharing options...
ginerjm Posted February 25, 2014 Share Posted February 25, 2014 (edited) But that is not what we are seeing. How about showing us ONE simple set of code that all goes together where we can see what you are saying is happening? AND you still haven't answered the question concerning why you are using the GET array. Edited February 25, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/286520-reusing-a-variable-but-the-variable-is-empty/#findComment-1470643 Share on other sites More sharing options...
ginerjm Posted February 25, 2014 Share Posted February 25, 2014 (edited) $CustomerID = $_GET['CustomerID']; $FirstName = $_GET['FirstName']; $SecondName = $_GET['SecondName']; $PhoneNumber = $_GET['PhoneNumber']; echo "<form id=alleditcustomerform action='add-viewing' autocomplete='on' method='post' >"; echo "You have selected the following details.<br>"; echo "<br>"; echo "<label for='CustomerID'>Customer ID*:</label><br>"; echo "<input id='CustomerID' type='text' maxlength='20' name='CustomerID' value='$rowcustomer[CustomerID]' readonly /><br>"; echo "<label for='FirstName'>First Name*:</label><br>"; echo "<input id='FirstName' type='text' maxlength='20' name='FirstName' value='$rowcustomer[FirstName]' pattern='^[a-zA-Z]{2,20}$' required readonly/><br>"; echo "<label for='SecondName'>Second Name*:</label><br>"; echo "<input id='SecondName' type='text' maxlength='20' name='SecondName' value='$rowcustomer[SecondName]' pattern='^[a-zA-Z]{2,20}$' required readonly/><br>"; echo "<label for='PhoneNumber'>Phone Number*:</label><br>"; echo "<input id='PhoneNumber' type='tel' maxlength='11' name='PhoneNumber' value='$rowcustomer[PhoneNumber]' pattern='^\s*\(?(020[7,8]{1}\)?[ ]?[1-9]{1}[0-9{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3}\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\s*$' required readonly/><br>"; echo "<br>"; echo "<input id='addcustomertoviewing' type='submit' name='addcustomertoviewing' value='Add Customer' /><br>"; $CustomerID = $rowcustomer['CustomerID']; $FirstName = $rowcustomer['FirstName']; $SecondName = $rowcustomer['SecondName']; $PhoneNumber = $rowcustomer['PhoneNumber']; echo "</form>"; } Ok - I pulled some of your code out to make this point. At the top of this code you use the GET array to set some variables but then you use the query results to display the in the form. Then you set those local vars with the query results but you aren't displaying them anywhere. Edited February 25, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/286520-reusing-a-variable-but-the-variable-is-empty/#findComment-1470644 Share on other sites More sharing options...
antbates1991 Posted February 25, 2014 Author Share Posted February 25, 2014 OKay I see what you mean now. I've gotten rid of the GET array because it isn't doing anything. if (isset($_POST['editcustomerbtn'])) { $_POST['selectedCustomerIDS']; echo "<form id=alleditcustomerform action='add-viewing' autocomplete='on' method='post' >"; echo "<br>"; foreach($_POST['selectedCustomerIDS'] as $CustomerIDforedit){ $theselectedrowcustomer = "SELECT * from CustomerTable WHERE CustomerID = '$CustomerIDforedit'"; $theselectedrowcustomerresults = mysql_query($theselectedrowcustomer); $rowcustomer = mysql_fetch_assoc($theselectedrowcustomerresults); $count = mysql_num_rows($theselectedrowcustomerresults); // thats the query I run to populate $rowcustomer } if ($count == 1) { // the values are then echoed out into this form and it is these I want to store. echo "<form id=alleditcustomerform action='add-viewing' autocomplete='on' method='post' >"; echo "You have selected the following details.<br>"; echo "<br>"; echo "<label for='CustomerID'>Customer ID*:</label><br>"; echo "<input id='CustomerID' type='text' maxlength='20' name='CustomerID' value='$rowcustomer[CustomerID]' readonly /><br>"; echo "<label for='FirstName'>First Name*:</label><br>"; echo "<input id='FirstName' type='text' maxlength='20' name='FirstName' value='$rowcustomer[FirstName]' pattern='^[a-zA-Z]{2,20}$' required readonly/><br>"; echo "<label for='SecondName'>Second Name*:</label><br>"; echo "<input id='SecondName' type='text' maxlength='20' name='SecondName' value='$rowcustomer[SecondName]' pattern='^[a-zA-Z]{2,20}$' required readonly/><br>"; echo "<label for='PhoneNumber'>Phone Number*:</label><br>"; echo "<input id='PhoneNumber' type='tel' maxlength='11' name='PhoneNumber' value='$rowcustomer[PhoneNumber]' pattern='^\s*\(?(020[7,8]{1}\)?[ ]?[1-9]{1}[0-9{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3}\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\s*$' required readonly/><br>"; echo "<br>"; echo "<input id='addcustomertoviewing' type='submit' name='addcustomertoviewing' value='Add Customer' /><br>"; // This is how I'm a trying to store the value in each form field into a Variable. $CustomerID = $rowcustomer['CustomerID']; $FirstName = $rowcustomer['FirstName']; $SecondName = $rowcustomer['SecondName']; $PhoneNumber = $rowcustomer['PhoneNumber']; echo "</form>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/286520-reusing-a-variable-but-the-variable-is-empty/#findComment-1470645 Share on other sites More sharing options...
ginerjm Posted February 25, 2014 Share Posted February 25, 2014 (edited) Ok - so you echo out one form with the first row of data and you save the values in local vars. Now what do you do? YOu do realize that those vars will be gone once you exit this script to show the user the form? Oh - and BTW - this form has another of those stupid fragments of a line in it, so I am doubtful that you have ever run this code. Edited February 25, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/286520-reusing-a-variable-but-the-variable-is-empty/#findComment-1470646 Share on other sites More sharing options...
antbates1991 Posted February 25, 2014 Author Share Posted February 25, 2014 (edited) I didn't know that. What would be the best way to save these values so I can use them later? Edited February 25, 2014 by antbates1991 Quote Link to comment https://forums.phpfreaks.com/topic/286520-reusing-a-variable-but-the-variable-is-empty/#findComment-1470647 Share on other sites More sharing options...
ginerjm Posted February 25, 2014 Share Posted February 25, 2014 Save them as session vars. Or since you put them out to the form for the user to read, you can grab them from the form again when the user submits it, if you first make sure that the form is valid. Quote Link to comment https://forums.phpfreaks.com/topic/286520-reusing-a-variable-but-the-variable-is-empty/#findComment-1470650 Share on other sites More sharing options...
antbates1991 Posted February 25, 2014 Author Share Posted February 25, 2014 What would be the correct way of storing the data from value=$'rowcustomer[CustomerID]'; into a Session Var? Ive tried $_POST[$_SESSION["CustomerID"]] = '$rowcustomer[property[CustomerID]]'; and $_POST[$_SESSION["CustomerID"]] ='$rowcustomer[CustomerID]' and in print_r i get [] => $rowcustomer[property[CustomerID]] ) or [] => $rowcustomer[CustomerID] ) Quote Link to comment https://forums.phpfreaks.com/topic/286520-reusing-a-variable-but-the-variable-is-empty/#findComment-1470670 Share on other sites More sharing options...
ginerjm Posted February 25, 2014 Share Posted February 25, 2014 Apparently you know absolutely no php - and what is worse you don't even have the ambition to read up on it to figure things out. Sorry - but I feel no need to help out those who won't help themselves. good by and good luck. Quote Link to comment https://forums.phpfreaks.com/topic/286520-reusing-a-variable-but-the-variable-is-empty/#findComment-1470671 Share on other sites More sharing options...
antbates1991 Posted February 25, 2014 Author Share Posted February 25, 2014 Wow, I'm only asking for a few pointers no need to be rude, and its goodbye. Quote Link to comment https://forums.phpfreaks.com/topic/286520-reusing-a-variable-but-the-variable-is-empty/#findComment-1470676 Share on other sites More sharing options...
Solution antbates1991 Posted February 25, 2014 Author Solution Share Posted February 25, 2014 All that needed changing was $_SESSION["CustomerID"] ='$rowcustomer[CustomerID]'; to $_SESSION["Customer"] = $rowcustomer['CustomerID']; now it works fine. Quote Link to comment https://forums.phpfreaks.com/topic/286520-reusing-a-variable-but-the-variable-is-empty/#findComment-1470687 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.