Click here to Skip to main content
15,895,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created select drop-down from MySQL column. When user clicks button it runs php code from another page using get method. I have stored 2 variables in get session. But when first time page is loaded in IE, i get error:
Notice: Undefined index: id 


But if I hit button, query is successful and echoes value in table. There is default option set in dropdown but, again I have to click to query result successfully in IE and Mozilla. No issue on page load in Chrome.
I tried to hit button only once using javascript but it button gets hit repeatedly resulting in page load continuously.

What I have tried:

Main Page:
PHP
<pre>$array_id = $_SESSION['id'];
//print_r($array_id);

//Get version id
$version_id = $array_id[array_keys($array_id)[0]];
//echo $version_id;

// Find send version number
$print_version = $conn->query("SELECT nx_version FROM workflow1 WHERE id='$version_id';"); 
$print_version1 = Array();
while($result_step2 = $print_version->fetch_assoc()){
    $print_version1[] = $result_step2['nx_version'];
}

//echo $print_version1[array_keys($print_version1)[0]];
    $result = $conn->query("SELECT DISTINCT nx_version FROM workflow1 ORDER BY id DESC");
    echo "<form action='http://localhost/w_5aug/process.php' id='frm1' method='get'>";
    echo "<html>";
    echo "<body>";
	echo "<p></p>";
	echo "<center>";
	echo " Select Base Verison To Compare With : ";
    echo "<select name='nx_version' id='nx_version'>";
while ($row = $result->fetch_assoc()) {
         $nx_version = $row['nx_version']; 
         if($print_version1[array_keys($print_version1)[0]] == $nx_version){
          echo '<option selected="selected">'.$nx_version.'</option>';
        }else{
          echo '<option>'.$nx_version.'</option>';
        }

 }
    echo "</select>";
    echo " <button type='submit' id='myButton'>Add Base Verison</button>";	
	echo "</center>";
    echo "</body>";
    echo "</html>";
	echo "<p></p>";	
	
	$array_select = $_SESSION['data'];
	print_r($array_select);

	echo "<form>";


Process.php:
$query = $conn->query("SELECT step1, step2 FROM workflow1 WHERE nx_version='$nx_version' LIMIT 1");
if ($query) {
    $array_select = mysqli_fetch_row($query);
    print_r($array_select);
}

//$array_select = Array();
// Find send version number
$id = $conn->query("SELECT id FROM workflow1 WHERE nx_version='$nx_version' LIMIT 1");
$array_id = Array();
while($result1 = $id->fetch_assoc()){
    $array_id[] = $result1['id'];
}

$_SESSION['id'] = $array_id;
$_SESSION['data'] = $array_select;
Posted
Updated 29-Aug-17 16:53pm
v4

Not a solution to your question, but another problem you have.
PHP
$print_version = $conn->query("SELECT nx_version FROM workflow1 WHERE id='$version_id';");

Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
When you run your code for the first time there is no session called $_SESSION['id'] thats why it is returning undefined variable.
Try like this
if($_SESSION['id']){
$array_id = $_SESSION['id'];
}else{
$array_id = 0;
}
 
Share this answer
 
Comments
SamadhanGaikwad 29-Aug-17 22:54pm    
Now I am getting Warning: array_keys() expects parameter 1 to be array, integer given in C:\xampp\htdocs\w_5Aug\chart.php on line 222 and Notice: Undefined index: data in C:\xampp\htdocs\w_5Aug\chart.php on line 258
Please have a look at updated code in question.
Aneel Shrestha 2-Sep-17 14:44pm    
You cannot insert array in session your session should be also array
$_SESSION['id'] = array();
$_SESSION['id'][] = $array_id;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900