PHP Session problems.

MBrown

Diamond Member
Jul 5, 2001
5,724
35
91
I have an issue (the first of many but I think this one issue is causing the rest of them) with my session_start.

I have succesfully passed a variable from on page to another which is a primary key for a table. I have succesfully used that variable to pull data from a database to populate a html form so that I can edit the data. But when I submit the edited data and get a bunch of errors the first of which is this...

Notice: Undefined index: serialNumber in C:\wamp\www\edit.php on line 10

I do not understand why am getting this error seeing as I have already used this variable in a query to the database with no errors at all.

Here is my code...

Code:
<html>
<head>
<title>Development Get Page</title>
</head>
<body>
<?php
echo '<h1>Gizmo Bids Database (Beta!)</h1>';

session_start();
$serialNumberValue = $_GET['serialNumber'];
echo "The Serial Number is: " .$serialNumberValue. ". <br />"; //this code is just to check to see if my variable passing is working

$con = mysqli_connect("localhost","root","","gizmo");
$form_query = "select * from bids where serialNumber = $serialNumberValue";
$list_sql = "select serialNumber from bids";
$form_result = mysqli_query($con,$form_query);
$form_row = mysqli_fetch_assoc($form_result);



// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else
 {
  echo "you are connected! <br> <br>";
 }


if(isset($_POST['dueDate']) &&
	isset($_POST['dueTime']) &&
	isset($_POST['state']) &&
	isset($_POST['organization']) &&
	isset($_POST['description']) &&
	isset($_POST['solicitation']) &&
	isset($_POST['note']) &&
	isset($_POST['website']))
	{
		$dueDate = $_POST[dueDate];
		$dueTime = $_POST[dueTime];
		$stateCode = $_POST[state];
		$organization = $_POST[organization];
		$description = $_POST[description];
		$solicitation = $_POST[solicitation];
		$note = $_POST[note];
		$website = $_POST[website];
		
		$query = "UPDATE bids
				  SET dueDate = $dueDate,
					  dueTime = $dueTime,
					  stateCode = $stateCode,
					  organization = $organization,
					  description = $description,
					  solicitation = $solicitation,
					  note = $note,
					  website = $website
				  WHERE serialNumber = $serialNumberValue";
		if (!mysqli_query($query,$con))
			echo "Edit has failed: $query <br />" . mysqli_error() . "<br /><br />";
	}
echo "you have made an edit!";

 
 

//This is the form for the page.  The fields are automatically populated with the current data
echo <<<_END
	<form action="edit.php" method="post">
	<pre>
		Due Date: <input type="date" name="dueDate" id="dueDate" value=$form_row[dueDate] /> Format YEAR-MO-DA <br><br> 
		Due Time: <input type="text" name="dueTime" value=$form_row[dueTime] /> Format HR:MIN:SEC (Military Time)<br><br> 
		State: <input type="text" name="state" value=$form_row[stateCode] /> Format XX <br><br> 
		Organization: <input type="text" name="organization" value=$form_row[organization] /> <br><br>	
		Description: <input type="text" name="description" value=$form_row[description] /> <br><br>
		Solicitation: <input type="text" name="solicitation" value=$form_row[solicitation] /> <br><br>
		Note: <input type="text" name="note" value=$form_row[note]  /> Remove the slash if you don't want it!!! <br><br>
		Website: <input type="text" name="website" value=$form_row[website] /> <br><br>
		<input type="submit" value="Edit Record" />
	</pre>
	</form>
_END;
 
 
 
 
?>



</body>
</html>

Thanks in advance.
 

Jaydip

Diamond Member
Mar 29, 2010
3,691
21
81
I haven't used PHP in some time but this line doesn't look correct to me.

$form_query = "select * from bids where serialNumber = $serialNumberValue";

I don't think it can substitute variable vales in string.
 

MBrown

Diamond Member
Jul 5, 2001
5,724
35
91
I haven't used PHP in some time but this line doesn't look correct to me.

$form_query = "select * from bids where serialNumber = $serialNumberValue";

I don't think it can substitute variable vales in string.

It worked for me.
 

douglasb

Diamond Member
Apr 11, 2005
3,157
0
76
On an unrelated note, I'm going to spare you all the obligatory "Little Bobby Tables" reference, and instead mention that this code is vulnerable to SQL injection.
 

MBrown

Diamond Member
Jul 5, 2001
5,724
35
91
I was using $_POST initially but I was getting the same problem.
 

MBrown

Diamond Member
Jul 5, 2001
5,724
35
91
Let me try and clarify what I my issue is here. After I pass my variable from the previous page to this one, it displays the current table data on the form. That works without any errors. As soon as I press the submit button on this page form, I get the undefined index error. My thought is that the $_POST array is getting overwritten so that my serialNumber index is no longer in the post array. Am I correct with my assumption?
 

beginner99

Diamond Member
Jun 2, 2009
5,231
1,605
136
Let me try and clarify what I my issue is here. After I pass my variable from the previous page to this one, it displays the current table data on the form. That works without any errors. As soon as I press the submit button on this page form, I get the undefined index error. My thought is that the $_POST array is getting overwritten so that my serialNumber index is no longer in the post array. Am I correct with my assumption?

You need to start understanding the basic because then the issue here will be obvious.

First just to repeat: Fix SQL-Injection vulnerability!!!
Even if this is just a meaningless hobby project, learn to do it right from the start. It is hard to get rid of bad habits! See link in a previous post but the solution is prepared statements.

The actual issue:

$_GET and $_POST have nothing, absolutely nothing to do with a PHP session:

http://en.wikipedia.org/wiki/HTTP#Request_methods

This is basics of how http works.

Since they are request methods, anything in them will expire at the end of the request. When you click on your submit button that will start a new request to edit.php and everything from your previous request will be lost. $_post will only contain the fields within your form.

If you want to store a value in the session you need to use $_SESSION. See PHP doc...

However this is not required. In this case you can add a hidden input field into your form, name it "serialNumber" and populate it on page load.

Besides that there is plenty other room for improvement like using <br> for layout is terrible as is the form you are creating within <pre>. Use <label> for adding a label to a input element.

http://www.w3.org/wiki/HTML/Elements/label

Use css for design!!! html is for semantics only and <br> is only to be used in actual text displayed and not for layout.

Sorry for being harsh, but again better to adjust as early as possible than getting used to bad habits.

Note that to create a good looking, usable web application you need to know a lot of things:

- HTML
- CSS
- JavaScript
- server side language -> PHP
 

MBrown

Diamond Member
Jul 5, 2001
5,724
35
91
You need to start understanding the basic because then the issue here will be obvious.

First just to repeat: Fix SQL-Injection vulnerability!!!
Even if this is just a meaningless hobby project, learn to do it right from the start. It is hard to get rid of bad habits! See link in a previous post but the solution is prepared statements.

The actual issue:

$_GET and $_POST have nothing, absolutely nothing to do with a PHP session:

http://en.wikipedia.org/wiki/HTTP#Request_methods

This is basics of how http works.

Since they are request methods, anything in them will expire at the end of the request. When you click on your submit button that will start a new request to edit.php and everything from your previous request will be lost. $_post will only contain the fields within your form.

If you want to store a value in the session you need to use $_SESSION. See PHP doc...

However this is not required. In this case you can add a hidden input field into your form, name it "serialNumber" and populate it on page load.

Besides that there is plenty other room for improvement like using <br> for layout is terrible as is the form you are creating within <pre>. Use <label> for adding a label to a input element.

http://www.w3.org/wiki/HTML/Elements/label

Use css for design!!! html is for semantics only and <br> is only to be used in actual text displayed and not for layout.

Sorry for being harsh, but again better to adjust as early as possible than getting used to bad habits.

Note that to create a good looking, usable web application you need to know a lot of things:

- HTML
- CSS
- JavaScript
- server side language -> PHP

I know that I need to address the SQL injection mess. That's not what I'm on now, and is not what I asked. Anyway I figured out what my problem was and fixed it. And it was exactly what I thought it was. It indeed didn't have anything to do with $_SESSION at this point in my code...I was able figured that out. The issue was when I pressed the submit button, the serialNumber value I had in $_POST was getting lost so what I did was put it back in by doing a hidden input in my form and the error went away.

Now my error is a problem with my SQL syntax, which I am not seeing...
 

beginner99

Diamond Member
Jun 2, 2009
5,231
1,605
136
the serialNumber value I had in $_POST was getting lost

It's not getting lost. it was not there because you did not set it in the first place.

I know I'm being pedantic but it's a major difference if you do something just because it works or if you actually understand why it works and why you need to do it that way.
 

haley01

Banned
Jun 20, 2013
1
0
0
www.widevisiontechnologies.com
Well I saw your problem, let me concern this with my seniors for better solution..

Sorry, your IP address is in a range of IP addresses from India that are used for frequent spamming.
-Admin DrPizza
 
Last edited by a moderator:
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |