Current location: Hot Scripts Forums » Other Discussions » Database » info into array

info into array

Reply
  #1 (permalink)  
Old 07-07-09, 05:57 AM
zenix zenix is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
info into array

Hi, I am new to programming and have been working on this sample database I've made up to solidify what I have learned. All seems to be going pretty well, but I seem to have stumbled into a wall. Is it possible to go through all the elements of a database (like: first name, last name, phone...) and place them into an array to be used for populating an HTML form? I'm looking to make it so I can click a button at the bottom of the form and view the next record in sequence.

Is this possible? The code I have for the sample db is here. It works well.

Code:
<?php


$conn = mysql_connect('localhost', 'root')or die(mysql_error());

$create = mysql_query("CREATE DATABASE IF NOT EXISTS db1") or die(mysql_error());

mysql_select_db ("db1");
?>
<form name ="form"method ="post" action ="browse.php">

  <p>First Name: <input type ="text" name ="fname">
    Last Name: <input type ="text"name ="lname"><br><br>
    address: <input type="text" name ="address"><br><br>
    address2: <input type="text" name ="address2" ><br><br>
    City: <input type ="text" name ="city"><br><br>
    State: <input type ="text" name ="state"><br><br>
    Zip: <input type ="text" name ="zip"><br><br>
    Phone:<input type ="text" name ="phone"><br><br>
    Notes:<textarea name ="notes"></textarea><br><br>
<input type ="hidden" name ="key">  
  <input type ="submit" name ="submit" value ="Query">
  </p>

</form>
<?php


//create table 
$testdb = 'CREATE TABLE IF NOT EXISTS testable(id int (11) NOT NULL auto_increment,
							 lname varchar(40) NOT NULL,
							 fname varchar(40) NOT NULL,
  							 address varchar(40) NOT NULL,
							 address2 varchar(40) NOT NULL,
							 city varchar(15) NOT NULL,
							 state varchar(2) NOT NULL,
							 zip int(5) NOT NULL,
							 phone varchar(11) NOT NULL,
							 notes longtext NOT NULL,
   							 PRIMARY KEY(id),
							 KEY testype(lname, fname))';

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

$lname = $_POST['lname'];
$fname = $_POST['fname'];
$address = $_POST['address'];
$address2= $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$notes = $_POST['notes'];

//"$address", address,
$insert = "INSERT INTO testable( id, lname, fname,  address, address2, city, state, zip, phone, notes,)
		   VALUES(0, '$lname', '$fname', '$address',  '$address2', '$city', '$state', '$zip', '$phone', '$notes' )";

$results =mysql_query($insert) or die ("Couldn't insert into database because: " .mysql_error());

if(mysql_affected_rows()==1)
{
	echo "Success!";
}
else
{
	echo "Failed" . mysql_error();
}
if($insert)
{
	echo 'Data inserted successfully!';
}
?>
<form name ="form1" method ="post" action ="form1.php">
<input type ="submit" name ="submit" value ="ADD">
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 07-07-09, 07:20 AM
ruteckycs's Avatar
ruteckycs ruteckycs is offline
Coding Addict
 
Join Date: Jul 2009
Posts: 273
Thanks: 3
Thanked 5 Times in 5 Posts
PHP Code:
<?php
// Make a MySQL Connection
mysql_connect("localhost""admin""****") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

// Get all the data from the "example" table
$result mysql_query("SELECT * FROM example"
or die(
mysql_error());  

echo 
"<table border='1'>";
echo 
"<tr> <th>Name</th> <th>Age</th> </tr>";
// keeps getting the next row until there are no more to get
while($row mysql_fetch_array$result )) {
    
// Print out the contents of each row into a table
    
echo "<tr><td>"
    echo 
$row['name'];
    echo 
"</td><td>"
    echo 
$row['age'];
    echo 
"</td></tr>"


echo 
"</table>";
?>

Last edited by Nico; 07-07-09 at 08:11 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 07-07-09, 08:45 AM
zenix zenix is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
Thank you for looking at my code and giving me the suggestion, I appreciate your time and effort very much. The code you wrote will put the data from the database into a table, what I'd like to do is have the data populate an HTML form one record at a time. When I view the data in the form and make edits, I'd like to be able to press a button and have the fields re populate with the data from the next record in the database. Is THIS at all possible?

Thank you very much again!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 07-07-09, 09:27 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 2,632
Thanks: 0
Thanked 15 Times in 15 Posts
You can use a session array to store your records.
This is quite efficient because all records are stored in memory and the database is only queried when you add a record.

This will only allow you to view and add records.
You will have to add an update query to change an existing record.
PHP Code:
<?php
session_start
();
$conn mysql_connect('localhost''root''')or die(mysql_error());
$create mysql_query("CREATE DATABASE IF NOT EXISTS db1") or die(mysql_error());
mysql_select_db ("db1");
$testdb 'CREATE TABLE IF NOT EXISTS testable(id int (11) NOT NULL auto_increment,
                             lname varchar(40) NOT NULL,
                             fname varchar(40) NOT NULL,
                               address varchar(40) NOT NULL,
                             address2 varchar(40) NOT NULL,
                             city varchar(15) NOT NULL,
                             state varchar(2) NOT NULL,
                             zip int(5) NOT NULL,
                             phone varchar(11) NOT NULL,
                             notes longtext NOT NULL,
                                PRIMARY KEY(id),
                             KEY testype(lname, fname))'
;

$result mysql_query($testdb) or die(mysql_error());
$array_position = !empty($_POST["key"]) ? $_POST["key"] : 0;
if(!empty(
$_POST["next"]))
{
 if(isset(
$_SESSION["fname"][$array_position]))
 {
  
$array_position++;
  }
 }
if(!empty(
$_POST["prev"]))
{
 
$array_position--;
 if(
$array_position<0){$array_position 0;}
 }
if(!empty(
$_POST["add"]))
{
 
$_SESSION["lname"][] = $_POST['lname'];
 
$_SESSION["fname"][] = $_POST['fname'];
 
$_SESSION["address"][] = $_POST['address'];
 
$_SESSION["address2"][]= $_POST['address2'];
 
$_SESSION["city"][] = $_POST['city'];
 
$_SESSION["state"][] = $_POST['state'];
 
$_SESSION["zip"][] = $_POST['zip'];
 
$_SESSION["phone"][] = $_POST['phone'];
 
$_SESSION["notes"][] = $_POST['notes'];
 
$insert "INSERT INTO testable(lname,fname,address,address2,city,state,zip,phone,notes) VALUES('".$_POST['lname']."','".$_POST['fname']."','".$_POST['address']."','".$_POST['address2']."','".$_POST['city']."','".$_POST['state']."','".$_POST['zip']."','".$_POST['phone']."','".$_POST['notes']."')";
 
$results mysql_query($insert) or die ("Couldn't insert into database because: " .mysql_error());
 if(
mysql_affected_rows()==1){echo 'Data inserted successfully!';}
 }
?>
<form name="form" method="post" action="#">
 <table>
  <tr><td align="right">First Name: </td><td><input type="text" name="fname" value="<?php echo $_SESSION["fname"][$array_position]; ?>"></td><td align="right">Last Name: </td><td><input type="text" name="lname" value="<?php echo $_SESSION["lname"][$array_position]; ?>"></td></tr>
  <tr><td align="right">address: </td><td><input type="text" name="address" value="<?php echo $_SESSION["address"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">address2: </td><td><input type="text" name="address2" value="<?php echo $_SESSION["address2"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">City: </td><td><input type="text" name="city" value="<?php echo $_SESSION["city"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">State: </td><td><input type="text" name="state" value="<?php echo $_SESSION["state"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">Zip: </td><td><input type="text" name="zip" value="<?php echo $_SESSION["zip"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">Phone: </td><td><input type="text" name="phone" value="<?php echo $_SESSION["phone"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">Notes: </td><td><textarea name="notes"><?php echo $_SESSION["notes"][$array_position]; ?></textarea></td><td colspan=2></td></tr>
  <tr><td colspan=4><input type="submit" name="add" value="Add a record"><input type="submit" name="next" value="View next record"><input type="submit" name="prev" value="View previous record"></td></tr>
 </table>
 <input type="hidden" name="key" value="<?php echo $array_position?>">
</form>
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 07-07-09, 09:38 AM
zenix zenix is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
Thumbs up

Thanks job!! This looks exactly like what I was thinking!! I had the idea, but no clue of how to best implement it...kinda like a writers block i guess. Anyway, I'll be playing around with the code you've given so I can learn the programming better. I noticed something in your coding that I was hoping you could explain, please.

In this bit of coding: $array_position = !empty($_POST["key"]) ? $_POST["key"] : 0;

What does the question mark do? I can use it in a GET method and have the data in the URL, but I haven't yet (until now) seen it in plain code.

Thanks again!! YOU are the coding God!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 07-08-09, 02:12 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 2,632
Thanks: 0
Thanked 15 Times in 15 Posts
Quote:
Originally Posted by zenix View Post
Thanks job!! This looks exactly like what I was thinking!! I had the idea, but no clue of how to best implement it...kinda like a writers block i guess. Anyway, I'll be playing around with the code you've given so I can learn the programming better. I noticed something in your coding that I was hoping you could explain, please.

In this bit of coding: $array_position = !empty($_POST["key"]) ? $_POST["key"] : 0;

What does the question mark do? I can use it in a GET method and have the data in the URL, but I haven't yet (until now) seen it in plain code.

Thanks again!! YOU are the coding God!
PHP Code:
$array_position = !empty($_POST["key"]) ? $_POST["key"] : 0
This is a BOOLEAN logic statement.
It is similar to an if statement.
It reads like this:
PHP Code:
if(!empty($_POST["key"])){$array_position=$_POST["key"];}
else{
$array_position=0;} 
Ok, I am glad you liked my code.
Here is the finished program with an UPDATE feature, Record # display and navigation buttons.
This program also fetches all existing records from the database and loads them into the session array at the very beginning.
PHP Code:
<?php
session_start
();
$conn mysql_connect('localhost''root''')or die(mysql_error());
$create mysql_query("CREATE DATABASE IF NOT EXISTS db1") or die(mysql_error());
mysql_select_db ("db1");
$testdb 'CREATE TABLE IF NOT EXISTS testable(id int (11) NOT NULL auto_increment,
                             lname varchar(40) NOT NULL,
                             fname varchar(40) NOT NULL,
                               address varchar(40) NOT NULL,
                             address2 varchar(40) NOT NULL,
                             city varchar(15) NOT NULL,
                             state varchar(2) NOT NULL,
                             zip int(5) NOT NULL,
                             phone varchar(11) NOT NULL,
                             notes longtext NOT NULL,
                                PRIMARY KEY(id),
                             KEY testype(lname, fname))'
;

$result mysql_query($testdb) or die(mysql_error());
if(empty(
$_SESSION))
{
 
$results mysql_query("select * from testable") or die(mysql_error());
 while(
$r mysql_fetch_assoc($results))
 {
  
$_SESSION["id"][]=$_SESSION["last_id"]=$r["id"];
  
$_SESSION["lname"][]=$r['lname'];
  
$_SESSION["fname"][]=$r['fname'];
  
$_SESSION["address"][]=$r['address'];
  
$_SESSION["address2"][]=$r['address2'];
  
$_SESSION["city"][]=$r['city'];
  
$_SESSION["state"][]=$r['state'];
  
$_SESSION["zip"][]=$r['zip'];
  
$_SESSION["phone"][]=$r['phone'];
  
$_SESSION["notes"][]=$r['notes'];
  }
 }
$array_position = !empty($_POST["key"]) ? $_POST["key"] : 0;
if(!empty(
$_POST["first"]))
{
 
$array_position=0;
 }
if(!empty(
$_POST["next"]))
{
 
$sw 1;
 if(isset(
$_SESSION["fname"][$array_position]))
 {
  
$array_position++;
  if(!empty(
$_SESSION["fname"][$array_position])){$sw 0;}
  }
 }
if(!empty(
$_POST["prev"]))
{
 
$sw 0;
 
$array_position--;
 if(
$array_position<0){$array_position 0;}
 }
if(!empty(
$_POST["last"]))
{
 
$array_position=count($_SESSION["id"])-1;
 }
if(!empty(
$_POST["update"]))
{
 
mysql_query("UPDATE testable SET lname = '".$_POST['lname']."',fname = '".$_POST['fname']."',address = '".$_POST['address']."',address2 = '".$_POST['address2']."',city = '".$_POST['city']."',state = '".$_POST['state']."',zip = '".$_POST['zip']."',phone = '".$_POST['phone']."',notes = '".$_POST['notes']."' WHERE id = '".$_SESSION["id"][$array_position]."'");
 
$_SESSION["lname"][$array_position]=$_POST['lname'];
 
$_SESSION["fname"][$array_position]=$_POST['fname'];
 
$_SESSION["address"][$array_position]=$_POST['address'];
 
$_SESSION["address2"][$array_position]=$_POST['address2'];
 
$_SESSION["city"][$array_position]=$_POST['city'];
 
$_SESSION["state"][$array_position]=$_POST['state'];
 
$_SESSION["zip"][$array_position]=$_POST['zip'];
 
$_SESSION["phone"][$array_position]=$_POST['phone'];
 
$_SESSION["notes"][$array_position]=$_POST['notes'];
 }
if(!empty(
$_POST["add"]))
{
 
$_SESSION["id"][]=$_SESSION["last_id"]++;
 
$_SESSION["lname"][]=$_POST['lname'];
 
$_SESSION["fname"][]=$_POST['fname'];
 
$_SESSION["address"][]=$_POST['address'];
 
$_SESSION["address2"][]=$_POST['address2'];
 
$_SESSION["city"][]=$_POST['city'];
 
$_SESSION["state"][]=$_POST['state'];
 
$_SESSION["zip"][]=$_POST['zip'];
 
$_SESSION["phone"][]=$_POST['phone'];
 
$_SESSION["notes"][]=$_POST['notes'];
 
$insert "INSERT INTO testable(lname,fname,address,address2,city,state,zip,phone,notes) VALUES('".$_POST['lname']."','".$_POST['fname']."','".$_POST['address']."','".$_POST['address2']."','".$_POST['city']."','".$_POST['state']."','".$_POST['zip']."','".$_POST['phone']."','".$_POST['notes']."')";
 
$results mysql_query($insert) or die ("Couldn't insert into database because: " .mysql_error());
 if(
mysql_affected_rows()==1){echo 'Data inserted successfully!';}
 }
?>
<form name="form" method="post" action="#">
 <table>
  <tr><td align="right">First Name: </td><td><input type="text" name="fname" value="<?php echo $_SESSION["fname"][$array_position]; ?>"></td><td align="right">Last Name: </td><td><input type="text" name="lname" value="<?php echo $_SESSION["lname"][$array_position]; ?>"></td></tr>
  <tr><td align="right">address: </td><td><input type="text" name="address" value="<?php echo $_SESSION["address"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">address2: </td><td><input type="text" name="address2" value="<?php echo $_SESSION["address2"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">City: </td><td><input type="text" name="city" value="<?php echo $_SESSION["city"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">State: </td><td><input type="text" name="state" value="<?php echo $_SESSION["state"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">Zip: </td><td><input type="text" name="zip" value="<?php echo $_SESSION["zip"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">Phone: </td><td><input type="text" name="phone" value="<?php echo $_SESSION["phone"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">Notes: </td><td><textarea name="notes"><?php echo stripslashes($_SESSION["notes"][$array_position]); ?></textarea></td><td colspan=2></td></tr>
  <tr><td><?php if($sw == 1){ ?><input type="submit" name="add" value="Add"><?php }else{ ?><input type="submit" name="update" value="Update"><?php ?></td><td colspan=2 align="center" style="color:#00f;">Record <b>-</b> <?php echo $pos $array_position+1; if($sw == 0){echo " of ".count($_SESSION["lname"]);} ?></td><td><input type="submit" name="first" value="First"><input type="submit" name="prev" value="Prev"><input type="submit" name="next" value="Next"><input type="submit" name="last" value="Last"></td></tr>
 </table>
 <input type="hidden" name="key" value="<?php echo $array_position?>">
</form>
__________________
Jerry Broughton

Last edited by job0107; 07-08-09 at 02:19 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 07-08-09, 03:10 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 2,632
Thanks: 0
Thanked 15 Times in 15 Posts
Well, my last program still had some bugs in it, but I think I have worked them out.
Click the Next button past the last record to add a new record. Or click the Last button then the Next button.
PHP Code:
<?php
session_start
();
$sw=1;
$_SESSION["last_id"] = empty($_SESSION["last_id"]) ? $_SESSION["last_id"];
$conn mysql_connect('localhost''root''')or die(mysql_error());
$create mysql_query("CREATE DATABASE IF NOT EXISTS db1") or die(mysql_error());
mysql_select_db ("db1");
$testdb 'CREATE TABLE IF NOT EXISTS testable(id int (11) NOT NULL auto_increment,
                             lname varchar(40) NOT NULL,
                             fname varchar(40) NOT NULL,
                               address varchar(40) NOT NULL,
                             address2 varchar(40) NOT NULL,
                             city varchar(15) NOT NULL,
                             state varchar(2) NOT NULL,
                             zip int(5) NOT NULL,
                             phone varchar(11) NOT NULL,
                             notes longtext NOT NULL,
                                PRIMARY KEY(id),
                             KEY testype(lname, fname))'
;

$result mysql_query($testdb) or die(mysql_error());
if(empty(
$_SESSION))
{
 
$results mysql_query("select * from testable") or die(mysql_error());
 while(
$r mysql_fetch_assoc($results))
 {
  
$_SESSION["id"][]=$_SESSION["last_id"]=$r["id"];
  
$_SESSION["lname"][]=$r['lname'];
  
$_SESSION["fname"][]=$r['fname'];
  
$_SESSION["address"][]=$r['address'];
  
$_SESSION["address2"][]=$r['address2'];
  
$_SESSION["city"][]=$r['city'];
  
$_SESSION["state"][]=$r['state'];
  
$_SESSION["zip"][]=$r['zip'];
  
$_SESSION["phone"][]=$r['phone'];
  
$_SESSION["notes"][]=$r['notes'];
  }
 }
$array_position = !empty($_POST["key"]) ? $_POST["key"] : 0;
if(!empty(
$_POST["first"]))
{
 
$sw 0;
 
$array_position=0;
 }
if(!empty(
$_POST["next"]))
{
 
$sw 1;
 if(isset(
$_SESSION["fname"][$array_position]))
 {
  
$array_position++;
  if(!empty(
$_SESSION["fname"][$array_position])){$sw 0;}
  }
 }
if(!empty(
$_POST["prev"]))
{
 
$sw 0;
 
$array_position--;
 if(
$array_position<0){$array_position 0;}
 }
if(!empty(
$_POST["last"]))
{
 
$sw 0;
 
$array_position=count($_SESSION["id"])-1;
 }
if(!empty(
$_POST["update"]))
{
 
mysql_query("UPDATE testable SET lname = '".$_POST['lname']."',fname = '".$_POST['fname']."',address = '".$_POST['address']."',address2 = '".$_POST['address2']."',city = '".$_POST['city']."',state = '".$_POST['state']."',zip = '".$_POST['zip']."',phone = '".$_POST['phone']."',notes = '".$_POST['notes']."' WHERE id = '".$_SESSION["id"][$array_position]."'");
 
$_SESSION["lname"][$array_position]=$_POST['lname'];
 
$_SESSION["fname"][$array_position]=$_POST['fname'];
 
$_SESSION["address"][$array_position]=$_POST['address'];
 
$_SESSION["address2"][$array_position]=$_POST['address2'];
 
$_SESSION["city"][$array_position]=$_POST['city'];
 
$_SESSION["state"][$array_position]=$_POST['state'];
 
$_SESSION["zip"][$array_position]=$_POST['zip'];
 
$_SESSION["phone"][$array_position]=$_POST['phone'];
 
$_SESSION["notes"][$array_position]=$_POST['notes'];
 
$sw=0;
 }
if(!empty(
$_POST["add"]))
{
 
$_SESSION["id"][]=$_SESSION["last_id"]++;
 
$_SESSION["lname"][]=$_POST['lname'];
 
$_SESSION["fname"][]=$_POST['fname'];
 
$_SESSION["address"][]=$_POST['address'];
 
$_SESSION["address2"][]=$_POST['address2'];
 
$_SESSION["city"][]=$_POST['city'];
 
$_SESSION["state"][]=$_POST['state'];
 
$_SESSION["zip"][]=$_POST['zip'];
 
$_SESSION["phone"][]=$_POST['phone'];
 
$_SESSION["notes"][]=$_POST['notes'];
 
$insert "INSERT INTO testable(lname,fname,address,address2,city,state,zip,phone,notes) VALUES('".$_POST['lname']."','".$_POST['fname']."','".$_POST['address']."','".$_POST['address2']."','".$_POST['city']."','".$_POST['state']."','".$_POST['zip']."','".$_POST['phone']."','".$_POST['notes']."')";
 
$results mysql_query($insert) or die ("Couldn't insert into database because: " .mysql_error());
 if(
mysql_affected_rows()==1){echo 'Data inserted successfully!';$sw=0;}
 }
?>
<form name="form" method="post" action="#">
 <table>
  <tr><td align="right">First Name: </td><td><input type="text" name="fname" value="<?php echo $_SESSION["fname"][$array_position]; ?>"></td><td align="right">Last Name: </td><td><input type="text" name="lname" value="<?php echo $_SESSION["lname"][$array_position]; ?>"></td></tr>
  <tr><td align="right">Address: </td><td><input type="text" name="address" value="<?php echo $_SESSION["address"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">Address2: </td><td><input type="text" name="address2" value="<?php echo $_SESSION["address2"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">City: </td><td><input type="text" name="city" value="<?php echo $_SESSION["city"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">State: </td><td><input type="text" name="state" value="<?php echo $_SESSION["state"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">Zip: </td><td><input type="text" name="zip" value="<?php echo $_SESSION["zip"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">Phone: </td><td><input type="text" name="phone" value="<?php echo $_SESSION["phone"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">Notes: </td><td><textarea name="notes"><?php echo stripslashes($_SESSION["notes"][$array_position]); ?></textarea></td><td colspan=2></td></tr>
  <tr><td><?php if($sw == 1){ ?><input type="submit" name="add" value="Add"><?php }else{ ?><input type="submit" name="update" value="Update"><?php ?></td><td colspan=2 align="center" style="color:#00f;">Record <b>-</b> <?php echo $pos $array_position+1; if($sw == 0){echo " of ".count($_SESSION["lname"]);} ?></td><td><input type="submit" name="first" value="First"><input type="submit" name="prev" value="Prev"><input type="submit" name="next" value="Next"><input type="submit" name="last" value="Last"></td></tr>
 </table>
 <input type="hidden" name="key" value="<?php echo $array_position?>">
</form>
__________________
Jerry Broughton

Last edited by job0107; 07-08-09 at 03:22 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 07-08-09, 07:06 AM
zenix zenix is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
Jerry,

Wow, you have REALLY put a lot of time and effort into this, I thank you very much! I wish I could offer more than a thanks as you have taught me a great deal.

I started playing around with the last code block you gave to me, after having printed it out and studied it taking notes to help my learning, and I can't seem to get it to work. I appears to be connected to my database and all, but none of the fields in the form are populating with data. I click on next and nothing happens, if I click the back button I get an undefined index error.

However, when I add data myself and click add it all seems to work very well, I get "Data inserted successfully" and I can navigate through the records that I've added here. Any ideas what I might be doing wrong? If not, that's alright...like I said, I am still learning this. This seems to be a very good base to grow my programming experience off.

Thank you a lot again for ALL your help!

Garry White
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 07-08-09, 07:33 AM
zenix zenix is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
Trophee

Something a little different; I have a page set up to display my database in a table. After I add a record to the form you wrote, as I've said before, I am able to navigate the database using the buttons...but only the records I have added to this form. When I open up my database table view page ALL the data is there from before as well as the new data placed there using the form you wrote.

Pretty screwed up?? I guess even (what was supposed to be) a simple database project can be pretty complex to debug.

Thank you again!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 07-08-09, 09:13 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 2,632
Thanks: 0
Thanked 15 Times in 15 Posts
Your right, there was still one small bug in the program.
It was in the section that populates the session array from the database.
I fixed that and I added some comments to help you understand the code better.
PHP Code:
<?php
session_start
(); // Start or restart the session.
$sw=1;  // This variable used to toggle between the ADD and UPDATE buttons. Initally set to display the Add button.
$_SESSION["last_id"] = empty($_SESSION["last_id"]) ? $_SESSION["last_id"]; // This session variable used to store the current record id. It is set to 1 if there are no records in the database.

// Connect to the mysql server and create a database and a table if they don't already exist. 
$conn mysql_connect('localhost''root''')or die(mysql_error());
$create mysql_query("CREATE DATABASE IF NOT EXISTS db1") or die(mysql_error());
mysql_select_db ("db1");
$testdb 'CREATE TABLE IF NOT EXISTS testable(id int (11) NOT NULL auto_increment,
                             lname varchar(40) NOT NULL,
                             fname varchar(40) NOT NULL,
                               address varchar(40) NOT NULL,
                             address2 varchar(40) NOT NULL,
                             city varchar(15) NOT NULL,
                             state varchar(2) NOT NULL,
                             zip int(5) NOT NULL,
                             phone varchar(11) NOT NULL,
                             notes longtext NOT NULL,
                                PRIMARY KEY(id),
                             KEY testype(lname, fname))'
;

$result mysql_query($testdb) or die(mysql_error());

// Check to see if the session array has been populated.
// If it hasn't, then fetch any records from the database and populate it.
if(empty($_SESSION["id"][0]))
{
 
$results mysql_query("select * from testable") or die(mysql_error());
 while(
$r mysql_fetch_assoc($results))
 {
  
$_SESSION["id"][]=$_SESSION["last_id"]=$r["id"]; // $_SESSION["last_id"] will contain the last record id. If there are no records in the database, then it was previously set to 1.
  
$_SESSION["lname"][]=$r['lname'];
  
$_SESSION["fname"][]=$r['fname'];
  
$_SESSION["address"][]=$r['address'];
  
$_SESSION["address2"][]=$r['address2'];
  
$_SESSION["city"][]=$r['city'];
  
$_SESSION["state"][]=$r['state'];
  
$_SESSION["zip"][]=$r['zip'];
  
$_SESSION["phone"][]=$r['phone'];
  
$_SESSION["notes"][]=$r['notes'];
  
$sw=0// Display the Update button.
  
}
 }
 
// Set the session array pointer to 0 to view the first record. Or set to current record being viewed.
$array_position = !empty($_POST["key"]) ? $_POST["key"] : 0;

// Check to see if the First button was pressed and set the session array pointer to the first record.
if(!empty($_POST["first"]))
{
 
$sw 0// This variable set to 0 so the Upadte button appears.
 
$array_position=0;
 }
 
// Check to see if the Next button was pressed.
if(!empty($_POST["next"]))
{
 
// Check to see if you are currently viewing a record.
 // If yes, then increment the session array pointer to the next record.
 // But not past the first empty array element.
 
if(isset($_SESSION["fname"][$array_position]))
 {
  
$array_position++;
  
// If the next record is not empty then display the Update button.
  
if(!empty($_SESSION["fname"][$array_position])){$sw 0;}
  }
 }
 
// Check to see if the Prev button was pressed. If yes, then decrement the session array pointer to the previous record.
if(!empty($_POST["prev"]))
{
 
$sw 0// Display the Update button.
 
$array_position--;
 if(
$array_position<0){$array_position 0;} // Don't let the session array pointer get less than 0.
 
}

// Check to see if the Last button was pressed.
// If yes, then set the session array pointer to the last record.
if(!empty($_POST["last"]))
{
 
$sw 0// Display the Update button.
 
$array_position=count($_SESSION["id"])-1;
 }
 
// Check to see if the Update button was pressed.
// If yes, then update the record in the database and the session array.
if(!empty($_POST["update"]))
{
 
mysql_query("UPDATE testable SET lname = '".$_POST['lname']."',fname = '".$_POST['fname']."',address = '".$_POST['address']."',address2 = '".$_POST['address2']."',city = '".$_POST['city']."',state = '".$_POST['state']."',zip = '".$_POST['zip']."',phone = '".$_POST['phone']."',notes = '".$_POST['notes']."' WHERE id = '".$_SESSION["id"][$array_position]."'");
 
$_SESSION["lname"][$array_position]=$_POST['lname'];
 
$_SESSION["fname"][$array_position]=$_POST['fname'];
 
$_SESSION["address"][$array_position]=$_POST['address'];
 
$_SESSION["address2"][$array_position]=$_POST['address2'];
 
$_SESSION["city"][$array_position]=$_POST['city'];
 
$_SESSION["state"][$array_position]=$_POST['state'];
 
$_SESSION["zip"][$array_position]=$_POST['zip'];
 
$_SESSION["phone"][$array_position]=$_POST['phone'];
 
$_SESSION["notes"][$array_position]=$_POST['notes'];
 
$sw=0// Display the Update button.
 
}

// Check to see if the Add button was pressed.
// If yes, then add the record to the session array and insert the record into the database.
// By default $sw is previously set to 1 to display the Add button.
if(!empty($_POST["add"]))
{
 
$_SESSION["id"][]=$_SESSION["last_id"]++;
 
$_SESSION["lname"][]=$_POST['lname'];
 
$_SESSION["fname"][]=$_POST['fname'];
 
$_SESSION["address"][]=$_POST['address'];
 
$_SESSION["address2"][]=$_POST['address2'];
 
$_SESSION["city"][]=$_POST['city'];
 
$_SESSION["state"][]=$_POST['state'];
 
$_SESSION["zip"][]=$_POST['zip'];
 
$_SESSION["phone"][]=$_POST['phone'];
 
$_SESSION["notes"][]=$_POST['notes'];
 
$insert "INSERT INTO testable(lname,fname,address,address2,city,state,zip,phone,notes) VALUES('".$_POST['lname']."','".$_POST['fname']."','".$_POST['address']."','".$_POST['address2']."','".$_POST['city']."','".$_POST['state']."','".$_POST['zip']."','".$_POST['phone']."','".$_POST['notes']."')";
 
$results mysql_query($insert) or die ("Couldn't insert into database because: " .mysql_error());
 if(
mysql_affected_rows()==1)
 {
  echo 
'Data inserted successfully!';
  
$sw=0// A new record has been created so display the Update button.
  
}
 }
?>
<form name="form" method="post" action="#">
 <table>
  <tr><td align="right">First Name: </td><td><input type="text" name="fname" value="<?php echo $_SESSION["fname"][$array_position]; ?>"></td><td align="right">Last Name: </td><td><input type="text" name="lname" value="<?php echo $_SESSION["lname"][$array_position]; ?>"></td></tr>
  <tr><td align="right">Address: </td><td><input type="text" name="address" value="<?php echo $_SESSION["address"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">Address2: </td><td><input type="text" name="address2" value="<?php echo $_SESSION["address2"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">City: </td><td><input type="text" name="city" value="<?php echo $_SESSION["city"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">State: </td><td><input type="text" name="state" value="<?php echo $_SESSION["state"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">Zip: </td><td><input type="text" name="zip" value="<?php echo $_SESSION["zip"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">Phone: </td><td><input type="text" name="phone" value="<?php echo $_SESSION["phone"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">Notes: </td><td><textarea name="notes"><?php echo stripslashes($_SESSION["notes"][$array_position]); ?></textarea></td><td colspan=2></td></tr>
  <tr>
   <td>
    <?php
    
// If $sw is set to 1 then the Add button is displayed.
    
if($sw == 1)
    {
    
?>
     <input type="submit" name="add" value="Add">
    <?php
     
}
    
// If it is set to 0 then the Update button is displayed.
    
else
    {
    
?>
     <input type="submit" name="update" value="Update">
    <?php
     
}
    
?>
   </td>
   <td colspan=2 align="center" style="color:#00f;">
    Record <b>-</b>
    <?php
     
// Display the current or new record number.
     
echo $pos $array_position+1;
     if(
$sw == 0)
     {
      
// Display the total number of records if any exist.
      // Unless you are adding a record ($sw will equal 1). Then only display the new record number.
      
echo " of ".count($_SESSION["lname"]);
      }
    
?>
    </td>
   <td><input type="submit" name="first" value="First"><input type="submit" name="prev" value="Prev"><input type="submit" name="next" value="Next"><input type="submit" name="last" value="Last"></td>
  </tr>
 </table>
 <input type="hidden" name="key" value="<?php echo $array_position?>">
</form>
__________________
Jerry Broughton

Last edited by job0107; 07-08-09 at 09:32 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove value from array Nikas PHP 1 05-20-09 04:20 AM
Multi-Dimensional Array Help Nikas PHP 17 05-04-09 03:10 AM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 04:54 PM
Serializing a set of arrays dannyallen PHP 2 10-11-04 04:04 AM
linking to iframe not working :( j0d JavaScript 5 01-19-04 09:14 PM


All times are GMT -5. The time now is 04:02 PM.
vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.