Jump to content

Seeking simple model for PHP Self form, to edit update mysql


Karebac

Recommended Posts

I am a beginner.  I need a good model script of how to find a record in a table, display, edit, delete, or add a new record.

 

I keep searching in google, but so far no luck.

 

I have several textbooks.  If I find something, I will post it here.

 

Thanks for any help, advice.

 

Link to comment
Share on other sites

The easiet to understand book that I have found is by Mike McGrath "PHP in Easy Steps" printed by Barnes & Noble.

 

I have a feeling that what I want as a model script involves PHP SELF.

 

I was all set, just now, to key the example into my text editor. But I decided to google and see if it is already available somewhere on the Internet.

 

Sure enough, it is!

 

http://stilen.com/notes/php4101.txt

 

<?php header("Cache-Control:no-cache"); 
  function setnum(){
    global $num;
    srand( (double)microtime() * 1000000);
    $num = rand(1,20);
  }
?>  
<html><head><title>Number guess</title></head><body>
<?php
  if($num==null)	# starting instructions
  {
    $msg="I have thought of a number between 1 and 20";
    $msg.="<h3>guess what it is ...</h3>";
  }
  #error message for invalid entries
  if ( $num != null and !is_numeric($guess)){
    $msg="your guess was invalid<h3>Try Again!</h3>";}
  else if ($guess == $num) #is guess correct
  {
    if($num != null)
    {  
      $msg="CORRECT! - THE NUMBER WAS $num";
      $msg.="<h3><a href=\"$PHP_SELF\">";
      $msg.="CLICK HERE TO TRY AGAIN???</a></h3>";
    }
    setnum();  #set a number number to guess
  }
  else if ($guess > $num) # is guess to high?
  {
    $msg="You guessed $guess<h3>My number is lower!</h3>";
  } else if($guess < $num) # is guess to low?
  {$msg="You guessed $guess<h3>My number is higher!</h3>";
  } 
  echo ($msg);	#write out the message
?>
<form action="<?php $PHP_SELF ?>" method="post">
<input type="hidden" name="num" value="<?php echo($num); ?>" > Guess:<input type="text" name="Submit">
</form></body></html>

Link to comment
Share on other sites

Oh great!  The sample code does not work.  When I run it, it keeps giving me the message

 

YOUR GUESS WAS INVALID TRY AGAIN,

 

which means that $guess is not numeric. 

 

And I thought that I would make some easy progress with this example code.

 

Link to comment
Share on other sites

What I really want to do, for starters, is to get this simple PHP SELF guessing game example to work.

 

If someone handed me a useable model for mysql table record addition, edit, update, delete... something simple that I could build on, then I would be in great shape.

 

Like, suppose I have a very simple table called address

with fields for

 

First name

lastname

street

zip

 

What is the best model script/form to find a record, add a record, delete a record, modify a record, in that table. 

 

And if you give me that simple example, then I can learn and expand it to my price table, and use it for all my tables.

 

 

Link to comment
Share on other sites

Thanks redarrow.  That would help.  Actually, I dont even need the tutorial. I just need an example of a script that updates, addeds, edits, deletes a simple table.

 

Prentend I have a table called phone with two fields:  Name and number.

 

I need a form/script that will find a name, allow update, delete, or add a new name and number.

 

I want to use it as a model for everything I am doing.

 

If the script works, I know enough to understand it and modify it, and use it as a model.

 

 

Link to comment
Share on other sites

Regarding the above link, so far so good.

 

The SQL script created the table for the exercise:

 

create table phone_book( 
id integer not null auto_increment, 
first_name varchar(50), 
last_name varchar(50), 
phone varchar(50),
primary key (id) 
) 

 

I am posting my progress with the thought in mind that perhaps someone else who is just starting out may profit from my struggle.

 

 

Link to comment
Share on other sites

There you go a quick example one page setup.

 

<?php

//session_start(); 
// if your using a session for the users and the id is set to the session.

// database connection

$db=mysql_connect("localhost","username","password");
mysql_select_db("database_name",$db);



// if link is pressed to delete entry.

if($_GET['cmd']=="delete"){

//get the name to delete the entry.

$name=$_GET['first_name'];

$query2="delete * from what_ever where fist_name='$name' ";

$result2=mysql_query($query2)or die("mysql_error");

if(mysql_affected_rows($resul2)){

	echo "Entry Deleted From Database!";
}
}



// if link is pressed to edit entry.

if($_GET['cmd']=="edit"){

$name=$_POST['name'];	

$query4="select * from wehat_ever where first_name='$name'";
$result4=mysql_fetch_assoc($query4)or die("mysql_error()");

//while loop for query to get all database information.

while($x=mysql_fetch_assoc($result4)){

// form for editing current information.

echo"<form method='POST' action=' '".$_SERVER['PHP_SELF']."'?cmd=delete'>
<br>
Please edit first name:
<br>	
    <input type='text' name='first_name' value=''".$x['first_name']."'>
    
<br>
Please edit last name:
<br>	
    <input type='text' name='first_name' value=''".$x['last_name']."'>
    
    
<br>
Please edit street:
<br>	
    <input type='text' name='first_name' value=''".$x['street']."'>
    
<br>
Please edit zip:
<br>	
    <input type='text' name='first_name' value=''".$x['zip']."'>
    
    <br><br>
    
    <input type='submit' name='submit' value='new edit'>
    
    ";

}
// if form posted.

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

//get the name to delete the entry.

$name=$_GET['first_name'];
$name=addslashes($_POST['name']);
$last_name=addslashes($_POST['last_name']);
$strret=addslashes($_POST['street']);
$zip=addslashes($_POST['zip']);

$query3="update set first_name,last_name,strret,zip from what_ever where fist_name='$name' ";

$result3=mysql_query($query3)or die("mysql_error");

if(mysql_affected_rows($resul3)){

	echo "Entry Deleted From Database!";
}
}
}

//database query select the table name and use a where clause to select the database entry via the current user logged in
//but if you have no current user for the where cluse you can use a id you no within the database field or not
// choose any that wik resultint in showing all data.

$query1="select * from wehat_ever where id='$what_ever'";
$result1=mysql_fetch_assoc($query1)or die("mysql_error()");

//while loop for query to get all database information.

while($x=mysql_fetch_assoc($result1)){

//display table with the while loop set to $x

echo"<table align='center'><td>First name:<br>".$x['first_name']."<br></td>
            <td>Lastname:<br>".$x['last_name']."<br></td>
            <br><td><br>Street:<br>".$x['street']."<br></td>
            <td><br>Zip:<br>".$x['zip']."</td></table>";

//links for delete and edit set in the wile loop to get correct info to edit or delete.

echo "<br><center>

<a href='".$_SERVER['PHP_SELF']."?cmd=delete&first_name='".$x['first_name']."'>Delete Entry</a>

<p></p>

<a href='".$_SERVER['PHP_SELF']."?cmd=edit&first_name='".$x['first_name']."'>Edit Entry</a>

</center>

<p></p>";
}

?>

Link to comment
Share on other sites

Thanks for the tip Barand, but all I REALLY want, at this point in time, is some simple but useable working model, that I can modify and adapt, for learning purposes.

 

And the code that I stumbled upon seems to work just fine.

 

 

It is a treat to find something that installs and works the first time, and is easy to understand, and does not include obscure things from PEAR.

 


<?PHP
/*******************************************************************************
This script simulates a spreadsheet using any MySQL table with 4 columns (fields).   
The first column must be an auto_increment integer named "id" but the rest you 
can rename using the config variables below. Created by Neil Moomey 
neilmoomey@gci.net.  Feel free to use it as you wish.  I only ask you give me credit.

Here is an example of how to set up a table and field names.  
SQL query to create the table phone_book:

create table phone_book( 
id integer not null auto_increment, 
first_name varchar(50), 
last_name varchar(50), 
phone varchar(50),
primary key (id) 
) 

Now change these variables to fit the table you just created.  For example:
$table = "phone_book";
$field1 = "first_name";
$field1_label = "First Name";

$field2 = "last_name";
$field2_label = "Last Name";

$field3 = "phone";
$field3_label = "Phone";
*******************************************************************************/

// Change these variables to fit your needs:
$table = "phone_book";
$field1 = "first_name";
$field1_label = "first_name";

$field2 = "last_name";
$field2_label = "last_name";

$field3 = "phone";
$field3_label = "phone";

$db_host="mysql";
$db_user="YoursTruly";
$db_pass="YeahRight";
$db="MyDatabase";
// End of variables definitions.  No need to edit code beyond this line

// Connect to database
mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db);
?>
<html>
<head>
<script language="JavaScript">
function focusform()
{
    document.forms[1].field1_value.focus();
}
</script>
</head>
<body OnLoad="focusform()">

<form action="<?echo$PHP_SELF;?>" method="post">
Keyword <input type="text" name="keyword">
<input type="submit" value="Search">
</form>

<?PHP

  if ($insert) { 
    mysql_query( "insert into $table ($field1, $field2, $field3) values (\"$field1_value\",\"$field2_value\",\"$field3_value\")"); 
  } 
  if ($update) { 
    mysql_query( "update $table set $field1=\"$field1_value\", $field2=\"$field2_value\",$field3=\"$field3_value\" where id=$update"); 
  } 
  if ($delete) { 
    mysql_query( "delete from $table where id=$delete"); 
  }  
  
  if (mysql_errno()!=0) { 
    switch (mysql_errno()) { 
      default: 
        echo  "Error #".mysql_errno(). " (".mysql_error(). ")<br>"; 
    } 
  } 
  if (!$sort) $sort="$field1";
  $query =  "select * from $table"; 
  switch ($sort) { 
    case  "id": $query=$query. " order by id"; break; 
    case  "$field1": $query=$query. " order by $field1"; break; 
case  "$field2": $query=$query. " order by $field2"; break; 
    case  "$field3": $query=$query. " order by $field3"; break; 
  } 
  
  if ($read) { 
    $query = "select * from $table where id=$read"; 
  }
  
  if ($keyword) { 
    $query = "select * from $table where $field1 LIKE '%$keyword%' OR $field2 LIKE '%$keyword%' OR $field3 LIKE '%$keyword%'"; 
  }   
  $result = mysql_query($query); 
  $rows = mysql_num_rows($result); 
  echo  "<table border=1 cellspacing=0>\n"; 
  echo  "<tr>\n";
  echo  "<td><a href=\"$PHP_SELF?sort=id\">ID</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field1\">$field1_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field2\">$field2_label</a></td>\n";
  echo  "<td><a href=\"$PHP_SELF?sort=$field3\">$field3_label</a></td>\n"; 
  echo  "<td colspan=2>";
  if ($keyword) echo "<a href=\"$PHP_SELF?sort=$field1\">View All</a>";
  echo  "</td>\n";
  echo  "</tr>\n"; 
  if (!$read) { 
  echo  "<form action=\"$PHP_SELF\" method=\"post\">\n"; 
  echo  "<tr>\n";
  echo  "<td>New</td>\n"; 
  echo  "<input type=hidden name=insert value=1></td>\n"; 
  echo  "<td><input type=text size=10 name=field1_value></td>\n"; 
  echo  "<td><input type=text size=10 name=field2_value></td>\n";
  echo  "<td><input type=text size=15 name=field3_value></td>\n"; 
  echo  "<td colspan=2 align=center>";
  echo  "<input type=submit value=\"   Add    \"></td>\n";
  echo  "<td></td>\n"; 
  echo  "</tr>\n";
  echo  "</form>\n";
  }

  if (!$read) { 
  while ($row = mysql_fetch_row($result)) { 
  echo  "<tr>\n"; 
      echo  "<td>$row[0]</a></td>\n"; 
  echo  "<td>$row[1]</td>\n"; 
      echo  "<td>$row[2]</td>\n"; 
      echo  "<td>$row[3]</td>\n"; 
      echo  "<td><a href=\"$PHP_SELF?read=$row[0]\">Edit</a></td>\n";
  echo  "<td><a href=\"$PHP_SELF?delete=$row[0]\">Delete</a></td>\n";
  echo  "</tr>\n";
      }
  }

  if ($read) {
  $row = mysql_fetch_row($result);
      echo  "<form action=\"$PHP_SELF\" method=\"post\">\n"; 
  echo  "<input type=hidden name=update value=\"$row[0]\">\n"; 
  echo  "<tr>\n";
      echo  "<td>$row[0]</a></td>\n"; 
      echo  "<td><input type=text size=10 name=field1_value  value=$row[1]></td>\n"; 
  echo  "<td><input type=text size=10 name=field2_value  value=$row[2]></td>\n";
      echo  "<td><input type=text size=15 name=field3_value value=$row[3]></td>\n";
      echo  "<td colspan=2 align=center><input type=submit value=\"  Do it!  \"></td>\n"; 
  echo  "</tr>\n";
      echo  "</form>\n"; 	  
    }
  mysql_close(); 
?>
</table>
</body>
</html>


 

In my opinion (but then what do I know), this is a wonderful model of how an example script for a beginner's tutorial should be constructed.

 

It is totally self-contained and self explanatory.

 

It even tells you EXACTLY what to change, and then tells you that everything after a certain point needs no change at all.

 

 

Link to comment
Share on other sites

Redarrow, thanks for that code. After I finish experimenting with this code that I found, I will study and experiment with yours, to see if I can adapt it to my needs.

 

The code I found for the phonebook is adaptable to my price list.

 

I am adding my fields, one by one, and working out the bugs and typos.

 

Here is what I have so far.

 


<?PHP

$table = "prices";
$field1 = "seq";
$field1_label = "seq";

$field2 = "part";
$field2_label = "part";

$field3 = "grit";
$field3_label = "grit";

$field4 = "qpb";
$field4_label = "qpb";

$field5 = "bpc";
$field5_label = "bpc";

// Note: bpc = boxes per case, qpb = qty per box, part=part number, seq is to control the sort sequence.

$db_host="mysql";
$db_user="YoursTruly";
$db_pass="YeahRight!";
$db="mydatabase";
// End of variables definitions.  No need to edit code beyond this line

// Connect to database
mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db);
?>
<html>
<head>
<script language="JavaScript">
function focusform()
{
    document.forms[1].field1_value.focus();
}
</script>
</head>
<body OnLoad="focusform()">

<form action="<?echo$PHP_SELF;?>" method="post">
Keyword <input type="text" name="keyword">
<input type="submit" value="Search">
</form>

<?PHP

  if ($insert) { 
    mysql_query( "insert into $table ($field1, $field2, $field3, $field4, $field5) values (\"$field1_value\",\"$field2_value\",\"$field3_value\",\"$field4_value\",\"$field5_value\")"); 
  } 
  if ($update) { 
    mysql_query( "update $table set $field1=\"$field1_value\", $field2=\"$field2_value\",$field3=\"$field3_value\",$field4=\"$field4_value\",$field5=\"$field5_value\" where pk=$update"); 
  } 
  if ($delete) { 
    mysql_query( "delete from $table where pk=$delete"); 
  }  
  
  if (mysql_errno()!=0) { 
    switch (mysql_errno()) { 
      default: 
        echo  "Error #".mysql_errno(). " (".mysql_error(). ")<br>"; 
    } 
  } 
  if (!$sort) $sort="$field1";
  $query =  "select * from $table"; 
  switch ($sort) { 
    case  "pk": $query=$query. " order by pk"; break; 
    case  "$field1": $query=$query. " order by $field1"; break; 
    case  "$field2": $query=$query. " order by $field2"; break; 
    case  "$field3": $query=$query. " order by $field3"; break; 
    case  "$field4": $query=$query. " order by $field4"; break; 
    case  "$field5": $query=$query. " order by $field5"; break; 
  } 
  
  if ($read) { 
    $query = "select * from $table where pk=$read"; 
  }
  
  if ($keyword) { 
    $query = "select * from $table where $field1 LIKE '%$keyword%' OR $field2 LIKE '%$keyword%' OR $field3 LIKE '%$keyword%'"; 
  }   
  $result = mysql_query($query); 
  $rows = mysql_num_rows($result); 
  echo  "<table border=1 cellspacing=0>\n"; 
  echo  "<tr>\n";
  echo  "<td><a href=\"$PHP_SELF?sort=pk\">ID</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field1\">$field1_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field2\">$field2_label</a></td>\n";
  echo  "<td><a href=\"$PHP_SELF?sort=$field3\">$field3_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field4\">$field4_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field4\">$field5_label</a></td>\n"; 
  echo  "<td colspan=2>";
  if ($keyword) echo "<a href=\"$PHP_SELF?sort=$field1\">View All</a>";
  echo  "</td>\n";
  echo  "</tr>\n"; 
  if (!$read) { 
  echo  "<form action=\"$PHP_SELF\" method=\"post\">\n"; 
  echo  "<tr>\n";
  echo  "<td>New</td>\n"; 
  echo  "<input type=hidden name=insert value=1></td>\n"; 
  echo  "<td><input type=text size=10 name=field1_value></td>\n"; 
  echo  "<td><input type=text size=10 name=field2_value></td>\n";
  echo  "<td><input type=text size=15 name=field3_value></td>\n"; 
  echo  "<td><input type=text size=15 name=field4_value></td>\n"; 
  echo  "<td><input type=text size=15 name=field5_value></td>\n"; 
  echo  "<td colspan=2 align=center>";
  echo  "<input type=submit value=\"   Add    \"></td>\n";
  echo  "<td></td>\n"; 
  echo  "</tr>\n";
  echo  "</form>\n";
  }

  if (!$read) { 
  while ($row = mysql_fetch_row($result)) { 
  echo  "<tr>\n"; 
      echo  "<td>$row[0]</a></td>\n"; 
  echo  "<td>$row[1]</td>\n"; 
      echo  "<td>$row[2]</td>\n"; 
      echo  "<td>$row[3]</td>\n"; 
      echo  "<td>$row[4]</td>\n"; 
      echo  "<td>$row[5]</td>\n"; 
      echo  "<td><a href=\"$PHP_SELF?read=$row[0]\">Edit</a></td>\n";
  echo  "<td><a href=\"$PHP_SELF?delete=$row[0]\">Delete</a></td>\n";
  echo  "</tr>\n";
      }
  }

  if ($read) {
  $row = mysql_fetch_row($result);
      echo  "<form action=\"$PHP_SELF\" method=\"post\">\n"; 
  echo  "<input type=hidden name=update value=\"$row[0]\">\n"; 
  echo  "<tr>\n";
      echo  "<td>$row[0]</a></td>\n"; 
      echo  "<td><input type=text size=10 name=field1_value  value=$row[1]></td>\n"; 
  echo  "<td><input type=text size=10 name=field2_value  value=$row[2]></td>\n";
      echo  "<td><input type=text size=15 name=field3_value value=$row[3]></td>\n";
      echo  "<td><input type=text size=15 name=field4_value value=$row[4]></td>\n";
      echo  "<td><input type=text size=15 name=field5_value value=$row[5]></td>\n";

      echo  "<td colspan=2 align=center><input type=submit value=\"  Do it!  \"></td>\n"; 
  echo  "</tr>\n";
      echo  "</form>\n"; 	  
    }
  mysql_close(); 
?>
</table>
</body>
</html>

Link to comment
Share on other sites

There is ONE THING I cannot figure out in this program that I have been modifying!

 

I have ADDED a button called SPECIAL, because I would like to be able to insert my OWN custom update routine into this working model.

 

The original program has a button called EDIT.  When you click on EDIT, the browser clears and displays ONLY the record you are editing.

 

I attempted to copy every aspect of the EDIT button and routine, and call it SPECIAL.

 

My routine seems to work fine, BUT, the browser DOES NOT CLEAR.  Instead, my record for edit appears at the top of the screen, but below it is the list of all the other records.  HOW can I make my SPECIAL button CLEAR the browser, so that only my record appears.

 

There must be just ONE or TWO commands that I am not overlooking, but I am so new to PHP that I cannot see the problem.  I would appreciate any advice.

 

IS THERE any clear screen command in PHP or HTML?

 

I wish there was a way for me to single step through this in a debugger mode.

 

I cant even figure out how to make PHP do a pause, to print a string, and wait for input.  If I knew how to do THAT then I could modify this code and force it to pause between each line of execution, and show me that line.

 

Notice that I found in google a tip for adding a second button to a form, to allow return to the main MENU.PHP.

 


<?PHP
$table = "prices";
$field1 = "seq";
$field1_label = "seq";

$field2 = "part";
$field2_label = "part";

$field3 = "grit";
$field3_label = "grit";

$field4 = "qpb";
$field4_label = "qpb";

$field5 = "bpc";
$field5_label = "bpc";

$field6 = "ppb";
$field6_label = "ppb";

$field7 = "ppc";
$field7_label = "ppc";

$field8 = "mslp";
$field8_label = "mslp";


$db_host="mysql";
$db_user="YoursTruly";
$db_pass="YeahRight!";
$db="mydatabase";
// End of variables definitions.  No need to edit code beyond this line

// Connect to database
mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db);
?>
<html>
<head>
<script language="JavaScript">
function focusform()
{
    document.forms[1].field1_value.focus();
}
</script>
</head>
<body OnLoad="focusform()">

<form action="<?echo$PHP_SELF;?>" method="post">
Keyword <input type="text" name="keyword">
<input type="submit" value="Search">
<input type="submit" name="gotomenu" value="Main Menu">


</form>

<?PHP

if (isset($_POST['gotomenu'])) header( "Location: menu.php" );
  if ($insert) { 
    mysql_query( "insert into $table ($field1, $field2, $field3, $field4, $field5, $field6, $field7, $field8) values (\"$field1_value\",\"$field2_value\",\"$field3_value\",\"$field4_value\",\"$field5_value\",\"$field6_value\",\"$field7_value\",\"$field8_value\")"); 
  } 
  if ($update) { 
    mysql_query( "update $table set $field1=\"$field1_value\", $field2=\"$field2_value\",$field3=\"$field3_value\",$field4=\"$field4_value\",$field5=\"$field5_value\",$field6=\"$field6_value\",$field7=\"$field7_value\",$field8=\"$field8_value\"  where pk=$update"); 
  } 
  if ($delete) { 
    mysql_query( "delete from $table where pk=$delete"); 
  }  

  if ($special) {
          $query = "select * from $table where pk=$special"; 
          $result = mysql_query($query); 
  $row = mysql_fetch_row($result);
echo  "<form action=\"$PHP_SELF\" method=\"post\">\n"; 

  echo  "<tr>\n";
  echo  "<td>Special</td>\n"; 
  echo  "<table border=1 cellspacing=0>\n"; 
  echo  "<tr>\n";
  echo  "<td><a href=\"$PHP_SELF?sort=pk\">ID</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field1\">$field1_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field2\">$field2_label</a></td>\n";
  echo  "<td><a href=\"$PHP_SELF?sort=$field3\">$field3_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field4\">$field4_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field5\">$field5_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field6\">$field6_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field7\">$field7_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field8\">$field8_label</a></td>\n"; 
  echo  "<td colspan=2>";
  if ($keyword) echo "<a href=\"$PHP_SELF?sort=$field1\">View All</a>";
  echo  "</td>\n";
  echo  "</tr>\n"; 

      echo  "<form action=\"$PHP_SELF\" method=\"post\">\n"; 
  echo  "<input type=hidden name=update value=\"$row[0]\">\n"; 
  echo  "<tr>\n";
      echo  "<td>$row[0]</a></td>\n"; 
      echo  "<td><input type=text size=10 name=field1_value  value=$row[1]></td>\n"; 
  echo  "<td><input type=text size=10 name=field2_value  value=$row[2]></td>\n";
      echo  "<td><input type=text size=5 name=field3_value value=$row[3]></td>\n";
      echo  "<td><input type=text size=5 name=field4_value value=$row[4]></td>\n";
      echo  "<td><input type=text size=5 name=field5_value value=$row[5]></td>\n";
      echo  "<td><input type=text size=5 name=field6_value value=$row[6]></td>\n";
      echo  "<td><input type=text size=5 name=field7_value value=$row[7]></td>\n";
      echo  "<td><input type=text size=5 name=field8_value value=$row[8]></td>\n";

      echo  "<td colspan=2 align=center><input type=submit value=\"  Do it!  \"></td>\n"; 
  echo  "</tr>\n";
      echo  "</form>\n"; 	

    }



  
  if (mysql_errno()!=0) { 
    switch (mysql_errno()) { 
      default: 
        echo  "Error #".mysql_errno(). " (".mysql_error(). ")<br>"; 
    } 
  } 
  if (!$sort) $sort="$field1";
  $query =  "select * from $table"; 
  switch ($sort) { 
    case  "pk": $query=$query. " order by pk"; break; 
    case  "$field1": $query=$query. " order by $field1"; break; 
    case  "$field2": $query=$query. " order by $field2"; break; 
    case  "$field3": $query=$query. " order by $field3"; break; 
    case  "$field4": $query=$query. " order by $field4"; break; 
    case  "$field5": $query=$query. " order by $field5"; break; 
    case  "$field6": $query=$query. " order by $field6"; break; 
    case  "$field7": $query=$query. " order by $field7"; break; 
    case  "$field8": $query=$query. " order by $field8"; break; 
  } 
  
  if ($read) { 
    $query = "select * from $table where pk=$read"; 
  }
  
  if ($keyword) { 
    $query = "select * from $table where $field1 LIKE '%$keyword%' OR $field2 LIKE '%$keyword%' OR $field3 LIKE '%$keyword%'"; 
  }   
  $result = mysql_query($query); 
  $rows = mysql_num_rows($result); 
  echo  "<table border=1 cellspacing=0>\n"; 
  echo  "<tr>\n";
  echo  "<td><a href=\"$PHP_SELF?sort=pk\">ID</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field1\">$field1_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field2\">$field2_label</a></td>\n";
  echo  "<td><a href=\"$PHP_SELF?sort=$field3\">$field3_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field4\">$field4_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field5\">$field5_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field6\">$field6_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field7\">$field7_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field8\">$field8_label</a></td>\n"; 
  echo  "<td colspan=2>";
  if ($keyword) echo "<a href=\"$PHP_SELF?sort=$field1\">View All</a>";
  echo  "</td>\n";
  echo  "</tr>\n"; 
  
if (!$read) { 
  echo  "<form action=\"$PHP_SELF\" method=\"post\">\n"; 
  echo  "<tr>\n";
  echo  "<td>New</td>\n"; 
  echo  "<input type=hidden name=insert value=1></td>\n"; 
  echo  "<td><input type=text size=10 name=field1_value></td>\n"; 
  echo  "<td><input type=text size=10 name=field2_value></td>\n";
  echo  "<td><input type=text size=11 name=field3_value></td>\n"; 
  echo  "<td><input type=text size=11 name=field4_value></td>\n"; 
  echo  "<td><input type=text size=11 name=field5_value></td>\n"; 
  echo  "<td><input type=text size=11 name=field6_value></td>\n"; 
  echo  "<td><input type=text size=11 name=field7_value></td>\n"; 
  echo  "<td><input type=text size=11 name=field8_value></td>\n"; 
  echo  "<td colspan=2 align=center>";
  echo  "<input type=submit value=\"   uAdd    \"></td>\n";
  echo  "<td></td>\n"; 
  echo  "</tr>\n";
  echo  "</form>\n";
  }

  if (!$read) { 
  while ($row = mysql_fetch_row($result)) { 
  echo  "<tr>\n"; 
      echo  "<td>$row[0]</a></td>\n"; 
  echo  "<td>$row[1]</td>\n"; 
      echo  "<td>$row[2]</td>\n"; 
      echo  "<td>$row[3]</td>\n"; 
      echo  "<td>$row[4]</td>\n"; 
      echo  "<td>$row[5]</td>\n"; 
      echo  "<td>$row[6]</td>\n"; 
      echo  "<td>$row[7]</td>\n"; 
      echo  "<td>$row[8]</td>\n"; 
      echo  "<td><a href=\"$PHP_SELF?read=$row[0]\">UEdit</a></td>\n";
      echo  "<td><a href=\"$PHP_SELF?delete=$row[0]\">UDelete</a></td>\n";
      echo  "<td><a href=\"$PHP_SELF?special=$row[0]\">USpecial</a></td>\n";
      echo  "</tr>\n";
      }
  }

  if ($read) {
  $row = mysql_fetch_row($result);
      echo  "<form action=\"$PHP_SELF\" method=\"post\">\n"; 
  echo  "<input type=hidden name=update value=\"$row[0]\">\n"; 
  echo  "<tr>\n";
      echo  "<td>$row[0]</a></td>\n"; 
      echo  "<td><input type=text size=10 name=field1_value  value=$row[1]></td>\n"; 
  echo  "<td><input type=text size=10 name=field2_value  value=$row[2]></td>\n";
      echo  "<td><input type=text size=5 name=field3_value value=$row[3]></td>\n";
      echo  "<td><input type=text size=5 name=field4_value value=$row[4]></td>\n";
      echo  "<td><input type=text size=5 name=field5_value value=$row[5]></td>\n";
      echo  "<td><input type=text size=5 name=field6_value value=$row[6]></td>\n";
      echo  "<td><input type=text size=5 name=field7_value value=$row[7]></td>\n";
      echo  "<td><input type=text size=5 name=field8_value value=$row[8]></td>\n";

echo  "<td colspan=2 align=center><input type=submit value=\"  Do it! \"></td>\n"; 
  echo  "</tr>\n";
      echo  "</form>\n"; 	  
    }

  mysql_close(); 
?>
</table>
</body>
</html>

Link to comment
Share on other sites

edit done my best ok.

 

all querys need diffrent names ,,, $query1 $query2  then so does all the $results1 $result2

 

so there no clashing ok.

 

the varable $read is undefined from 202 ok.

 

sort out that done all the $_SERVER['php_self'] for you ok

make a backup and try this ok. post findings.

 

<?php

$table = "prices";
$field1 = "seq";
$field1_label = "seq";

$field2 = "part";
$field2_label = "part";

$field3 = "grit";
$field3_label = "grit";

$field4 = "qpb";
$field4_label = "qpb";

$field5 = "bpc";
$field5_label = "bpc";

$field6 = "ppb";
$field6_label = "ppb";

$field7 = "ppc";
$field7_label = "ppc";

$field8 = "mslp";
$field8_label = "mslp";


$db_host="mysql";
$db_user="YoursTruly";
$db_pass="YeahRight!";
$db="mydatabase";
// End of variables definitions.  No need to edit code beyond this line

// Connect to database
mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db);
?>



<html>
<head>
<script language="JavaScript">
function focusform()
{
    document.forms[1].field1_value.focus();
}
</script>
</head>
<body OnLoad="focusform()">

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Keyword <input type="text" name="keyword">
<input type="submit" value="Search">
<input type="submit" name="gotomenu" value="Main Menu">


</form>

<?php

if (isset($_POST['gotomenu'])) header( "Location: menu.php" );
  if ($insert) { 
    mysql_query( "insert into $table ($field1, $field2, $field3, $field4, $field5, $field6, $field7, $field8) values (\"$field1_value\",\"$field2_value\",\"$field3_value\",\"$field4_value\",\"$field5_value\",\"$field6_value\",\"$field7_value\",\"$field8_value\")"); 
  } 
  if ($update) { 
    mysql_query( "update $table set $field1=\"$field1_value\", $field2=\"$field2_value\",$field3=\"$field3_value\",$field4=\"$field4_value\",$field5=\"$field5_value\",$field6=\"$field6_value\",$field7=\"$field7_value\",$field8=\"$field8_value\"  where pk=$update"); 
  } 
  if ($delete) { 
    mysql_query( "delete from $table where pk='$delete'"); 
  }  

  if ($special) {
          $query = "select * from $table where pk='$special'"; 
          $result = mysql_query($query); 
  $row = mysql_fetch_row($result);
  
  
echo" <form action=".$_SERVER['PHP_SELF']." method=\"post\">\n"; 

  echo  "<tr>\n";
  echo  "<td>Special</td>\n"; 
  echo  "<table border=1 cellspacing=0>\n"; 
  echo  "<tr>\n";
  echo  "<td><a href=\"$PHP_SELF?sort=pk\">ID</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field1\">$field1_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field2\">$field2_label</a></td>\n";
  echo  "<td><a href=\"$PHP_SELF?sort=$field3\">$field3_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field4\">$field4_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field5\">$field5_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field6\">$field6_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field7\">$field7_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field8\">$field8_label</a></td>\n"; 
  echo  "<td colspan=2>";
  if ($keyword) echo "<a href=\" ".$_SERVER['PHP_SELF']."?sort=$field1\">View All</a>";
  echo  "</td>\n";
  echo  "</tr>\n"; 

      echo  "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">\n"; 
  echo  "<input type=hidden name=update value=\"$row[0]\">\n"; 
  echo  "<tr>\n";
      echo  "<td>$row[0]</a></td>\n"; 
      echo  "<td><input type=text size=10 name=field1_value  value=$row[1]></td>\n"; 
  echo  "<td><input type=text size=10 name=field2_value  value=$row[2]></td>\n";
      echo  "<td><input type=text size=5 name=field3_value value=$row[3]></td>\n";
      echo  "<td><input type=text size=5 name=field4_value value=$row[4]></td>\n";
      echo  "<td><input type=text size=5 name=field5_value value=$row[5]></td>\n";
      echo  "<td><input type=text size=5 name=field6_value value=$row[6]></td>\n";
      echo  "<td><input type=text size=5 name=field7_value value=$row[7]></td>\n";
      echo  "<td><input type=text size=5 name=field8_value value=$row[8]></td>\n";

      echo  "<td colspan=2 align=center><input type=submit value=\"  Do it!  \"></td>\n"; 
  echo  "</tr>\n";
      echo  "</form>\n"; 	

    }
    
    if (mysql_errno()!=0) { 
    switch (mysql_errno()) { 
      default: 
        echo  "Error #".mysql_errno(). " (".mysql_error(). ")<br>"; 
    } 
  } 
  if (!$sort) $sort="$field1";
  $query =  "select * from $table"; 
  switch ($sort) { 
    case  "pk": $query=$query. " order by pk"; break; 
    case  "$field1": $query=$query. " order by $field1"; break; 
    case  "$field2": $query=$query. " order by $field2"; break; 
    case  "$field3": $query=$query. " order by $field3"; break; 
    case  "$field4": $query=$query. " order by $field4"; break; 
    case  "$field5": $query=$query. " order by $field5"; break; 
    case  "$field6": $query=$query. " order by $field6"; break; 
    case  "$field7": $query=$query. " order by $field7"; break; 
    case  "$field8": $query=$query. " order by $field8"; break; 
  } 
  
  if ($read) { 
    $query = "select * from $table where pk='$read' ";
   $result=mysql_query($query); 
  }
  
  if ($keyword) { 
    $query = "select * from $table where $field1 LIKE '%$keyword%' OR $field2 LIKE '%$keyword%' OR $field3 LIKE '%$keyword%'"; 
  }   
  $result = mysql_query($query); 
  $rows = mysql_num_rows($result); 
  echo  "<table border=1 cellspacing=0>\n"; 
  echo  "<tr>\n";
  echo  "<td><a href=\"$PHP_SELF?sort=pk\">ID</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field1\">$field1_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field2\">$field2_label</a></td>\n";
  echo  "<td><a href=\"$PHP_SELF?sort=$field3\">$field3_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field4\">$field4_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field5\">$field5_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field6\">$field6_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field7\">$field7_label</a></td>\n"; 
  echo  "<td><a href=\"$PHP_SELF?sort=$field8\">$field8_label</a></td>\n"; 
  echo  "<td colspan=2>";
  if ($keyword) echo "<a href=\"".$_PHP['PHP_SELF']."?sort=$field1\">View All</a>";
  echo  "</td>\n";
  echo  "</tr>\n"; 
  
if (!$read) { 
  echo  "<form action=\" ".$_SERVER['PHP_SELF']."\" method=\"post\">\n"; 
  echo  "<tr>\n";
  echo  "<td>New</td>\n"; 
  echo  "<input type=hidden name=insert value=1></td>\n"; 
  echo  "<td><input type=text size=10 name=field1_value></td>\n"; 
  echo  "<td><input type=text size=10 name=field2_value></td>\n";
  echo  "<td><input type=text size=11 name=field3_value></td>\n"; 
  echo  "<td><input type=text size=11 name=field4_value></td>\n"; 
  echo  "<td><input type=text size=11 name=field5_value></td>\n"; 
  echo  "<td><input type=text size=11 name=field6_value></td>\n"; 
  echo  "<td><input type=text size=11 name=field7_value></td>\n"; 
  echo  "<td><input type=text size=11 name=field8_value></td>\n"; 
  echo  "<td colspan=2 align=center>";
  echo  "<input type=submit value=\"   uAdd    \"></td>\n";
  echo  "<td></td>\n"; 
  echo  "</tr>\n";
  echo  "</form>\n";
  }

  if (!$read) { 
  while ($row = mysql_fetch_row($result)) { 
  echo  "<tr>\n"; 
      echo  "<td>$row[0]</a></td>\n"; 
  echo  "<td>$row[1]</td>\n"; 
      echo  "<td>$row[2]</td>\n"; 
      echo  "<td>$row[3]</td>\n"; 
      echo  "<td>$row[4]</td>\n"; 
      echo  "<td>$row[5]</td>\n"; 
      echo  "<td>$row[6]</td>\n"; 
      echo  "<td>$row[7]</td>\n"; 
      echo  "<td>$row[8]</td>\n"; 
      echo  "<td><a href=\"".$_SERVER['PHP_SELF']."?read=$row[0]\">UEdit</a></td>\n";
      echo  "<td><a href=\" ".$_SERVER['PHP_SELF']."?delete=$row[0]\">UDelete</a></td>\n";
      echo  "<td><a href=\"".$SERVER['PHP_SELF']."?special=$row[0]\">USpecial</a></td>\n";
      echo  "</tr>\n";
      }
  }

  if ($read) {
  $row = mysql_fetch_row($result);
      echo  "<form action=\"".$_SERVER['PHP_SELF']." method=\"post\">\n"; 
  echo  "<input type=hidden name=update value=\"$row[0]\">\n"; 
  echo  "<tr>\n";
      echo  "<td>$row[0]</a></td>\n"; 
      echo  "<td><input type=text size=10 name=field1_value  value=$row[1]></td>\n"; 
  echo  "<td><input type=text size=10 name=field2_value  value=$row[2]></td>\n";
      echo  "<td><input type=text size=5 name=field3_value value=$row[3]></td>\n";
      echo  "<td><input type=text size=5 name=field4_value value=$row[4]></td>\n";
      echo  "<td><input type=text size=5 name=field5_value value=$row[5]></td>\n";
      echo  "<td><input type=text size=5 name=field6_value value=$row[6]></td>\n";
      echo  "<td><input type=text size=5 name=field7_value value=$row[7]></td>\n";
      echo  "<td><input type=text size=5 name=field8_value value=$row[8]></td>\n";

echo  "<td colspan=2 align=center><input type=submit value=\"  Do it! \"></td>\n"; 
  echo  "</tr>\n";
      echo  "</form>\n"; 	  
    }

  mysql_close(); 
?>
</table>
</body>
</html>

Link to comment
Share on other sites

Redarrow! Thanks! You really helped me.

 

I went through my code and looked at everything which would be conditioned by !read, and whereever it occurs, I add also (!special).

 

 

I also experimented with a Special button which calls another php program, and passes the primary key as a parameter. Adding a function in this manner would give me lots of potential to work with many fields, and take up a lot of screen real estate, and when I am done, return to the original program.

 


if ($special) {
    header( "Location: priceparameter.php?parameter=$special" );
    }
    
<?
$id = $_GET{parameter};
//$id = "test";
echo("<a href=\"priceeditupdate3.php\">Submit $id</a>");

?>

 

Link to comment
Share on other sites

Redarrow!  I took the sample code you provided for me, at the beginning of this thread, and worked with it for an hour or two last night, but I keep getting a mysql error.  I numbered the error messages so I could know which one, and it is #4.

 

I can tell it is connecting to my database ok.

 

 


<?php

//session_start(); 
// if your using a session for the users and the id is set to the session.

// database connection

$db = mysql_connect("mysql","yourstruly","yeahright!");
mysql_select_db("mydatabase",$db);



// if link is pressed to delete entry.

if($_GET['cmd']=="delete"){

//get the name to delete the entry.

$name = $_GET['first_name'];

$query2 = "delete * from phone_book where first_name = '$name' ";

$result2 = mysql_query($query2)or die("mysql_error1");

if(mysql_affected_rows($result2)){

	echo "Entry Deleted From Database!";
}
}



// if link is pressed to edit entry.

if($_GET['cmd']=="edit"){

$name = $_POST['name'];	

$query4 = "select * from phone_book where first_name = '$name'";
$result4 = mysql_fetch_assoc($query4)or die("mysql_error 2");

//while loop for query to get all database information.

while($x = mysql_fetch_assoc($result4)){

// form for editing current information.

echo"<form method='POST' action=' '".$_SERVER['PHP_SELF']."'?cmd=delete'>
<br>
Please edit first name:
<br>	
    <input type = 'text' name='first_name' value=''".$x['first_name']."'>
    
<br>
Please edit last name:
<br>	
    <input type = 'text' name='last_name' value=''".$x['last_name']."'>
    
    
<br>
Please edit street:
<br>	
    <input type = 'text' name='phone' value=''".$x['phone']."'>
    
<br>

    
    <input type = 'submit' name='submit' value='new edit'>
    
    ";

}
// if form posted.

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

//get the name to delete the entry.

$name=$_GET['first_name'];
$name=addslashes($_POST['name']);
$last_name=addslashes($_POST['last_name']);
$phone=addslashes($_POST['phone']);

$query3 = "update set first_name,last_name, phone from phone_book where fist_name='$name' ";

$result3=mysql_query($query3)or die("mysql_error3");

if(mysql_affected_rows($resul3)){

	echo "Entry Deleted From Database!";
}
}
}

//database query select the table name and use a where clause to select the database entry via the current user logged in
//but if you have no current user for the where cluse you can use a id you no within the database field or not
// choose any that will result in showing all data.

$what_ever = 999;
$query1 = "select * from phone_book where id != 0";
$result1 = mysql_fetch_assoc($query1)or die("mysql_error 4");

//while loop for query to get all database information.

while( $x = mysql_fetch_assoc($result1)){

//display table with the while loop set to $x

echo "<table align='center'>
<td>First name:<br>".$x['first_name']."<br></td>
            <td>Firstname:<br>".$x['first_name']."<br></td>
            <br><td><br>LastName:<br>".$x['last_name']."<br></td>
            <td><br>Phone:<br>".$x['phone']."</td></table>";

//links for delete and edit set in the wile loop to get correct info to edit or delete.

echo "<br><center><a href='".$_SERVER['PHP_SELF']."?cmd=delete&first_name='".$x['first_name']."'>Delete Entry</a>

<p></p>

<a href='".$_SERVER['PHP_SELF']."?cmd=edit&first_name='".$x['first_name']."'>Edit Entry</a>

</center>

<p></p>";
}

?>

Link to comment
Share on other sites

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

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

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

×   Your previous content has been restored.   Clear editor

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

×
×
  • Create New...

Important Information

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