Networking North Dakota Here Service Your Computer Here
AmegA Design - You Be Creative - We will help you create it! Computers For Kids North Dakota
AmegA Web Design, & Bulk Ink Systems
New and Used Computers and equipment in North Dakota Tutoring for Windows based PC's
AmegA News Letter and Information STOP BUYING EXPENSIVE INK CARTRIDGES!
Streaming Internet Communication is the Future  
  Streaming Internet Communication is the Future
 
Streaming Internet Communication is the Future

Thank You Web Monkey
http://www.webmonkey.com
I Have made a copy of these how to dues because of the awesome direction it gives for php learning

Web Monkey PHP

 


PHP
By Web Monkey

Page 5 — Make the Forms Smarter

--------------------------------------------------------------------------------
Throughout this tutorial, I've been loading the SQL statement into a variable ($sql) before firing the query at the database with mysql_query(). This is useful for debugging. If something goes wrong, you can always echo the SQL to the screen to examine it for mistakes.

We already know how to get data into the database. Now let's try modifying records that are already in the database. Editing data combines two elements we've already seen: displaying data on the screen and sending data back to the database via form input. However, editing is slightly different in that we have to show the appropriate data in the form.

First, let's recycle the code from Lesson 1 to display the employee names on our page. But this time through, we're going to populate our form with employee information. It should look a little like this:

<html>

<body>

<?php

$db = mysql_connect("localhost", "root");

mysql_select_db("mydb",$db);

if ($id) {

// query the DB

$sql = "SELECT * FROM employees WHERE id=$id";

$result = mysql_query($sql);

$myrow = mysql_fetch_array($result);

?>

<form method="post" action="<?php echo $PHP_SELF?>">

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

First name:<input type="Text" name="first" value="<?php echo $myrow["first"] ?>"><br>

Last name:<input type="Text" name="last" value="<?php echo $myrow["last"] ?>"><br>

Address:<input type="Text" name="address" value="<?php echo $myrow["address"] ?>"><br>

Position:<input type="Text" name="position" value="<?php echo $myrow["position"] ?>"><br>

<input type="Submit" name="submit" value="Enter information">

</form>

<?php

} else {

// display list of employees

$result = mysql_query("SELECT * FROM employees",$db);

while ($myrow = mysql_fetch_array($result)) {

printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF, $myrow["id"], $myrow["first"], $myrow["last"]);

}

}

?>

</body>

</html>


We just echoed the field information into the value attribute of the each element, which was fairly easy. Let's build on this a little more. We will add the ability to send the edited code back to the database. Again, we're going to use the Submit button to test whether we need to process the form input. Also note the slightly different SQL statement we use.


<html>

<body>

<?php

$db = mysql_connect("localhost", "root");

mysql_select_db("mydb",$db);

if ($id) {

if ($submit) {

$sql = "UPDATE employees SET first='$first',last='$last',address='$address',position='$position' WHERE id=$id";

$result = mysql_query($sql);

echo "Thank you! Information updated.\n";

} else {

// query the DB

$sql = "SELECT * FROM employees WHERE id=$id";

$result = mysql_query($sql);

$myrow = mysql_fetch_array($result);

?>

<form method="post" action="<?php echo $PHP_SELF?>">

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

First name:<input type="Text" name="first" value="<?php echo $myrow["first"] ?>"><br>

Last name:<input type="Text" name="last" value="<?php echo $myrow["last"] ?>"><br>

Address:<input type="Text" name="address" value="<?php echo $myrow["address"] ?>"><br>

Position:<input type="Text" name="position" value="<?php echo $myrow["position"] ?>"><br>

<input type="Submit" name="submit" value="Enter information">

</form>

<?php

}

} else {

// display list of employees

$result = mysql_query("SELECT * FROM employees",$db);

while ($myrow = mysql_fetch_array($result)) {

printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF, $myrow["id"], $myrow["first"], $myrow["last"]);

}

}

?>

</body>

</html>


And that's that. We've managed to combine most of the features we've seen into one script. You can also see how we've used an if() statement inside another if() statement to check for multiple conditions.

It's time to put it all together and make one killer script.

next page»









  Streaming Internet Communication is the Future
  Streaming Internet Communication is the Future
 
Streaming Internet Communication is the Future

Thank You Web Monkey
http://www.webmonkey.com
I Have made a copy of these how to dues because of the awesome direction it gives for php learning

Web Monkey PHP

 



AmegA Home 1910 E Front Ave Bismarck, ND

1910 E Front Ave - Bismarck, ND
701-223-9694
HOME * NETWORKING * SERVICE * SALES * TUTORING * WEB DESIGN * COMPUTERS FOR KIDS * NEWSLETTER * CONTINUOUS INK MACHINE SYSTEM * VIDEO EMAIL * CONTACT US HERE
Would You Like To Hear Some Silly Commercials That AmegA Has Placed On Some Cool Radio Stations Here
Bill Straily& Scott MC Gowen At Cumulus - Jason Huber & The B Man at Clear Channel
Commercials
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17