/    Sign up×
Community /Pin to ProfileBookmark

using "session"

I wonder if anyone can assist me, how can I change these two forms (create.php and modify.php) into one form only say (createMod.php) using session?

[B]create.php[/B]

[code=php]
<?php

$id = “”;
$name = “”;
$surname = “”;
$dept= “”;

if (isset($_POST[‘submit’])) {

$id = $_POST[‘id’];
$name = $_POST[‘name’];
$surname = $_POST[‘surname’];
$dept = $_POST[‘dept’];

}
?>
<html ><head></head><body>
<form action=”<?php echo $_SERVER[‘PHP_SELF’]; ?>” method=”post”>
<div>
<div align=”center”>
<table width=”300″ border=”1″>
..
..
..<!–FORM TABLE–>
..
</table></div></form></body></html>

[/code]

[B]modify.php[/B]

[code=php]
<?php
require ‘includes/application_top.php’;

$id = “”;
$name = “”;
$surname = “”;
$dept= “”;

if (!isset($_POST[‘submit’]))
{

$q = “SELECT * FROM persons WHERE ID = $_GET[id]”;

$result = mysql_query($q) or die (mysql_error());
$row = mysql_fetch_array($result);

$id = $_POST[‘id’];
$name = $row[‘name’];
$surname = $row[‘surname’];
$dept = $row[‘dept_id’];

}

if (isset($_POST[‘submit’]))
{
$id = $_POST[‘id’];
$name = $_POST[‘name’];
$surname = $_POST[‘surname’];
$dept = $_POST[‘dept’];
}
?>
<html ><head></head><body>
<form action=”<?php echo $_SERVER[‘PHP_SELF’]; ?>” method=”post”>
<div align=”center”>
<table width=”300″ border=”1″>
..
.. <!–FORM TABLE–>
..
</table></div></form></body></html>
[/code]

to post a comment
PHP

61 Comments(s)

Copy linkTweet thisAlerts:
@siabanieauthorJan 25.2011 — I wonder if anyone can help me; I tried to declare all the variable using session; then pull all the data from the database also create a form on the same page of modify form.

Can anyone help me out where do I have to start as I am new about session.

[code=php]
<?php
session_start();

//Register our session variables
session_register('id');
session_register('name');
session_register('surname');
session_register('dept');


//Store our posted values in the session variables
$_SESSION['name'] = $_POST['id'];
$_SESSION['name'] = $_POST['name'];
$_SESSION['surname'] = $_POST['surname'];
$_SESSION['dept'] = $_POST['dept'];
?>

[/code]
Copy linkTweet thisAlerts:
@DexterMorganJan 25.2011 — Hi,

From what I have read you shouldnt really use session_register.

The following is enough to store all of the post values in a session.

[code=php]
session_start();
$_SESSION['id'] = $_POST['id'];
$_SESSION['name'] = $_POST['name'];
$_SESSION['surname'] = $_POST['surname'];
$_SESSION['dept'] = $_POST['dept'];
[/code]


Then to access any of these session values you would do:

[code=php]
echo $_SESSION['name']; //or pass to functions etc
[/code]


As for your original question I do not really understand what you want to do.
Copy linkTweet thisAlerts:
@siabanieauthorJan 25.2011 — Hi DexterMorgan,

I tried to pull the data to modify the values but it does not seem to be working. It gave me error that the variables are undefined. Can you help me what did I do wrong?

Notice: Undefined index: id,name, surname, address, dept in line 8, 9, 10, 11, 12

modify.php
[code=php]
<?php
require 'includes/connection.php';
session_start();

//Store our posted values in the session variables
$_SESSION['id'] = $_POST['id']; // Line 8
$_SESSION['name'] = $_POST['name']; // Line 9
$_SESSION['surname'] = $_POST['surname'];// Line 10
$_SESSION['address'] = $_POST['address']; // Line 11
$_SESSION['dept'] = $_POST['dept']; // Line 12


//Edit section
if (!isset($_POST['submit']))
{
$edit_sql= "SELECT * FROM persons WHERE ID = $_GET[id]";

$result = mysql_query($edit_sql) or die (mysql_error());
$row = mysql_fetch_array($result);
}

// Checking error if name and surname are empty field
// Can we use empty fucntion istead would it would much better way?
$errormsg = "";

if($name == "")
$errormsg = $errormsg. "Name<br/ >";

if ($surname == "")
$errormsg = $errormsg. "Surname Please <br/ >";

if ($errormsg != "")
echo "Please fill the blank info: <br/ > $errormsg";

else
{

$input_field = "";

foreach ($_POST as $key => $val)
{

if ($key != 'submit')
{
$input_field.= "<input type = 'hidden' name = '$key' value = '$val'/>";
}
}
}

?>

<html><head>
<title>Modify Document</title>
</head>

<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div align="center">
<table width="300" border="1">
<h1> Modifying A User </h1>

<tr>
<th scope="row">Name</th>
<td><input type="text" name="name" value="<?php echo $_SESSION['name'];?>" /></td>
</tr>

<tr>
<th scope="row">Surname</th>
<td>
<input type="text" name="surname" value="<?php echo $_SESSION['surname']; ?>" /></td>
</tr>

<tr>
<th scope="row">Address</th>
<td><input type="text" name="add" value="<?php echo $_SESSION['address']; ?>" /></td>
</tr>


<tr>
<th scope="row">Department</th>
<td><input type="text" name="add" value="<?php echo $_SESSION['dept']; ?>" /></td>
</tr>

</table>
<br/>

<a href="index.php">
<input type="button" name="back" value="Back" /></a>

<!--Getting the id from the main page (index.php)-->
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">

<input type="submit" name="submit" value="Modify"/>

</div>
</form></body></html>
[/code]
Copy linkTweet thisAlerts:
@DexterMorganJan 25.2011 — Hmm odd, it should be fine with that code. try putting session_start() straight after the opening <?php tag.

[code=php]
function pre($v) {
echo '<pre>';
print_r($v);
echo '</pre>';
}
[/code]


Then can you use that function to dump out the session and post arrays to check that everything is how it should be e.g.

[code=php]
pre($_SESSION);
pre($_POST);
[/code]
Copy linkTweet thisAlerts:
@siabanieauthorJan 25.2011 — I have put the function in my code, and I am not getting any values of it, do you think it has to do with my main (index.php) page?

The function output:

Array

(

[name] =>

[surname] =>

[department] =>

[address] =>

[phone] =>

[user] => Test

[authuser] => 1

[id] =>

[dept] =>

)

Array

(

)


[code=php]
<?php
session_start();
require 'includes/connection.php';

function pre($v) {
echo '<pre>';
print_r($v);
echo '</pre>';
}


//Store our posted values in the session variables
$_SESSION['id'] = $_POST['id']; // Line 8
$_SESSION['name'] = $_POST['name']; // Line 9
$_SESSION['surname'] = $_POST['surname'];// Line 10
$_SESSION['address'] = $_POST['address']; // Line 11
$_SESSION['dept'] = $_POST['dept']; // Line 12

pre($_SESSION);
pre($_POST);

//Edit section
if (!isset($_POST['submit']))
{
$edit_sql= "SELECT * FROM persons WHERE ID = $_GET[id]";

$result = mysql_query($edit_sql) or die (mysql_error());
$row = mysql_fetch_array($result);
}
...
...
...

[/code]
Copy linkTweet thisAlerts:
@DexterMorganJan 25.2011 — Are you still getting the PHP undefined index notices?

Are you passing POST data to this page?

try the following code:
[code=php]
$_SESSION['id'] = (isset($_POST['id'] && !empty($_POST['id']) ) ? $_POST['id'] : '';
$_SESSION['name'] = (isset($_POST['name'] && !empty($_POST['name']) ) ? $_POST['name'] : '';
$_SESSION['surname'] = (isset($_POST['surname'] && !empty($_POST['surname']) ) ? $_POST['surname'] : '';
$_SESSION['dept'] = (isset($_POST['dept'] && !empty($_POST['dept']) ) ? $_POST['dept'] : '';
[/code]
Copy linkTweet thisAlerts:
@siabanieauthorJan 25.2011 — Yes I still getting all the errors: Undefined index notices as posted#5, and yes I am posting data to this page. This page is a modify page so any data should be pull from the previous page (main page) and this page also be able to create a new user data. Basically this page do two things: One is modify the page and second create a new user data.

This is what the first bit look like now, but I got a new error :

Parse error: parse error, expecting ','' or ')'' [B]line 12[/B] - I tried to put the bracket ')' which I think we missed but don't think I got it at the right place.

[B]person.php[/B]
[code=php]
<?php
session_start();
require 'includes/connection.php';

function pre($v) {
echo '<pre>';
print_r($v);
echo '</pre>';
}

$_SESSION['id'] = (isset($_POST['id'] && !empty($_POST['id']) ) ? $_POST['id'] : ''; //Line 12
$_SESSION['name'] = (isset($_POST['name'] && !empty($_POST['name']) ) ? $_POST['name'] : '';
$_SESSION['surname'] = (isset($_POST['surname'] && !empty($_POST['surname']) ) ? $_POST['surname'] : '';
$_SESSION['dept'] = (isset($_POST['dept'] && !empty($_POST['dept']) ) ? $_POST['dept'] : '';

pre($_SESSION);
pre($_POST);

//Edit section
if (!isset($_POST['submit']))
{
$edit_sql= "SELECT * FROM persons WHERE ID = $_GET[id]";
echo $edit_sql;
$result = mysql_query($edit_sql) or die (mysql_error());
$row = mysql_fetch_array($result);
}
..
..
..
[/code]
Copy linkTweet thisAlerts:
@DexterMorganJan 25.2011 — Missed a bracket sorry should be:
[code=php]
$_SESSION['id'] = (isset($_POST['id']) && !empty($_POST['id']) ) ? $_POST['id'] : '';
$_SESSION['name'] = (isset($_POST['name']) && !empty($_POST['name']) ) ? $_POST['name'] : '';
$_SESSION['surname'] = (isset($_POST['surname']) && !empty($_POST['surname']) ) ? $_POST['surname'] : '';
$_SESSION['dept'] = (isset($_POST['dept']) && !empty($_POST['dept']) ) ? $_POST['dept'] : '';
[/code]


It looks like your post array is empty
Copy linkTweet thisAlerts:
@siabanieauthorJan 25.2011 — Yes, got the bracket and run okay now - but I don't understand why my array is empty? Is that has to do with my main page? I don't get it..

[B]index.php[/B] (Main page)

[code=php]
<?php

session_start();
require 'includes/application_top.php';

$data = mysql_query ("SELECT persons.id, persons.name AS persons_name, persons.surname, persons.address,
dept.dept_name FROM persons LEFT JOIN dept ON persons.dept_id = dept.id ORDER BY persons.id DESC") or die (mysql_error());
?>
<html><head>
<title>Friends Document</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<table border="0" cellpadding="2">
<tr>
<td>
<p><a href="person.php">Create User </a></p>
</td>

<td>
<p><a href="logout.php">Log Out</a><br /></p>
</td>
</tr>
</table>


<table border="2" cellpadding="3">

<tr>
<th>
ID
</th>
<th>First Name</th>
<th>Surname</th>
<th>Address</th>
<th>Department</th>
<th>Edit Link</th>
<th>Delete Link</th>
</tr>
<?php

while($row = mysql_fetch_array( $data ))
{
?>
<tr>
<td><?php echo $row['id'];?></td>

<td>
<?php echo $row['persons_name'];?>

<?php
if (isset($_GET['id']) && (isset($_GET['page'])))
{
if ($_GET['id'] == $row['id'] && $_GET['page'] == "create")
{
echo "Created";
}

if ($_GET['id'] == $row['id'] && $_GET['page'] == "modify")
{
echo "Edited";
}
}
?>
</td>

<!--Printing and pulling all the data-->
<td><?php echo $row['surname'];?></td>
<td><?php echo $row['address'];?></td>
<td><?php echo $row['dept_name'];?></td>

<!--modify.php?= (showing the id)-->
<td><a href = "person.php?id=<?php echo $row['id'];?>">Modify User </a></td>
<td>
<a href= "deleteConfirm.php?id= <?php echo $row['id'];?>">Delete User</a></td>
</tr>

<?php
}
?>
</table></form></body></html>

[/code]


$_SESSION['id'] = (isset($_POST['id']) && !empty($_POST['id']) ) [B][I][COLOR="DarkRed"]? $_POST['id'] :[/COLOR][/I][/B] '';

PS: I don't understand what is this for: ? $_POST['id'] can you please explain?
Copy linkTweet thisAlerts:
@DexterMorganJan 25.2011 — [code=php]<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> [/code]

The code above translates to:

[code=html]<form action="/index.php" method="post">[/code]

The values that you are sending are just going to index.php, you will need to send these to the other page.

EDIT:

didnt see the final bit of your post, sorry.

"$_SESSION['id'] = (isset($_POST['id']) && !empty($_POST['id']) ) ? $_POST['id'] : '';

PS: I don't understand what is this for: ? $_POST['id'] can you please explain? "

That is called a ternary operator - it is just a way of doing an if/else in one line, the syntax is rather confusing at first.

I used it there to check if the post value is set and isnt empty, if it passes these conditions then set the value to the session, if it fails then just set ''
Copy linkTweet thisAlerts:
@siabanieauthorJan 25.2011 — I see okay - well, think would be better if we doing it an if/else then.

I have changed:

[code=php]
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
[/code]


to
[code=php]
<form action="person.php" method="post"> //Is there any other way to write it in PHP by getting the 'id' cos I think we didn't pull which 'id' that we going to modify?
[/code]


but, it's still not working..I think we are missing the 'id'?
Copy linkTweet thisAlerts:
@DexterMorganJan 25.2011 — just read through the index page.

you are not sending anything via post.

you do not even need a form on the index page, the form you have has no inputs.

[code=php]<!--modify.php?= (showing the id)-->
<td><a href = "person.php?id=<?php echo $row['id'];?>">Modify User </a></td>[/code]


So you want to click this modify user link, and it will take you to the person page carrying that person's id?

There will not be any POST data sent from this page to the person page.

You could access this id using GET on the person page
Copy linkTweet thisAlerts:
@siabanieauthorJan 25.2011 — The index page are collecting all the values from my database and whatever data I am sending to the modify page (person.php) - the data should be pulled out into that page in accord to which 'id' I want it be edited. I don't understand why it has no inputs....

Yes, when I click the modify link it should take me to person.php which carry all the data that needs to be edited then after that it will bring me to another page call confirmation page.
Copy linkTweet thisAlerts:
@DexterMorganJan 25.2011 — The index page are collecting all the values from my database and whatever data I am sending to the modify page (person.php) - all the data should be pulled out into that page. I don't understand why it has no inputs.... [/QUOTE]

By inputs I mean html form inputs e.g. <input type="text" name="example" />


Yes, when I click the modify link it should take me to person.php which carry all the data that need to be edited then after that it will bring me to another page call confirmation page.[/QUOTE]


That link will take you to person.php but will only carry one value, which is id, this is the only value that you have added to the url.

You could carry all of this information from the index to the person page using a session.
Copy linkTweet thisAlerts:
@siabanieauthorJan 25.2011 — Ok yes, sorry well I tried to using session at the index page as shown but it still did not pull the data out into person page.... Im not sure what did I do wrong? Im really confused how can we pull the data from the index page to person page using session?

[B]index.php[/B]
[code=php]
...
...
<?php

while($row = mysql_fetch_array( $data ))
{
?>
<tr>
<td><?php echo $row.$_SESSION['id'];?></td>

<td>
<?php echo $row.$_SESSION['persons_name'];?>

<?php
if (isset($_GET['id']) && (isset($_GET['page'])))
{
if ($_GET['id'] == $row['id'] && $_GET['page'] == "create")
{
echo "Created";
}

if ($_GET['id'] == $row['id'] && $_GET['page'] == "modify")
{
echo "Modified";
}
}
?>
</td>
<td><?php echo $row.$_SESSION['surname'];?></td>
<td><?php echo $row.$_SESSION['address'];?></td>
<td><?php echo $row.$_SESSION['dept_name'];?></td>
<td><a href = "person_c.php?id=<?php echo $row['id'];?>">Modify User </a></td>

<td>
<a href= "deleteConfirm.php?id= <?php echo $row['id'];?>">Delete User</a></td>
</tr>
<?php
}
?>
...
...

[/code]
Copy linkTweet thisAlerts:
@DexterMorganJan 25.2011 — There you are just concatenating values.

How many rows are you planning on getting out of the database in index.php?
Copy linkTweet thisAlerts:
@siabanieauthorJan 25.2011 — A lot of rows, but I still get an empty inputs on person page.??
Copy linkTweet thisAlerts:
@DexterMorganJan 25.2011 — Alright well if you set the session values inside the loop you are going to overwrite them with each iteration.

One way of getting values on the persons page is:

  • 1. stop using sessions on the index.php

  • 2. on the index page get the id by doing $id = $_GET['id']

  • 3. query the database again adding a where id = $id

  • 4. place the values from the database into the inputs
  • Copy linkTweet thisAlerts:
    @siabanieauthorJan 25.2011 — Right, I have removed all the session on the index page. I am not fully understand about the rest but here what I did:

    On the index page get the id by doing $id = $_GET['id']

    [B]index.php[/B]

    [code=php]
    //I have declare the $id variable on top of the page as so I thought maybe I can use like this instead?
    //$id = (int)$_GET['id'];
    <td><a href = "person_c.php?id=<?php echo $id;?>">Modify User </a></td>
    [/code]


    Query the database by adding there WHERE id = $id

    [B]index.php[/B]
    [code=php]
    $id = (int)$_GET['id'];
    $data = mysql_query ("SELECT persons.id, persons.name AS persons_name, persons.surname, persons.address, persons.mobile,
    dept.dept_name FROM persons LEFT JOIN dept ON persons.dept_id = dept.id ORDER BY persons.id DESC WHERE id = ". $id) or die (mysql_error()); //Line 21
    [/code]


    Place the values from the database into the inputs

    [B]index.php[/B]
    [code=php]
    ..
    ..
    <td><input type="text" name="name" value="<?php echo $row['surname'];?>" /></td>
    <td><input type="text" name="surname" value="<?php echo $row['surname'];?>" /></td>
    <td><input type="text" name="dept" value="<?php echo $row['dept'];?>" /></td>
    ..
    ..
    [/code]


    But, then when I run I got an error: I am not sure if am doing it correct?

    [code=php]
    Notice: Undefined index: id on line 21
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id = 0' at line 2
    [/code]
    Copy linkTweet thisAlerts:
    @DexterMorganJan 25.2011 — Sorry, step 2 and further i should have put on the person page.

    The index you should keep as :

    [code=php] <!--modify.php?= (showing the id)-->
    <td><a href = "person.php?id=<?php echo $row['id'];?>">Modify User </a></td>
    <td>
    <a href= "deleteConfirm.php?id= <?php echo $row['id'];?>">Delete User</a></td>[/code]

    Then on the person page you will be able to access the id by doing $id = $_GET['id'];

    sorry about the error.
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 25.2011 — Ok, let me try again ? but it still does not working...


    [B]person.php[/B] query remind the same
    [code=php]
    if (!isset($_POST['submit']))
    {
    $edit_sql= "SELECT * FROM persons WHERE ID = $id";
    echo $edit_sql;
    $result = mysql_query($edit_sql) or die (mysql_error());
    $row = mysql_fetch_array($result);
    }
    [/code]


  • 2. on the [B]index page[/B] get the id by doing $id = $_GET['id']
    [code=php]
    Where do I put this in person page?
    [/code]





  • 3. query the database again adding a where id = $id

    [B]index.php[/B]
    [code=php]
    $id = (int)$_GET['id'];
    $data = mysql_query ("SELECT persons.id, persons.name AS persons_name, persons.surname, persons.address, persons.mobile,
    dept.dept_name FROM persons LEFT JOIN dept ON persons.dept_id = dept.id ORDER BY persons.id DESC WHERE id = ". $id) or die (mysql_error());
    [/code]


  • 4. place the values from the database into the inputs (these values already as they are before)

    [B]person.php[/B]
    [code=php]
    ...
    ...
    <table width="300" border="1">
    <h1> Modifying A User </h1>

    <tr>
    <th scope="row">Name</th>
    <td><input type="text" name="name" value="<?php echo $_SESSION['name'];?>" /></td>
    </tr>

    <tr>
    <th scope="row">Surname</th>
    <td>
    <input type="text" name="surname" value="<?php echo $_SESSION['surname']; ?>" /></td>
    </tr>

    <tr>
    <th scope="row">Address</th>
    <td><input type="text" name="add" value="<?php echo $_SESSION['address']; ?>" /></td>
    </tr>


    <tr>
    <th scope="row">Department</th>
    <td><input type="text" name="add" value="<?php echo $_SESSION['dept']; ?>" /></td>
    </tr>

    </table>
    ...
    ...
    [/code]
  • Copy linkTweet thisAlerts:
    @siabanieauthorJan 25.2011 — Just another question: Maybe what Im trying to achieve is not feasible? Because what I want to achieve here is:

    I have two PHP pages

    1. create.php

    2. modify.php

    and two confirmation pages

    3. confirmCreate.php

    4. confirmModify.php

    then then index.php (Finishing)

    I want to combine the create page and modify page make it one page (person.php) then make another page for the confirmation page for both of these pages where the query will be added here (UPDATE and INSERT) but I am not so sure if I have to use the SESSION function in confirmation page or person page? Or can I use SESSION both of the pages (person.php and confirmation page) ?
    Copy linkTweet thisAlerts:
    @DexterMorganJan 25.2011 — 
  • 1. stop using sessions on the index.php

  • 2. on the person page get the id by doing $id = (int)$_GET['id'];

  • 3. query the database again adding a where id = $id (person page)
    [code=php]if (!isset($_POST['submit']))
    {
    $edit_sql= "SELECT * FROM persons WHERE ID = $id";
    echo $edit_sql;
    $result = mysql_query($edit_sql) or die (mysql_error());
    $row = mysql_fetch_array($result);
    } [/code]


  • [code=php]if (!isset($_POST['submit'])) [/code]
    will not work as there will be no post submit so just get rid of that.


  • 4. place the values from the database into the inputs (person page)


  • [code=php]<table width="300" border="1">
    <h1> Modifying A User </h1>

    <tr>
    <th scope="row">Name</th>
    <td><input type="text" name="name" value="<?php echo $_SESSION['name'];?>" /></td>
    </tr>

    <tr>
    <th scope="row">Surname</th>
    <td>
    <input type="text" name="surname" value="<?php echo $_SESSION['surname']; ?>" /></td>
    </tr>

    <tr>
    <th scope="row">Address</th>
    <td><input type="text" name="add" value="<?php echo $_SESSION['address']; ?>" /></td>
    </tr>


    <tr>
    <th scope="row">Department</th>
    <td><input type="text" name="add" value="<?php echo $_SESSION['dept']; ?>" /></td>
    </tr>

    </table>[/code]


    replace the echo $_SESSION['...'] with $row['...'] in the above code.

    You should now be able to see values in the persons page.

    Yeah what you want to do is possible.
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 25.2011 — I still not able to get all the values in the persons page. It said Undefined variable: id..

    Error:
    [code=php]
    Array
    (
    [name] =>
    [surname] =>
    [department] =>
    [address] =>
    [user] => Test
    [authuser] => 1
    [id] =>
    [dept] =>
    [num] => 9
    )

    Array
    (
    )


    Notice: Undefined variable: id on line 23
    SELECT * FROM persons WHERE ID = You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
    [/code]


    EDIT:

    [B]person.php[/B]
    [code=php]
    <?php
    session_start();
    require 'includes/application_top.php';

    $id = (int) $_GET['id'];

    $edit_sql= "SELECT * FROM persons WHERE ID = ". $id;
    $result = mysql_query($edit_sql) or die (mysql_error());
    $row = mysql_fetch_array($result);
    ?>

    <html><head>
    <title>Modify and Create Document</title>
    </head>

    <body>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <div align="center">
    <table width="300" border="1">
    <h1> Modifying A User </h1>

    <tr>
    <th scope="row">Name</th>
    <td><input type="text" name="name" value="<?php $row['name'];?>" /></td>
    </tr>

    <tr>
    <th scope="row">Surname</th>
    <td>
    <input type="text" name="surname" value="<?php $row['surname']; ?>" /></td>
    </tr>

    <tr>
    <th scope="row">Address</th>
    <td><input type="text" name="add" value="<?php echo $row['address']; ?>" /></td>
    </tr>

    <!--Department-->
    <tr>
    <th scope="row">Department</th>
    <td>

    <select name="dept">
    <option value="">Select..</option>

    <?php
    $data = mysql_query ("SELECT * FROM dept ORDER BY id DESC") or die (mysql_error());

    echo $data;

    while($row_dept = mysql_fetch_array( $data ))
    {
    ?>
    <option value="<?php echo $row_dept['id'] ;?>" <?php if($row_dept['id']==$dept){echo ' selected="selected"';}?>> // LINE 105
    <?php echo $row_dept['dept_name'] ;?>
    </option>

    <?php
    }
    ?>
    </select>
    </td>
    </tr>

    <!--End-->


    </table>
    <br/>

    <a href="index.php">
    <input type="button" name="back" value="Back" /></a>

    <!--Getting the id from the main page (index.php)-->
    <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">

    <input type="submit" name="submit" value="Modify"/>

    </div>
    </form>
    </body>
    </html>


    [/code]


    EDIT:

    [B]index.php[/B]
    [code=php]
    <?php
    session_start();
    require 'includes/application_top.php';

    $data = mysql_query ("SELECT persons.id, persons.name, persons.surname, persons.address, persons.mobile,
    dept.dept_name FROM persons LEFT JOIN dept ON persons.dept_id = dept.id ORDER BY persons.id DESC") or die (mysql_error());

    //echo $data;

    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Friends Document</title>
    </head>

    <body>
    <form action="person.php" method="post">
    <table border="0" cellpadding="2">
    <tr>
    <td>
    <p><a href="person_cms.php">Create User </a></p>
    </td>

    <td>
    <p><a href="logout.php">Log Out</a><br /></p>
    </td>
    </tr>
    </table>


    <table border="2" cellpadding="3">

    <tr>
    <th>
    ID
    </th>
    <th>First Name</th>
    <th>Surname</th>
    <th>Address</th>
    <th>Department</th>
    <th>Phone</th>
    <th>Edit Link</th>
    <th>Delete Link</th>
    </tr>
    <?php

    while($row = mysql_fetch_array( $data ))
    {
    ?>
    <tr>
    <td><?php echo $row['id'];?></td>


    <td>
    <?php echo $row['name'];?>


    <?php
    if (isset($_GET['id']) && (isset($_GET['page'])))
    {
    if ($_GET['id'] == $row['id'] && $_GET['page'] == "create")
    {
    echo "Created";
    }

    if ($_GET['id'] == $row['id'] && $_GET['page'] == "modify")
    {
    echo "Modified";
    }
    }
    ?>
    </td>

    <!--Printing and pulling all the data-->
    <td><?php echo $row['surname'];?></td>
    <td><?php echo $row['address'];?></td>
    <td><?php echo $row['dept_name'];?></td>
    <td><?php echo $row['mobile'];?></td>

    <!--modify.php?= (showing the id)-->
    <td><a href = "person_c.php?id=<?php echo $row['id'];?>">Modify User </a></td>

    <td>
    <a href= "deleteConfirm.php?id= <?php echo $row['id'];?>">Delete User</a></td>

    </tr>

    <?php
    }
    ?>
    </table>
    </form>
    </body>
    </html>

    [/code]
    Copy linkTweet thisAlerts:
    @DexterMorganJan 25.2011 — Remove the session and post stuff that is before $edit_sql

    Place $id = (int) $_GET['id']; before $edit_sql
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 26.2011 — Getting there but not quite - I now can pull the data but only for address value, I got empty values for name, surname is that because as we not using session as no values have been pass on and an error for department value 'dept'

    Please review back at post #25 [B]EDIT[/B] for latest code of index.php and person.php - I noticed that we did not use any function SESSION here, would that cause empty values for some of the form?

    Error:
    [code=php]
    <b>Notice</b>: Undefined variable: dept on line <b>105

    <option value="<?php echo $row_dept['id'] ;?>" <?php if($row_dept['id']==$dept){echo ' selected="selected"';}?>> // LINE 105
    [/code]
    Copy linkTweet thisAlerts:
    @DexterMorganJan 26.2011 — try using the pre function on $row_dept
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 26.2011 — pre function like this:

    EDIT:

    [B]person.php[/B]
    [code=php]
    <?php
    $data = mysql_query ("SELECT * FROM dept ORDER BY id DESC") or die (mysql_error());

    echo $data;

    while($row_dept = mysql_fetch_array( $data ))
    echo $row_dept;
    {
    ?>
    <option value="<?php echo $row_dept['id'] ;?>" <?php if($row_dept['id']==$dept){echo ' selected="selected"';}?>>
    <?php echo $row_dept['dept_name'] ;?>
    [/code]


    I changed the query a bit but it still giving me an error this time 'ID' and there is no form shown on page.

    EDIT

    [B]person.php[/B]
    [code=php]
    SELECT persons.id, persons.name, persons.surname, persons.address, persons.mobile, dept.dept_name FROM persons LEFT JOIN dept ON persons.dept_id = dept.id WHERE ID = 206Column 'ID' in where clause is ambiguous
    [/code]


    EDIT

    [B]person.php[/B]

    Error in SQL indicated on 'ID'
    [code=php]
    $edit_sql = "SELECT persons.id, persons.name, persons.surname, persons.address, persons.mobile,
    dept.dept_name FROM persons LEFT JOIN dept ON persons.dept_id = dept.id WHERE ID = ". $id;

    [/code]


    I don't understand how we going to pass on the values from index page to persons page as we not using the session? I used the function $_GET and $_POST before and it worked but I want to use the session variables for this purpose.
    Copy linkTweet thisAlerts:
    @DexterMorganJan 26.2011 — No, the pre function was:

    [code=php]
    function pre($v){
    echo '<pre>';
    print_r($v);
    echo '</pre>';
    }
    [/code]


    which page are you showing me there?

    The idea is:

    you have a load of links on the index page that take you to the person page, these links have an id in them.

    On the person page you get the id out of the url using: $id = $_GET['id']

    Then, still on the person page, you query the database saying "SELECT * FROM dept WHERE id = $id LIMIT 1"

    Then you will enter the values from that query into the inputs.

    How do you want to use sessions? I do not understand, you said there are going to be a lot of rows on the index page, you want to store all of these with sessions?
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 26.2011 — The page I shown here is on the person page. #EDIT - Please look at post #29 as I have paste the whole code for person.php page.

    I have attached photos of the index page, modify page and create page with confirmation pages for each modify and create. These pages are done using $_POST and $_GET method but I want to change the code now using session function by combining the create page and modify page into one page only but still doing the same functionality. Then, I also want use only one confirmation page instead of having two pages which are create confirm and modify confirm, again using session.

    I hope what I'm trying to explain here make sense.

    ADD: post#29 not allow me to edit so I put the person.php code here instead.

    [B]person.php[/B]
    [code=php]
    <?php
    require 'includes/application_top.php';

    function pre($v) {
    echo '<pre>';
    print_r($v);
    echo '</pre>';
    }

    $id = (int)$_GET['id'];

    $edit_sql = "SELECT persons.id, persons.name, persons.surname, persons.address, persons.mobile,
    dept.dept_name FROM persons LEFT JOIN dept ON persons.dept_id = dept.id WHERE ID = ". $id;

    $result = mysql_query($edit_sql) or die (mysql_error());

    $row = mysql_fetch_array($result);

    ?>

    <html><head>
    <title>Modify Document</title>
    </head>

    <body>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <div align="center">
    <table width="300" border="1">
    <h1> Modifying A User </h1>

    <tr>
    <th scope="row">Name</th>
    <td><input type="text" name="name" value="<?php echo $row['name'];?>" /></td>
    </tr>

    <tr>
    <th scope="row">Surname</th>
    <td>
    <input type="text" name="surname" value="<?php echo $row['surname']; ?>" /></td>
    </tr>

    <tr>
    <th scope="row">Address</th>
    <td><input type="text" name="add" value="<?php echo $row['address']; ?>" /></td>
    </tr>

    <!--Department-->
    <tr>
    <th scope="row">Department</th>
    <td>

    <select name="dept">
    <option value="">Select..</option>

    <?php
    $data = mysql_query ("SELECT * FROM dept ORDER BY id DESC") or die (mysql_error());

    while($row_dept = mysql_fetch_array( $data ))
    {
    ?>
    <option value="<?php echo $row_dept['id'] ;?>" <?php if($row_dept['id']==$dept){echo ' selected="selected"';}?>>
    <?php echo $row_dept['dept_name'] ;?>
    </option>

    <?php
    pre($row_dept);

    ?>
    <?php
    }
    ?>
    </select>
    </td>
    </tr>

    <!--End-->


    <!--tr>
    <th scope="row">Department</th>
    <td><input type="text" name="add" value="<?php echo $row['dept_id']; ?>" /></td>
    </tr-->

    </table>
    <br/>

    <a href="index.php">
    <input type="button" name="back" value="Back" /></a>

    <!--Getting the id from the main page (index.php)-->
    <input type="hidden" name="id" value="<?php echo $id; ?>">

    <input type="submit" name="submit" value="Modify"/>

    </div>
    </form>
    </body>
    </html>

    [/code]


    [upl-file uuid=655cad85-9e46-4fc4-af2e-1c83d5628432 size=51kB]index.jpg[/upl-file]

    [upl-file uuid=1f53d6e6-65df-4bc7-bbfb-83d0d8466d6e size=34kB]modify.jpg[/upl-file]

    [upl-file uuid=f3eb58e8-070e-42ad-92f4-c2d053c89ef7 size=37kB]confirmModifypage.jpg[/upl-file]

    [upl-file uuid=82ed9156-993f-4ce5-bd95-eff7c13e5bab size=30kB]create.jpg[/upl-file]

    [upl-file uuid=437df886-4ab4-47e4-9847-414d10c66a2c size=38kB]confirmCreatepage.jpg[/upl-file]
    Copy linkTweet thisAlerts:
    @gavshouseJan 26.2011 — Why is the modify.php posting to itself ?

    [code=html]<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">[/code]

    You dont have any code in modify.php to handle this post data, im guessing you want to post to another page where it sorts it out.
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 26.2011 — Yes, this

    [code=php]
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    [/code]


    should be go to the confirmation page (to confirm if the data that has been edited is correct) then after submitted the values pass to the final page (index page) with the current edited values.

    [code=php]
    <form action="confirmation.php" method="post">
    [/code]


    The confirmation page will have query (UPDATE for the modify and INSERT for the create) - but even though at the moment I'm still not getting any values for my person page form....
    Copy linkTweet thisAlerts:
    @gavshouseJan 26.2011 — So whats the issue now ? or is it all ok
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 26.2011 — Well the person page form still not extracting all the values from index page. I am not sure where was wrong...?
    Copy linkTweet thisAlerts:
    @DexterMorganJan 26.2011 — Well the person page form still not extracting all the values from index page. I am not sure where was wrong...?[/QUOTE]
    The person page is only getting one value from the index page as you are only sending one value from the index page, which is $id
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 26.2011 — That means I should use the session variables so I can send the rest of the values to person page?

    But you suggested we did not need session on the index page and person page...? Then if we not using session How we going to send the values...?
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 26.2011 — I managed to pulled the data from index page eventually apart from the department name did not pull and an error on line 51.

    Can anyone trace what I did wrong?

    [code=php]
    Notice: Undefined variable: dept on line 51
    [/code]


    [B]person.php[/B]
    [code=php]
    ...
    ...

    <!--Department-->
    <tr>
    <th scope="row">Department</th>
    <td>

    <select name="dept">
    <option value="">Select..</option>

    <?php
    $data = mysql_query ("SELECT * FROM dept ORDER BY id DESC") or die (mysql_error());

    while($row_dept = mysql_fetch_array( $data ))
    {
    ?>
    <option value="<?php echo $row_dept['id'] ;?>" <?php if($row_dept['id']==$dept){echo ' selected="selected"';}?>> //Line 51
    <?php echo $row_dept['dept_name'] ;?>
    </option>
    <?php
    }
    ?>
    </select>
    </td>
    </tr>
    <!--Department end-->
    ...
    ...
    [/code]



    [B]EDIT:[/B]

    I add this new query here: But now the drop down menu did not give me any option but 'Select..'...?
    [code=php]
    ...
    ...
    <!--Department-->
    ...
    ...
    $data = ("SELECT * FROM dept ORDER BY id DESC WHERE id = $dept")or die (mysql_error());

    while($row_dept = mysql_fetch_array( $data ))
    {
    ?>
    <option value="<?php echo $row_dept['id'] ;?>" <?php if($row_dept['id']==$dept){echo ' selected="selected"';}?>> //Line 51
    <?php echo $row_dept['dept_name'] ;?>
    </option>
    <?php
    }
    ?>
    </select>
    </td>
    </tr>
    <!--Department end-->
    ...
    [/code]
    Copy linkTweet thisAlerts:
    @DexterMorganJan 26.2011 — We have already covered this.

    So you want to save every single row from the database using sessions?

    If you can get the data in the index from the database why can you not do it again but more specifically adding a WHERE clause on the person page?
    Copy linkTweet thisAlerts:
    @criterion9Jan 26.2011 — If you can get the data in the index from the database why can you not do it again but more specifically adding a WHERE clause on the person page?[/QUOTE]
    For what it is worth I would discourage using sessions as you could then link to the page from multiple places without requiring session variables. The most logical choice is to pull the single record from the database based on the ID sent to the page.
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 26.2011 — For what it is worth I would discourage using sessions as you could then link to the page from multiple places without requiring session variables. The most logical choice is to pull the single record from the database based on the ID sent to the page.[/QUOTE]

    Well I have done these before using $_POST but I end up 4 different pages (create page, modify page, confirmModify page, confirmCreate page) but they said if I am using sessions I can reduce the page by two which are (modify+create.php and comfirmation.php)

    We have already covered this.

    So you want to save every single row from the database using sessions?

    If you can get the data in the index from the database why can you not do it again but more specifically adding a WHERE clause on the person page?[/QUOTE]


    Yes I want to use sessions to save every single row, I used $_POST before and they told me that I can use sessions. If you look at the photos I have attached the create page and modify page almost the same but two different pages and now I like to make it only one page (modify+create) using sessions but does two different things.

    I have [B]EDIT[/B] the [B]post #38[/B] at the query part adding WHERE clause. But now all I got for the department form is an option "Select..." but no values. ??
    Copy linkTweet thisAlerts:
    @DexterMorganJan 26.2011 — Alright ok.

    The query you are doing in the person page is not the same one as from the index page.

    Alright, you can still have one page.

    A very simple way of doing it would be :

    You know that if the user wants to modify a person then the person page will have an id passed to it via GET

    When a user wants to create a person then the id will not be there, agreed?

    It would be as simple as
    [code=php]if(isset($_GET['id']){
    //they want to modify
    }else{
    //they want to create
    }[/code]
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 26.2011 — 
    You know that if the user wants to modify a person then the person page will have an id passed to it via GET

    When a user wants to create a person then the id will not be there, agreed?
    [/QUOTE]


    Yes I agree, but how we going to achieve this using sessions? Plus, I still have not getting the value from the department name form. I do not understand why...?

    For you info my Table Database will look like this:

    Table PERSONS has field (id, name, surname, address, dept_id)

    Table DEPT has field (id, dept_name)

    Note: persons.dept_id == dept.id
    Copy linkTweet thisAlerts:
    @DexterMorganJan 26.2011 — Yes I agree, but how we going to achieve this using sessions?[/QUOTE]
    We have achieved it there using GET, why do we need to achieve it using sessions?

    You do not need sessions in this case, who is this 'they' who are saying use sessions, you could do what you want to do with $_GET and $_POST if you do it correctly.

    The index.php and persons.php SQL queries are different, can you see on the index.php you do a query with a JOIN, this is how you are getting the dept name.

    The SQL query that you do in persons.php does not JOIN anything, this is why you do not get a dept name.
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 26.2011 — I am not sure if this makes sense but would it be possible for me to do something like this and maybe add some sessions functions:

    [code=php]
    if (isset ($_GET['id']) {
    $_SESSION ['page'] = 'modify';
    } else
    $_SESSION ['page'] = 'created';
    [/code]


    Also I was wondering if we can use foreach (I still not fully master this function) something like:

    [code=php]
    foreach ($_POST as $key =>$val) {
    $_SESSION[$key] = $val;
    }
    [/code]


    to sorted our inputs form on the create+modify page or other purpose? Do we need to declare the all the variables that we will be using when sessions is used? or even register sessions variables? (name, surname etc)
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 26.2011 — We have achieved it there using GET, why do we need to achieve it using sessions?

    You do not need sessions in this case, who is this 'they' who are saying use sessions, you could do what you want to do with $_GET and $_POST if you do it correctly.

    The index.php and persons.php SQL queries are different, can you see on the index.php you do a query with a JOIN, this is how you are getting the dept name.

    The SQL query that you do in persons.php does not JOIN anything, this is why you do not get a dept name.[/QUOTE]


    Yes we have achieved these forms using $_GET and $_POST as the photos shown as well but I would like to use different using method "sessions" as my colleague told me that I can do these using sessions and I like to know how it can be done...

    I saw some examples how does sessions work but those variables has been set and fixed ie: $_SESSION ['name'] = 'Michael'; but the one Im trying to working on is getting the values from the database.

    I tried to put the same query in persons.php as :

    [code=php]
    ..
    ..
    $data = "SELECT persons.id, persons.name, persons.surname, persons.address, persons.mobile,
    dept.dept_name FROM persons LEFT JOIN dept ON persons.dept_id = dept.id ORDER BY persons.id DESC WHERE ID = $_GET[id]";

    $result = mysql_query($data) or die (mysql_error());

    $row = mysql_fetch_array($result);
    ..
    [/code]


    But, I got an error:

    [code=php]
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ID = 205' at line 2
    [/code]
    Copy linkTweet thisAlerts:
    @DexterMorganJan 26.2011 — [code=php]
    $id = (int) $_GET['id'];
    $data = "SELECT persons.id, persons.name, persons.surname, persons.address, persons.mobile,
    dept.dept_name FROM persons LEFT JOIN dept ON persons.dept_id = dept.id WHERE persons.id = $id ORDER BY persons.id DESC";
    [/code]

    Try that.

    You do not need to use sessions.

    edit:

    persons.mobile, where has this come from?

    back at post #43 you said:

    For you info my Table Database will look like this:

    Table PERSONS has field (id, name, surname, address, dept_id)

    Table DEPT has field (id, dept_name)[/QUOTE]


    there is no mobile field there.
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 26.2011 — Yes you were right about the mobile field I just removed them. The person page now works but the problem still getting the department name. The form for department still not getting the values and giving me an error:

    [code=php]
    Undefined variable: dept on line 54
    [/code]


    Which is this line at persons page department query: as shown more back at post #38
    [code=php]
    ..
    ..
    <!--Department-->
    <option value="<?php echo $row_dept['id'] ;?>" <?php if($row_dept['id']==$dept){echo ' selected="selected"';}?>>
    ..
    ..
    [/code]
    Copy linkTweet thisAlerts:
    @DexterMorganJan 26.2011 — Please can you paste all of the code again.
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 26.2011 — I have added the mobile now so the database is correct. I still not able to get the values for the department name. And I was wondering if we can use sessions for this? as I will need to do a create page form on the same page of persons.php page.

    Here are the latest codes:

    [B]index.php[/B]
    [code=php]
    <?php
    require 'includes/application_top.php';

    $data = mysql_query ("SELECT persons.id, persons.name, persons.surname, persons.address, persons.mobile,
    dept.dept_name FROM persons LEFT JOIN dept ON persons.dept_id = dept.id ORDER BY persons.id DESC") or die (mysql_error());

    echo $data;

    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Friends Document</title>
    </head>

    <body>
    <form action="persons.php" method="post">
    <table border="0" cellpadding="2">
    <tr>
    <td>
    <p><a href="person_cms.php">Create User </a></p>
    </td>

    <td>
    <p><a href="logout.php">Log Out</a><br /></p>
    </td>
    </tr>
    </table>


    <table border="2" cellpadding="3">

    <tr>
    <th>
    ID
    </th>
    <th>First Name</th>
    <th>Surname</th>
    <th>Address</th>
    <th>Department</th>
    <th>Phone</th>
    <th>Edit Link</th>
    <th>Delete Link</th>
    </tr>
    <?php

    while($row = mysql_fetch_array( $data ))

    { echo $row;
    ?>
    <tr>
    <td><?php echo $row['id'];?></td>


    <td>
    <?php echo $row['name'];?>


    <?php
    if (isset($_GET['id']) && (isset($_GET['page'])))
    {
    if ($_GET['id'] == $row['id'] && $_GET['page'] == "create")
    {
    echo "Created";
    }

    if ($_GET['id'] == $row['id'] && $_GET['page'] == "modify")
    {
    echo "Modified";
    }
    }
    ?>
    </td>

    <?php /*?><?php if($row_dept['id']==$row['dept_id']){echo ' selected="selected"';}?><?php */?>

    <!--Printing and pulling all the data-->
    <td><?php echo $row['surname'];?></td>
    <td><?php echo $row['address'];?></td>
    <td><?php echo $row['dept_name'];?></td>
    <td><?php echo $row['mobile'];?></td>

    <!--modify.php?= (showing the id)-->
    <td><a href = "person_c.php?id=<?php echo $row['id'];?>">Modify User </a></td>

    <td>

    <a href= "deleteConfirm.php?id= <?php echo $row['id'];?>">Delete User</a></td>

    </tr>

    <?php
    }
    ?>
    </table>
    </form>
    </body>
    </html>
    [/code]


    [B]persons.php[/B]
    [code=php]
    <?php

    require 'includes/application_top.php';

    $id = (int) $_GET['id'];
    $q = "SELECT persons.id, persons.name, persons.surname, persons.address, persons.mobile,
    dept.dept_name FROM persons LEFT JOIN dept ON persons.dept_id = dept.id WHERE persons.id = $id ORDER BY persons.id DESC";
    $result = mysql_query($q) or die (mysql_error());
    $row = mysql_fetch_array($result);

    ?>
    <html><head>
    <title>Modify Document</title>
    </head>

    <body onLoad="document.confirmationPage.submit();">
    <form name="confirmationPage" action="confirmPage.php" method="post">
    <div align="center">
    <table width="300" border="1">
    <h1> Modifying A User </h1>

    <tr>
    <th scope="row">Name</th>
    <td><input type="text" name="name" value="<?php echo $row['name'];?>" /></td>
    </tr>

    <tr>
    <th scope="row">Surname</th>
    <td>
    <input type="text" name="surname" value="<?php echo $row['surname']; ?>" /></td>
    </tr>

    <tr>
    <th scope="row">Address</th>
    <td><input type="text" name="add" value="<?php echo $row['address']; ?>" /></td>
    </tr>

    <!--Department-->
    <tr>
    <th scope="row">Department</th>
    <td>

    <select name="dept">
    <option value="">Select..</option>

    <?php
    $data = mysql_query ("SELECT * FROM dept ORDER BY id DESC") or die (mysql_error());

    while($row_dept = mysql_fetch_array( $data ))
    {
    ?>
    <option value="<?php echo $row_dept['id'] ;?>" <?php if($row_dept['id']==$dept){echo ' selected="selected"';}?>>
    <?php echo $row_dept['dept_name'] ;?>
    </option>
    <?php
    }
    ?>
    </select>
    </td>
    </tr>
    <!--Department end-->
    <tr>
    <th scope="row">Phone</th>
    <td><input type="text" name="add" value="<?php echo $row['mobile']; ?>" /></td>
    </tr>

    </table>
    <br/>

    <a href="index.php">
    <input type="button" name="back" value="Back" /></a>

    <!--Getting the id from the main page (index.php)-->
    <input type="hidden" name="id" value="<?php echo $id; ?>">

    <input type="submit" name="submit" value="Modify"/>

    </div>
    </form>
    </body>
    </html>

    [/code]
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 26.2011 — I got the error:

    persons.php
    [code=php]
    Undefined variable: dept on line 57
    [/code]


    Which line 57 is: (persons.php)

    [code=php]
    ..
    ..
    <option value="<?php echo $row_dept['id'] ;?>" <?php if($row_dept['id']== $dept){echo ' selected="selected"';}?>>
    ..
    ..
    [/code]


    and still at persons.php, I tried to defined and placed it just after the

    $row = mysql_fetch_array(result)


    [code=php]
    $dept = $row['dept_id'];
    [/code]


    But it's still not working - I am still wondering how I am going to do this in sessions?
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 27.2011 — Right, I have change slightly what I need to achieve - I would like to have one form which will be able to create a new user and modify the user. And when it submitted it will go to another page 'confirm page' which will be using sessions.

    On the confirm page; it will be using UPDATE and INSERT query and using sessions. Can anyone help me with this?

    Here is one of my createConfirm.php code and I will be needed another for modify confirm but same page in other word: this page I will call confirmPage.php How can I covert this createConfirm so it will be able to do create confirm and modify confirm page at the same time using sessions?

    [B]createConfirm.php[/B]
    [code=php]
    <?php
    require 'includes/connection.php';

    $name = "";
    $surname = "";
    $add = "";
    $dept = "";
    $mobile = "";

    $name = $_POST['name'];
    $surname = $_POST['surname'];
    $add = $_POST['add'];
    $dept = $_POST['dept'];
    $mobile = $_POST['mobile'];

    if (isset($_POST['submit'])) {


    $create_sql = "INSERT INTO persons SET Name='$name', Surname='$surname', dept_id='$dept', Address='$add', Mobile='$mobile' ";

    $confirm_create = mysql_query ($create_sql) or die (mysql_error());

    header("Location: index.php?id=".mysql_insert_id()."&page=create");
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Confirm Create</title>
    </head>

    <body>

    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
    <div align="center">
    <table width="200" border="1">
    <h1>Are You Happy With These Info?</h1>
    <tr>
    <th scope="row">Name</th>
    <td><input type="text" name="name" value="<?php echo $name;?>" /> </td>
    </tr>

    <tr>
    <th scope="row">Surname</th>
    <td><input type="text" name="surname" value="<?php echo $surname;?>" /></td>
    </tr>

    <tr>
    <th scope="row">Address</th>
    <td><input type="text" name="add" value="<?php echo $add;?>" /></td>
    </tr>


    <tr>
    <th scope="row">Department</th>
    <td>

    <select name="dept">
    <option value="">Select..</option>

    <?php
    $data = mysql_query ("SELECT * FROM dept ORDER BY id DESC") or die (mysql_error());

    while($row_dept = mysql_fetch_array( $data ))
    {
    ?>
    <option value="<?php echo $row_dept['id'] ;?>" <?php if($row_dept['id']==$dept){echo ' selected="selected"';}?>>
    <?php echo $row_dept['dept_name'] ;?>
    </option>

    <?php
    }
    ?>
    </select>
    </td>
    </tr>

    <tr>
    <th scope="row">Mobile</th>
    <td><input type="text" name="mobile" value="<?php echo $mobile;?>" /> </td>
    </tr>

    <tr>
    <br/ >
    <td colspan="2" align="center"><p>
    <input type="button" value="Back" onclick="history.go(-1)" />

    <input type="submit" name="submit" value="Submit"/>
    </p></td>
    </tr>
    </table>

    </div>
    </form>

    </body>
    </html>

    [/code]
    Copy linkTweet thisAlerts:
    @criterion9Jan 27.2011 — And when it submitted it will go to another page 'confirm page' which will be using sessions.[/QUOTE]
    How about we take a couple steps back and ask the first question. Are you familiar with what sessions are? Do you know what they are used for?
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 28.2011 — How about we take a couple steps back and ask the first question. Are you familiar with what sessions are? Do you know what they are used for?[/QUOTE]

    Well, I never use sessions before but I saw some examples on-line and as far as I understand it's like cookies - when sessions start the variables will stores within the browser until we quit or end the sessions. If I'm wrong please correct me.
    Copy linkTweet thisAlerts:
    @criterion9Jan 28.2011 — Well, I never use sessions before but I saw some examples on-line and as far as I understand it's like cookies - when sessions start the variables will stores within the browser until we quit or end the sessions. If I'm wrong please correct me.[/QUOTE]

    This is correct, however, this is usually not used for the use you are trying to use it for. Usually we poll the DB for a dataset and provide links the view/update/delete the record by passing the id to the appropriate action page (which may or may not be the same page with an action appended). The page then requests a single entity from the DB and presents it appropriately. If you were to put the entire dataset into session storage and never poll the DB before completing any actions on that data you may not be using the most current data.

    As an example: John loads the dataset. Jane loads the dataset. John deletes a record. Jane edits the same record John just deleted. When Jane tries to save the edits the DB cannot update the record because it doesn't exist.
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 28.2011 — Thanks for the feedback, the one that I am trying to do here is updating the record so instead of doing $_POST which I done before I would like to change it using sessions now as I think it can be done and will help me to understand it too.

    I have changed the code slight and managed to pull all the data, but the only problem I face is pull the department value. I think my query isn't correct here as you can see at line 52. Can you please help me what I did wrong in here?


    Thanks.

    Error:
    [code=php]
    Notice: Undefined variable: dept on line 52
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

    [/code]


    Cut-down version
    [code=php]
    <?php
    session_start();
    ..
    ..

    <?php

    $data = mysql_query ("SELECT dept_name FROM dept WHERE id = $dept") or die (mysql_error()); // Line 52

    $row_dept= mysql_fetch_array( $data );

    echo $_SESSION['dept_name'];
    ?>

    </td>
    </tr>

    <!--End Department-->..
    ..
    [/code]
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 28.2011 — I have sorted the query sessions thanks..!?
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 28.2011 — I wonder if there is a way I can combine my two forms (create and modify pages) into one form only which can do these functions?

    Any ideas?
    Copy linkTweet thisAlerts:
    @criterion9Jan 28.2011 — Given URLs like the following:

    page.php?action=edit&id=##

    page.php?action=insert

    etc...


    Something like this would allow you to organize your code appropriately:
    [code=php]
    if(isset($_GET['action'])){
    switch($_GET['action']){
    case "edit":
    //all stuff for edit action
    break;
    case "insert":
    default: //fall through intentional as this will be the default action
    //display blank form
    break;
    }
    }
    [/code]


    Please note:

    There is still no need for sessions here. Typically I find that the only stuff I store in sessions are the tokens to assist in verifying that the forms are originating from the same domain, the username or user id, and maybe some data that needs to be shared with all pages in the application. I have never found it useful to use sessions to pass variables between 2 forms.
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 28.2011 — Thanks for the inputs, well I have created the modify form here but I still a bit confused where do I have to place the code for create form? Is that mean I will have to do two forms using switch statement?

    I think I can use the sessions on the confirmation pages as that page will store values from this page (modify+create page).

    Here is the modify form page:
    [code=php]
    <?php

    require 'includes/application.php';
    //Define
    $name = "";
    $surname = "";
    $add = "";
    $dept= "";
    $mobile = "";

    if (!isset($_POST['goSubmit']))
    {

    $q = "SELECT * FROM persons WHERE ID = $_GET[id]";

    $result = mysql_query($q) or die (mysql_error());
    $row = mysql_fetch_array($result);

    $name = $row['name'];
    $surname = $row['surname'];
    $address = $row['address'];
    $dept = $row['dept_id'];
    $mobile = $row['mobile'];

    }

    if (isset($_POST['goSubmit']))
    {
    $name = $_POST['name'];
    $surname = $_POST['surname'];
    $address = $_POST['address'];
    $dept = $_POST['dept'];
    $mobile = $_POST['mobile'];

    $errormsg = "";

    if($name == "")
    $errormsg = $errormsg. "Name<br/ >";

    if ($surname == "")
    $errormsg = $errormsg. "Surname Please <br/ >";

    if ($mobile != "" && !is_numeric ($mobile))
    $errormsg = $errormsg. "Mobile No? <br/ >";

    if ($errormsg != "")
    echo "Please fill the blank info: <br/ > $errormsg";

    else
    {
    $input_field = "";


    foreach ($_POST as $key => $val)
    {
    if ($key != 'goSubmit')
    {
    $_SESSION[$key] = $val;
    }
    }

    header("Location: confirmPage.php");
    }
    }

    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Modify Document</title>
    </head>

    <body>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <div align="center">
    <table width="300" border="1">
    <h1> Modifying A User </h1>

    <tr>
    <th scope="row">Name</th>
    <td><input type="text" name="name" value="<?php echo $name;?>" /></td>
    </tr>

    <tr>
    <th scope="row">Surname</th>
    <td>
    <input type="text" name="surname" value="<?php echo $surname; ?>" /></td>
    </tr>

    <tr>
    <th scope="row">Address</th>
    <td><input type="text" name="address" value="<?php echo $address; ?>" /></td>
    </tr>


    <tr>
    <th scope="row">Department</th>
    <td>

    <select name="dept">
    <option value="">Select..</option>

    <?php
    $data = mysql_query ("SELECT * FROM dept ORDER BY id DESC") or die (mysql_error());

    echo $data;

    while($row_dept = mysql_fetch_array( $data ))
    {
    ?>
    <option value="<?php echo $row_dept['id'] ;?>" <?php if($row_dept['id']==$dept){echo ' selected="selected"';}?>>
    <?php echo $row_dept['dept_name'] ;?>
    </option>

    <?php
    }
    ?>
    </select>
    </td>
    </tr>

    <tr>
    <th scope="row">Mobile</th>
    <td><input type="text" name="mobile" value="<?php echo $mobile; ?>" /></td>
    </tr>

    </table>
    <br/>

    <a href="indexPage.php">
    <input type="button" name="back" value="Back" /></a>

    <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">

    <input type="submit" name="goSubmit" value="Modify"/>

    </div>
    </form>

    </body>
    </html>

    [/code]


    EDIT: I wonder if it's possible for me to use if/elseif instead of switch statement to achieve this?
    Copy linkTweet thisAlerts:
    @siabanieauthorJan 29.2011 — Can anyone help me out: I have create a form for a new user to register. And I also want to add a modify form on it which is the code back at post #60....I want to do if/else if statement.

    Can anyone help me to combine these two forms into one page? And how it will work?

    [B]create form[/B]
    [code=php]<?php

    require 'includes/application.php';
    //Define
    $name = "";
    $surname = "";
    $address = "";
    $dept= "";
    $mobile = "";

    // isset determine if a varaible is set



    if (isset($_POST['goSubmit']))
    {
    $name = $_POST['name'];
    $surname = $_POST['surname'];
    $address = $_POST['address'];
    $dept = $_POST['dept'];
    $mobile = $_POST['mobile'];

    print_r($_POST);

    $errormsg = "";

    if($name == "")
    $errormsg = $errormsg. "Name<br/ >";

    if ($surname == "")
    $errormsg = $errormsg. "Surname Please <br/ >";

    if ($mobile != "" && !is_numeric ($mobile))
    $errormsg = $errormsg. "Mobile No? <br/ >";

    if ($errormsg != "")
    echo "Please fill the blank info: <br/ > $errormsg";

    else
    {
    $input_field = "";


    foreach ($_POST as $key => $val)
    {
    if ($key != 'goSubmit')
    {
    $_SESSION[$key] = $val;

    }
    }

    header("Location: confirmPage.php");

    }
    }


    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Modify Document</title>
    </head>

    <body>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <div align="center">
    <table width="300" border="1">
    <h1> Modifying A User </h1>

    <tr>
    <th scope="row">Name</th>
    <td><input type="text" name="name" value="<?php echo $name;?>" /></td>
    </tr>

    <tr>
    <th scope="row">Surname</th>
    <td>
    <input type="text" name="surname" value="<?php echo $surname; ?>" /></td>
    </tr>

    <tr>
    <th scope="row">Address</th>
    <td><input type="text" name="address" value="<?php echo $address; ?>" /></td>
    </tr>


    <tr>
    <th scope="row">Department</th>
    <td>

    <select name="dept">
    <option value="">Select..</option>

    <?php
    $data = mysql_query ("SELECT * FROM dept ORDER BY id DESC") or die (mysql_error());

    echo $data;

    while($row_dept = mysql_fetch_array( $data ))
    {
    ?>
    <option value="<?php echo $row_dept['id'] ;?>" <?php if($row_dept['id']==$dept){echo ' selected="selected"';}?>>
    <?php echo $row_dept['dept_name'] ;?>
    </option>

    <?php
    }
    ?>
    </select>
    </td>
    </tr>

    <tr>
    <th scope="row">Mobile</th>
    <td><input type="text" name="mobile" value="<?php echo $mobile; ?>" /></td>
    </tr>

    </table>
    <br/>

    <a href="indexPage.php">
    <input type="button" name="back" value="Back" /></a>

    <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">

    <input type="submit" name="goSubmit" value="Modify"/>


    </div>
    </form>



    </body>
    </html>

    [/code]


    I was thinking something like these condition to do the forms but I am not exactly sure... - But after this page another confirmation page will appear once the button submitted is click.
    [code=php]
    if (isset($_GET['id']) == $id) {
    //do the modify query
    //display the modify form
    } else if (isset ($_GET ['id'] == NULL) {
    //display the form where user can fill up the form
    }
    [/code]

    OR, can I do like this instead:
    [code=php]
    if (isset ($_GET['id'])) {
    $_SESSION ['page'] = 'modify';
    //perform the modify form
    } else if (isset($_GET['id'])) {
    $_SESSION ['page'] = 'create';
    //perform the create form
    }
    [/code]
    Copy linkTweet thisAlerts:
    @siabanieauthorFeb 01.2011 — ----Problem solved--- Thanks guy ---
    ×

    Success!

    Help @siabanie spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...