Click here to Skip to main content
13,410,621 members (32,429 online)
Rate this:
 
Please Sign up or sign in to vote.
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:
<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 24-Aug-17 9:24am
Updated 29-Aug-17 17:53pm
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Not a solution to your question, but another problem you have.
$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[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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;
}
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.180221.1 | Last Updated 29 Aug 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100