theghost121
asked on
undefined variable - PHP
Hi Experts,
When trying to export the values from the MSSQL database the values for TRS3 work without error, however TRS1 and 2 both fail
I have attached both the html and php
HTML Code
dbo-export-trs-process.php
When trying to export the values from the MSSQL database the values for TRS3 work without error, however TRS1 and 2 both fail
I have attached both the html and php
Error
Environment: <env>
Notice: Undefined variable: csvname1 in C:\xampp\htdocs\mhs\dbo_export_trs_process.php on line 160
Notice: Undefined variable: csvname2 in C:\xampp\htdocs\mhs\dbo_export_trs_process.php on line 161
Notice: Undefined variable: csvname1 in C:\xampp\htdocs\mhs\dbo_export_trs_process.php on line 169
Notice: Undefined variable: csvname2 in C:\xampp\htdocs\mhs\dbo_export_trs_process.php on line 170
Notice: Undefined variable: csvname1 in C:\xampp\htdocs\mhs\dbo_export_trs_process.php on line 183
Notice: Undefined variable: csvname2 in C:\xampp\htdocs\mhs\dbo_export_trs_process.php on line 184
sqlcmd -S <sqlinstance> -d MHS -E -Q "SET NOCOUNT ON;select Variable, VALUE from Data where Environment = '<env>' and Type = 'TRS1' order by Variable asc" -o "csv\.csv" -W -h-1 -s"," -w 700
sqlcmd -S <sqlinstance> -d MHS -E -Q "SET NOCOUNT ON;select Variable, VALUE from Data where Environment = '<env>' and Type = 'TRS2' order by Variable asc" -o "csv\.csv" -W -h-1 -s"," -w 700
sqlcmd -S <sqlinstance> -d MHS -E -Q "SET NOCOUNT ON;select Variable, VALUE from Data where Environment = '<env>' and Type = 'TRS3' order by Variable asc" -o "csv\<env>-TRS3.csv" -W -h-1 -s"," -w 700
xcopy "\\<fshare>\c$\xampp\htdocs\mhs\csv\.csv" "\\<csvshare>\CSV Files\MHS\<env>\" /Y /V
xcopy "\\<fshare>\c$\xampp\htdocs\mhs\csv\.csv" "\\<csvshare>\CSV Files\MHS\<env>\" /Y /V
xcopy "\\<fshare>\c$\xampp\htdocs\mhs\csv\<env>-TRS3.csv" "\\<csvshare>\CSV Files\MHS\<env>\" /Y /V
IF NOT EXIST \\<fshare>\c$\xampp\htdocs\mhs\logs\<env>\. MKDIR \\<fshare>\c$\xampp\htdocs\mhs\logs\<env>
copy "\\<fshare>\c$\xampp\htdocs\mhs\csv\.csv" "\\<fshare>\c$\xampp\htdocs\mhs\logs\<env>\_12032014_035732.log" /Y /V
copy "\\<fshare>\c$\xampp\htdocs\mhs\csv\.csv" "\\<fshare>\c$\xampp\htdocs\mhs\logs\<env>\_12032014_035732.log" /Y /V
copy "\\<fshare>\c$\xampp\htdocs\mhs\csv\<env>-TRS3.csv" "\\<fshare>\c$\xampp\htdocs\mhs\logs\<env>\<env>-TRS3_12032014_035732.log" /Y /V
HTML Code
<!DOCTYPE html>
<html>
<head>
<title>MHS Configuration Database</title>
<!-- setup bootstrap files and jquery -->
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<script src="jquery/jquery.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
</head>
<h1>MHS Configuration Database <small>ISD Messaging</small></h1>
<p>Use this system to export an environments TRS data</p>
<!-- build the 'main menu' button -->
<a href="dbo_Data_list.php" title="Main Menu" class="btn btn-primary" target="_self"><i class="icon-home icon-white"></i> Main Menu</a>
<form action="dbo_export_trs_process.php" method='post'>
<h3>Select the Environment to export</h3>
<!-- start the setup of the table -->
<table border='0px'>
<thead>
<tr>
<th>Environment</th>
<!-- <th>Type</th> -->
</tr>
</thead>
<tbody>
<tr>
<td>
<?php
//+-================-+
//| connect to a DB |
//+-================-+
$connection_string = 'DRIVER={SQL Server};SERVER=<SQLINSTANCE>;DATABASE=<DB>';
$conn = odbc_connect( $connection_string, '', '' );
if ($conn)
{
// +-================================-+
// | Build the 'Environment' pulldown |
// +-================================-+
$envQuery = "select distinct Environment from DATA where Type like 'TRS%' order by Environment asc";
$envResult = odbc_exec($conn, $envQuery);
$envDropdown = "<select name='Environments'>";
while($row = odbc_fetch_array($envResult)) {
$envDropdown .= "\r\n<option value='{$row['Environment']}'>{$row['Environment']}</option>";
}
$envDropdown .= "\r\n</select>";
// output the pulldown into the table
echo $envDropdown;
echo "</td>";
echo "<td>";
// build Tower pulldown
//$trsDropdown = "<select name='Type'><option value='TRS'>TRS</option></select>";
/*
// output the pulldown into the table
echo $trsDropdown;
echo "</td>";
echo "</tr>";
echo "</tbody>";
echo "</table>";
*/
// close the connection
odbc_close ($conn);
}
else echo "odbc not connected";
?>
<!-- Build the 'submit' button -->
<input type="submit" value="Export" class="btn btn-success"/>
</form>
</body>
</html>
PHP Code<!DOCTYPE html>
<html>
<head>
<title>MHS Configuration Database</title>
<!-- setup bootstrap files and jquery -->
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<script src="jquery/jquery.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<div class="alert alert-success">
<h1>MHS Configuration Database <small>ISD Messaging</small></h1>
<p>Use this system to export an environments config data</p>
<p><?php if(isset($_SESSION['current_user']))
echo '<b>'.'<i class="icon-user"></i>You are logged in as: '.'</b>'.$_SESSION['current_user'];?></p>
<!-- build the 'Main Menu' button -->
<a href="dbo_Data_list.php" title="Main Menu" class="btn btn-primary" target="_self"><i class="icon-home icon-white"></i> Main Menu</a>
<!-- build the 'Previous' button -->
<a href="dbo_export_complete_trs.php" title="Previous" class="btn btn-primary" target="_self"><i class="icon-home icon-white"></i> Previous</a>
</div>
<!-- Output the variables to the screen to check we have the correct values -->
Environment: <?php echo $_POST['Environments']; ?><br />
<br />
<?php
// setup SQL DB Connection
$connection_string = 'DRIVER={SQL Server};SERVER=<SQLINST>;DATABASE=<DB>';
$ssql="<SQLSRV>";
$conn = odbc_connect( $connection_string, '', '' );
$sdb= "<DB>";
$fileshare= "<Share>";
// trim trailing spaces from passed through variables
$env=trim($_POST['Environments']," ");
$loc = "select distinct Value from Data where Variable = 'LOCATION' and Environment='$env'";
$dc=trim($loc," ");
$typeQuery= "select distinct Type from Data where Type >='TRS'";
$type=trim($typeQuery," ");
// get the current time and date for the log
$curr_date = date("dmY_his", time());
// setup strings to use for exporting and copying
// Export values from Database
If ($dc == "MPE")
{
$exportStringTRS1="sqlcmd -S $ssql -d $sdb -E -Q \"SET NOCOUNT ON;select Variable, VALUE from Data where Environment = '%ENV%' and Type = 'TRS1' order by Variable asc\" -o \"csv\%CSVNAME%.csv\" -W -h-1 -s\",\" -w 700";
$exportStringTRS2="sqlcmd -S $ssql -d $sdb -E -Q \"SET NOCOUNT ON;select Variable, VALUE from Data where Environment = '%ENV%' and Type = 'TRS2' order by Variable asc\" -o \"csv\%CSVNAME%.csv\" -W -h-1 -s\",\" -w 700";
$exportStringTRS3="sqlcmd -S $ssql -d $sdb -E -Q \"SET NOCOUNT ON;select Variable, VALUE from Data where Environment = '%ENV%' and Type = 'TRS3' order by Variable asc\" -o \"csv\%CSVNAME%.csv\" -W -h-1 -s\",\" -w 700";
$locationQuery = "select Value from Data where Environment = '$env' and Type = 'TRS1' and Variable = 'LOCATION'";
}
else If ($dc == "ADC")
{
$exportStringTRS1="sqlcmd -S $ssql -d $sdb -E -Q \"SET NOCOUNT ON;select Variable, VALUE from Data where Environment = '%ENV%' and Type = 'TRS1' order by Variable asc\" -o \"csv\%CSVNAME%.csv\" -W -h-1 -s\",\" -w 700";
$exportStringTRS2="sqlcmd -S $ssql -d $sdb -E -Q \"SET NOCOUNT ON;select Variable, VALUE from Data where Environment = '%ENV%' and Type = 'TRS2' order by Variable asc\" -o \"csv\%CSVNAME%.csv\" -W -h-1 -s\",\" -w 700";
$exportStringTRS3="sqlcmd -S $ssql -d $sdb -E -Q \"SET NOCOUNT ON;select Variable, VALUE from Data where Environment = '%ENV%' and Type = 'TRS3' order by Variable asc\" -o \"csv\%CSVNAME%.csv\" -W -h-1 -s\",\" -w 700";
$locationQuery = "select Value from Data where Environment = '$env' and Type = 'TRS1' and Variable = 'LOCATION'";
}
else ($dc == "ODC");
{
$exportStringTRS1="sqlcmd -S $ssql -d $sdb -E -Q \"SET NOCOUNT ON;select Variable, VALUE from Data where Environment = '%ENV%' and Type = 'TRS1' order by Variable asc\" -o \"csv\%CSVNAME%.csv\" -W -h-1 -s\",\" -w 700";
$exportStringTRS2="sqlcmd -S $ssql -d $sdb -E -Q \"SET NOCOUNT ON;select Variable, VALUE from Data where Environment = '%ENV%' and Type = 'TRS2' order by Variable asc\" -o \"csv\%CSVNAME%.csv\" -W -h-1 -s\",\" -w 700";
$exportStringTRS3="sqlcmd -S $ssql -d $sdb -E -Q \"SET NOCOUNT ON;select Variable, VALUE from Data where Environment = '%ENV%' and Type = 'TRS3' order by Variable asc\" -o \"csv\%CSVNAME%.csv\" -W -h-1 -s\",\" -w 700";
$locationQuery = "select Value from Data where Environment = '$env' and Type = 'TRS1' and Variable = 'LOCATION'";
}
$trsString1="xcopy \"\\\\$fileshare\c$\\xampp\htdocs\mhs\csv\%CSVNAME%.csv\" \"\\\%LOCATION%\CSV Files\MHS\%ENV%\\\" /Y /V";
$trsString2="xcopy \"\\\\$fileshare\c$\\xampp\htdocs\mhs\csv\%CSVNAME%.csv\" \"\\\%LOCATION%\CSV Files\MHS\%ENV%\\\" /Y /V";
$trsString3="xcopy \"\\\\$fileshare\c$\\xampp\htdocs\mhs\csv\%CSVNAME%.csv\" \"\\\%LOCATION%\CSV Files\MHS\%ENV%\\\" /Y /V";
$copyToLogs1="copy \"\\\\$fileshare\c$\\xampp\htdocs\mhs\csv\%CSVNAME%.csv\" \"\\\\$fileshare\c$\\xampp\htdocs\mhs\\logs\%ENV%\%CSVNAME%_$curr_date.log\" /Y /V";
$copyToLogs2="copy \"\\\\$fileshare\c$\\xampp\htdocs\mhs\csv\%CSVNAME%.csv\" \"\\\\$fileshare\c$\\xampp\htdocs\mhs\\logs\%ENV%\%CSVNAME%_$curr_date.log\" /Y /V";
$copyToLogs3="copy \"\\\\$fileshare\c$\\xampp\htdocs\mhs\csv\%CSVNAME%.csv\" \"\\\\$fileshare\c$\\xampp\htdocs\mhs\\logs\%ENV%\%CSVNAME%_$curr_date.log\" /Y /V";
$netUseString="net use \\\\%LOCATION% %NETUSERPASS% /user:%NETUSER% /persistent:no";
// execute the query
$locationResult = odbc_exec($conn, $locationQuery);
// grab the value we need
$location = odbc_result($locationResult,1);
//$type=trim($_POST['Location']," ");
$location = trim($location," ");
// check for the location and set values accordingly
if ($location=="MPE")
{
// set MPE information here
$csvShare="<SERVER>";
$netUser="<USER>";
$netUserPass="<PASS>";
}
elseif ($location=="ADC")
{
// set ADC information here
$csvShare="<SERVER>";
$netUser="<USER>";
$netUserPass="<PASS>";
}
elseif ($location=="ODC")
{
// set ODC information here
$csvShare="<SERVER>";
$netUser="<USER>";
$netUserPass="<PASS>";
}
// TRS CSV File create
if ($type =="TRS1" && $location==="ODC")
{
$csvname1="%ENV%-TRS1";
}
else if ($type =="TRS2" && $location==="ODC")
{
$csvname2="%ENV%-TRS2";
}
else if ($type =="TRS3" && $location==="ODC")
{
$csvname3="%ENV%-TRS3";
}
else if ($type =="TRS1" && $location==="ADC")
{
$csvname1="%ENV%-TRS1";
}
else if ($type =="TRS2" && $location==="ADC")
{
$csvname2="%ENV%-TRS2";
}
else if ($type =="TRS3" && $location==="ADC")
{
$csvname3="%ENV%-TRS3";
}
else if ($type =="TRS1" && $location==="MPE")
{
$csvname1="%ENV%-TRS1";
}
else if ($type =="TRS2" && $location==="MPE")
{
$csvname2="%ENV%-TRS2";
}
else ($type =="TRS3" && $location==="MPE");
{
$csvname3="%ENV%-TRS3";
}
// find and replace the variables in the export string
$exportStringTRS1=str_replace('%CSVNAME%',$csvname1,$exportStringTRS1); // this has to come first as it uses the %ENV% variable that also needs replacing!
$exportStringTRS2=str_replace('%CSVNAME%',$csvname2,$exportStringTRS2);
$exportStringTRS3=str_replace('%CSVNAME%',$csvname3,$exportStringTRS3);
$exportStringTRS1=str_replace('%ENV%',$env,$exportStringTRS1);
$exportStringTRS2=str_replace('%ENV%',$env,$exportStringTRS2);
$exportStringTRS3=str_replace('%ENV%',$env,$exportStringTRS3);
// find and replace the variables in the copy string
$trsString1=str_replace('%CSVNAME%',$csvname1,$trsString1); // this has to come first as it uses the %ENV% variable that also needs replacing!
$trsString2=str_replace('%CSVNAME%',$csvname2,$trsString2);
$trsString3=str_replace('%CSVNAME%',$csvname3,$trsString3);
$trsString1=str_replace('%ENV%',$env,$trsString1);
$trsString2=str_replace('%ENV%',$env,$trsString2);
$trsString3=str_replace('%ENV%',$env,$trsString3);
$trsString1=str_replace('%LOCATION%',$csvShare,$trsString1);
$trsString2=str_replace('%LOCATION%',$csvShare,$trsString2);
$trsString3=str_replace('%LOCATION%',$csvShare,$trsString3);
// find and replace the variables in the logs string
$copyToLogs1=str_replace('%CSVNAME%',$csvname1,$copyToLogs1); // this has to come first as it uses the %ENV% variable that also needs replacing!
$copyToLogs2=str_replace('%CSVNAME%',$csvname2,$copyToLogs2);
$copyToLogs3=str_replace('%CSVNAME%',$csvname3,$copyToLogs3);
$copyToLogs1=str_replace('%ENV%',$env,$copyToLogs1);
$copyToLogs2=str_replace('%ENV%',$env,$copyToLogs2);
$copyToLogs3=str_replace('%ENV%',$env,$copyToLogs3);
$copyToLogs1=str_replace('%LOCATION%',$csvShare,$copyToLogs1);
$copyToLogs2=str_replace('%LOCATION%',$csvShare,$copyToLogs2);
$copyToLogs3=str_replace('%LOCATION%',$csvShare,$copyToLogs3);
// find and replace the variables in the netUse string
$netUseString=str_replace('%LOCATION%',$csvShare,$netUseString);
$netUseString=str_replace('%NETUSER%',$netUser,$netUseString);
$netUseString=str_replace('%NETUSERPASS%',$netUserPass,$netUseString);
// setup check for log file directory
$createDIR="IF NOT EXIST \\\\$fileshare\c$\\xampp\htdocs\mhs\logs\%ENV%\. MKDIR \\\\$fileshare\c$\\xampp\htdocs\mhs\logs\%ENV%";
$createDIR=str_replace('%ENV%',$env,$createDIR);
// execute the export command
echo $exportStringTRS1. "</P>";
exec($exportStringTRS1,$output,$return);
if ($return)
{
?><script type="text/javascript">
alert("CSV Export Failed. Please investigate.");
</script><?php
}
// execute the export command
echo $exportStringTRS2. "</P>";
exec($exportStringTRS2,$output,$return);
if ($return)
{
?><script type="text/javascript">
alert("CSV Export Failed. Please investigate.");
</script><?php
}
// execute the export command
echo $exportStringTRS3. "</P>";
exec($exportStringTRS3,$output,$return);
if ($return)
{
?><script type="text/javascript">
alert("CSV Export Failed. Please investigate.");
</script><?php
}
// execute the netuse to create connection to correct DC
//echo $netUseString. "</P>";
exec($netUseString,$output,$return);
// execute the csv copy command
echo $trsString1. "</P>";
exec($trsString1,$output,$return);
if ($return)
{
?><script type="text/javascript">
alert("CSV Copy to Wintel share failed. Please investigate.");
</script><?php
}
// execute the csv copy command
echo $trsString2. "</P>";
exec($trsString2,$output,$return);
if ($return)
{
?><script type="text/javascript">
alert("CSV Copy to Wintel share failed. Please investigate.");
</script><?php
}
// execute the csv copy command
echo $trsString3. "</P>";
exec($trsString3,$output,$return);
if ($return)
{
?><script type="text/javascript">
alert("CSV Copy to Wintel share failed. Please investigate.");
</script><?php
}
//check the log directory exists and if not then create it.
echo $createDIR. "</P>";
exec($createDIR,$output,$return);
// execute the log copy command
echo $copyToLogs1. "</P>";
exec($copyToLogs1,$output,$return);
if ($return)
{
?><script type="text/javascript">
alert("CSV Copy to log directory failed. Please investigate.");
</script><?php
}
// execute the log copy command
echo $copyToLogs2. "</P>";
exec($copyToLogs2,$output,$return);
if ($return)
{
?><script type="text/javascript">
alert("CSV Copy to log directory failed. Please investigate.");
</script><?php
}
// execute the log copy command
echo $copyToLogs3. "</P>";
exec($copyToLogs3,$output,$return);
if ($return)
{
?><script type="text/javascript">
alert("CSV Copy to log directory failed. Please investigate.");
</script><?php
}
?>
</br>
</body>
</html>
dbo-export-complete-trs.phpdbo-export-trs-process.php
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The error is telling you that you are not setting them
Going by your code they are only ever set if certain conditions are met.
The only variable that gets a default value is $csvname3
Going by your code they are only ever set if certain conditions are met.
The only variable that gets a default value is $csvname3
greetings theghost121, , , your programming "Logic" used in the Nine "If Else" sequence starting with -
// TRS CSV File create
if ($type =="TRS1" && $location==="ODC")
is NOT correct at all, because you test the $location BUT DO NOT USE that test to change any of the result output, What I would do is to take out (erase, delete) ALL of the 9 "If Else" code and just have this -
$csvname1="%ENV%-TRS1";
$csvname2="%ENV%-TRS2";
$csvname3="%ENV%-TRS3";
// /// // // // //
I can not understand or see any reason for all the
if ($type =="TRS1" && $location==="ODC")
tests, as Nothing is any different in the results, no matter what the $location input is, however, in the following code using the $csvname1 variable, it may actually matter about what the $type and $location are set to, in order to get the line -
$exportStringTRS1=str_repl ace('%CSVN AME%',$csv name1,$exp ortStringT RS1);
with $csvname1 to have correct output ? ?
Also you have Bad Logic, in the IF tests for -
// check for the location and set values accordingly
if ($location=="MPE")
ALL, that's right, every one, of the outputs are exactly the same, the "IF tests" do nothing at all!
// TRS CSV File create
if ($type =="TRS1" && $location==="ODC")
is NOT correct at all, because you test the $location BUT DO NOT USE that test to change any of the result output, What I would do is to take out (erase, delete) ALL of the 9 "If Else" code and just have this -
$csvname1="%ENV%-TRS1";
$csvname2="%ENV%-TRS2";
$csvname3="%ENV%-TRS3";
// /// // // // //
I can not understand or see any reason for all the
if ($type =="TRS1" && $location==="ODC")
tests, as Nothing is any different in the results, no matter what the $location input is, however, in the following code using the $csvname1 variable, it may actually matter about what the $type and $location are set to, in order to get the line -
$exportStringTRS1=str_repl
with $csvname1 to have correct output ? ?
Also you have Bad Logic, in the IF tests for -
// check for the location and set values accordingly
if ($location=="MPE")
ALL, that's right, every one, of the outputs are exactly the same, the "IF tests" do nothing at all!
ASKER
I've requested that this question be closed as follows:
Accepted answer: 0 points for theghost121's comment #a39923996
for the following reason:
Thanks both, I managed to sort out the issue with your help... Thanks again
Accepted answer: 0 points for theghost121's comment #a39923996
for the following reason:
Thanks both, I managed to sort out the issue with your help... Thanks again
ASKER
From the code above each variable just repeats over again.
Open in new window