Jump to content

Sessions not working properly


karan

Recommended Posts

I have 3 files (m1.php, m2.php, m3.php)


m1 code:



<?php session_start(); //starting the session ?>
<!DOCTYPE HTML>
<html>
<strong> Enter Phone Number: </strong>
<form action="m2.php" method = "POST">
<input type="text" name="ph"/>
<input type="submit" name="submit" value="Submit" />
</form>

<?php
if (isset($_POST['submit'])) {
$_SESSION['ph'] = $_POST['ph'];
}
?>
</html>

m2 code:



<?php session_start(); ?>
<html>
<body> Details of:
<strong>
<?php
echo $_POST['ph'];
$_SESSION['phone'] = $_POST['ph'];
?>
</strong>
<?php
$ph = $_SESSION['phone'];
$link = oci_connect('hd','hd', 'localhost/mydb');
if(!$link) {
$e = oci_error();
exit('Connection error ' . $e['message']);
}
$q1 = "select CUST_ID from customer where CUST_PHONE = :bv_ph";
$q1parse = oci_parse($link, $q1);
oci_bind_by_name($q1parse, ':bv_ph', $phone);
oci_execute($q1parse);
oci_fetch($q1parse);
$res = oci_result($q1parse, 'CUST_ID');
if(!$res) {
echo "No Order found. New Order?";
}
?>
<?php
if(isset($_POST['option']) && ($_POST['option']) == "Yes") {
header("Location: m3.php");
}
elseif(isset($_POST['option']) && ($_POST['option']) == "No") {
header("location: m1.php");
}

$q2 = "select A.ADDRESS, A.AREA from customer c
join customer_address ca on C.CUST_ID = CA.CUST_ID
join address a on A.ADDRESS_ID = CA.ADDRESS_ID where C.CUST_ID = :id_bv";
$q2parse = oci_parse($link, $q2);
oci_bind_by_name($q2parse, ':id_bv', $res);
oci_execute($q2parse);
while($row = oci_fetch_array($q2parse)) {
echo "<tr><td>" . htmlentities($row["ADDRESS"]) . "</td>";
echo "<td>" . htmlentities($row["AREA"]) . "</td></tr>";
}
oci_free_statement($q2parse);
oci_close($link);

?>

<form action = "" method = "POST" >
<input type = "radio" name = "option" value = "Yes" checked> Yes <br>
<input type = "radio" name = "option" value ="No"> No <br>
<input type = "submit" value = "submit">
</form>
<table border = "black">
<tr>
<th> ADDRESS </th>
<th> AREA </th>
</tr>
</table>
</body>
</html>

m3 code:



<?php session_start(); ?>
<!DOCTYPE HTML>
<html>
<?php
echo $_SESSION['phone'];
?>
</html>

The problem is that the session variable ($_SESSION['phone']) works in m2.php but it does not display in m3.php. It just displays nothing. My main aim is to take the phone number and 1st check in the database. If it exists then display the information else add the no. in the database along with other details like name, address etc. I am trying to use sessions for this but the above problem is not making me do it. Stuck on this since long time.Help Please!


Also, if I use the same thing without the form in m2.php, it works perfectly.


Link to comment
Share on other sites

m1.php has logic in it that will never be run.  All that script does is output a from.

 

m2 - Where is $phone being set?  Plus - with your poorly structured script (mixing of html and php) - your attempt to use a header to jump to somewhere else will probably fail since you have already output your html.  That is why it is recommend to do your php stuff FIRST and only THEN output the html.  You should turn on php error checking in these scripts while doing your development to see any error messages that occur.  

 

PS - what is the table for in m2?  And why the php code looking for the option element of POST when you haven't yet output that form?  That will show you a warning message unless you fix your code to check if it exists first.  (See my signature)

 

Welcome to PHP

Link to comment
Share on other sites

You keep making the same mistakes.

 

We already had a long discussion about how to properly structure your scripts and why spaghetti code and interwoven forms are bad. Now you're coming back with ... spaghetti code and interwoven forms. Does it really surprise you that you're running into the same problems over and over again?

 

Programming is not about producing lots of code and then hoping it somehow "works". It's about approaching a problem systematically.

 

I suggest you throw away the code, actually read our replies and then start over. This time step by step. Start with one(!) script which actually makes sense. Then we can talk about more advanced tasks.

Link to comment
Share on other sites

How do you want me to structure it. According to me

 

Step 1: In 'm1.php', the user enters the phone number, which is passed to m2.php.

Step 2: First thing in 'm2.php' is to display the number entered by the user. (setting $_POST['ph'] = $_SESSION['phone'])

Step 3: Check weather the number exists in the DB or not. (which I did with OCI using $link etc.) 

Step 4: If the number does not exist then ask the user weather he wants to add new details or not. (which I get by the form in m2.php).

Step 5: Move to 'm3.php' and add the customer details.

 

There are more things to be done in m3.php but that I will be only able to do when I have my number in all the pages. The scripts are arranged in the way the user will enter data like phone number. How else am I supposed to structure it? That's how I am approaching the problem.

Link to comment
Share on other sites

The table in m2 is for displaying the details of the customer having the number entered if it exists. I am very new to php so i have no idea how am I supposed to structure this. I just did step by step as it will happen. As in,

 

First eh user enters number, then the number is checked, if it exists then the details are fetched from the database else its asked weather he wants to enter the details or not. If no then it takes back to where he entered the number, if yes then he has to fill in the details. So the html and php are mixed. But how am I supposed to do it if not like this?

Link to comment
Share on other sites

We've already gone through the structure. You actually acknowledged the advice, so it's rather strange that you've then completely ignored it. You're using the exact same bad code you had last month.

 

As several people told you several times, form scripts should generally submit to themselves. This makes it a lot easier to understand the processing and handle errors, because it's all in one script.

 

Read the replies. If you don't understand them, feel free to ask. But when you completely ignore multiple detailed answers, then I get the impression I'm wasting my time with you.

Edited by Jacques1
Link to comment
Share on other sites

I did not ignore any of your advice. I did try it. I removed everything and only the sessions remained. It worked perfectly. Here too, it does work fine till m2. But the minute I add the radio button form, it doesn't work. My entire project is just stuck because of this. Please just tell me what the order of the php and the html should be in the above case. Its just getting frustrating. I tried everything. 

Link to comment
Share on other sites

As I see it your m1 does NOT get a phone number.  All it does is display an html form.  Poorly written for an html page and poorly designed for an input form.

 

Judging by Jacques comments I probably just wasted some valuable typing time, but oh, well....

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.