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
[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>
[B]modify.php
[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=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]
[code=php]
session_start();
$_SESSION['id'] = $_POST['id'];
$_SESSION['name'] = $_POST['name'];
$_SESSION['surname'] = $_POST['surname'];
$_SESSION['dept'] = $_POST['dept'];
[/code]
[code=php]
echo $_SESSION['name']; //or pass to functions etc
[/code]
[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]
[code=php]
function pre($v) {
echo '<pre>';
print_r($v);
echo '</pre>';
}
[/code]
[code=php]
pre($_SESSION);
pre($_POST);
[/code]
[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]
[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]
','' or
[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]
[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]
[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]
[code=php]<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> [/code]
[code=html]<form action="/index.php" method="post">[/code]
[code=php]
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
[/code]
[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]
[code=php]<!--modify.php?= (showing the id)-->
<td><a href = "person.php?id=<?php echo $row['id'];?>">Modify User </a></td>[/code]
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]
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]
[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]
[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]
[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]
[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]
[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]
[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]
[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]
Where do I put this in person page?
[/code]
[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]
[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]
[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]
[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]
[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]
[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]
[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]
[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]
[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]
[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]
[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]
[code=php]
function pre($v){
echo '<pre>';
print_r($v);
echo '</pre>';
}
[/code]
[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]
[code=html]<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">[/code]
[code=php]
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
[/code]
[code=php]
<form action="confirmation.php" method="post">
[/code]
Well the person page form still not extracting all the values from index page. I am not sure where was wrong...?[/QUOTE]
[code=php]
Notice: Undefined variable: dept on line 51
[/code]
[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]
[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]
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.[/QUOTE]
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]
[code=php]if(isset($_GET['id']){
//they want to modify
}else{
//they want to create
}[/code]
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?[/QUOTE]
[code=php]
if (isset ($_GET['id']) {
$_SESSION ['page'] = 'modify';
} else
$_SESSION ['page'] = 'created';
[/code]
[code=php]
foreach ($_POST as $key =>$val) {
$_SESSION[$key] = $val;
}
[/code]
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]
[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]
[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]
[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]
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]
[code=php]
Undefined variable: dept on line 54
[/code]
[code=php]
..
..
<!--Department-->
<option value="<?php echo $row_dept['id'] ;?>" <?php if($row_dept['id']==$dept){echo ' selected="selected"';}?>>
..
..
[/code]
[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]
[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]
[code=php]
Undefined variable: dept on line 57
[/code]
[code=php]
..
..
<option value="<?php echo $row_dept['id'] ;?>" <?php if($row_dept['id']== $dept){echo ' selected="selected"';}?>>
..
..
[/code]
[code=php]
$dept = $row['dept_id'];
[/code]
[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]
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?[/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.[/QUOTE]
[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]
[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]
[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]
[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]
[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]
[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]
[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]
0.1.9 — BETA 5.19