Hello all!
I have managed to perform the following:
- Adding Data to a MySQL Database.
- Retrieving/Viewing Data from MySQL Database.
I currently need to be able to perform the following:
- While I view Data coming from the MySQL Database, I can currently edit the data on the same "view" form. Then I want to be able to save the changed data back to the Database.
The problem is that when finished editing the data, the "update" button does NOT work...
I want to be able to put/save the altered data back to the MySQL Database.
And the button I have created does NOT work...
Here is the code I use to enter NEW data to the MySQL Database:
<?php
if ($_POST[op] !="add") {
// haven't seen the form, so show it
$display_block = "<h1>Add an Entry</h1>
<form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
<p><strong>First/Last Names:</strong><br>
<input type=\"text\" name=\"f_name\" size=30 maxlength=75>
<input type=\"text\" name=\"l_name\" size=30 maxlength=75>
<p><strong>Address:</strong><br>
<input type=\"text\" name=\"address\" size=30>
<p><strong>City/State/Zip:</strong><br>
<input type=\"text\" name=\"city\" size=30 maxlenght=50>
<input type=\"text\" name=\"state\" size=15 maxlenght=15>
<input type=\"text\" name=\"zipcode\" size=5 maxlenght=5>
<p><strong>Address Type:</strong><br>
<input type=\"radio\" name=\"add_type\" value=\"home\" checked> home
<input type=\"radio\" name=\"add_type\" value=\"work\"> work
<input type=\"radio\" name=\"add_type\" value=\"other\"> other
<p><strong>Telephone Number:</strong><br>
<input type=\"text\" name=\"tel_number\" size=30 maxlenght=25>
<input type=\"radio\" name=\"tel_type\" value=\"home\" checked> home
<input type=\"radio\" name=\"tel_type\" value=\"work\"> work
<input type=\"radio\" name=\"tel_type\" value=\"other\"> other
<p><strong>Fax Number:</strong><br>
<input type=\"text\" name=\"fax_number\" size=30 maxlenght=25>
<input type=\"radio\" name=\"fax_type\" value=\"home\" checked> home
<input type=\"radio\" name=\"fax_type\" value=\"work\"> work
<input type=\"radio\" name=\"fax_type\" value=\"other\"> other
<p><strong>Email Address:</strong><br>
<input type=\"text\" name=\"email\" size=30 maxlenght=25>
<input type=\"radio\" name=\"email_type\" value=\"home\" checked> home
<input type=\"radio\" name=\"email_type\" value=\"work\"> work
<input type=\"radio\" name=\"email_type\" value=\"other\"> other
<p><strong>Personal Note:</strong><br>
<textarea name=\"note\" cols=35 rows=5 wrap=virtual></textarea>
<input type=\"hidden\" name=\"op\" value=\"add\">
<! Comment: This is a hidden field>
<! Comment: All the IF-Else IF statements are based on this line>
<! Comment: SOS - Never use in a comment exclamation marks or quote marks>
<p>
<input type=\"button\" id=\"button1\"
value=\"Return to Main Menu\" onclick=\"window.location='http://localhost/TestSams19.378 - Creating a Menu.php' \" />
<! I added this button, so that it returns us to the main Menu.>
<input type=\"reset\" value=\"Clear Fields\" />
<! The above button Clears all the Field's contents>
<input type=\"submit\" name=\"submit\" value=\"Add Entry\">
</p>
</FORM>";
}
else if ($_POST[op] == "add"){
//time to add tables, so check for required fields
if (($_POST[f_name] == "") || ($_POST[l_name] == "")){
// If the "First Name" or "Last Name" fields are NOT filled, we reload this
// page.
header("Location: TestSams19.379 - addentry.php");
exit;
// Make sure the Location File is correct!
}
// connect to database
$conn = mysql_connect("localhost", "root", "test")
or die(mysql_error());
// Make sure the user name you use to connect to mysql database are correct!
mysql_select_db("testDB",$conn) or die(mysql_error());
// add to master_id for use with other tables
$add_master = "insert into master_name values ('', now(), now(),
'$_POST[f_name]', '$_POST[l_name]')";
mysql_query($add_master) or die (mysql_error());
// get master_id for use with other tables
$master_id = mysql_insert_id();
if (($_POST[address]) || ($_POST[city]) || ($_POST[state]) ||
($_POST[zipcode])) {
// something relevant, so add to address table
$add_address = "insert into address values ('',$master_id, now(), now(),
'$_POST[address]', '$_POST[city]', '$_POST[state]', '$_POST[zipcode]',
'$_POST[add_type]')";
mysql_query($add_address) or die (mysql_error());
}
if ($_POST[tel_number]) {
// something relevant, so add to telephone table
$add_tel = "insert into telephone values ('',$master_id, now(), now(),
'$_POST[tel_number]', '$_POST[tel_type]')";
mysql_query($add_tel) or die (mysql_error());
}
if ($_POST[fax_number]) {
// something relevant, so add to fax table
$add_fax = "insert into fax values ('',$master_id, now(), now(),
'$_POST[fax_number]', '$_POST[fax_type]')";
mysql_query($add_fax) or die (mysql_error());
// Example of error: "Query was empty".
}
if ($_POST[email]) {
// something relevant, so add to email table
$add_email = "insert into email values ('',$master_id, now(), now(),
'$_POST[email]', '$_POST[email_type]')";
mysql_query($add_email) or die (mysql_error());
}
if ($_POST[note]) {
// something relevant, so add to notes table
$add_note = "insert into personal_notes values ('',$master_id, now(),
now(), '$_POST[note]')";
mysql_query($add_note) or die (mysql_error());
}
$display_block = "<h1>Entry Added</h1>
<p>Your entry has been added. Would you like to
<a href=\"TestSams19.379 - addentry.php\">add another</a>?</p>";
// Make sure the above link points to the correct page.
}
?>
<html>
<head>
<title>Add an Entry</title>
</head>
<body>
<?php echo $display_block; ?>
</body>
</html>
The above code works fine!!!
It does NOT require any fix...
I just inserted it here, so that it might help you see how all things work...
The problematic code is the following:
<?php
// connect to database
$conn = mysql_connect("localhost", "root", "test")
or die(mysql_error());
// Make sure the user name you use to connect to mysql database are correct!
mysql_select_db("testDB",$conn) or die(mysql_error());
if ($_POST[op] != "view")
{
// haven't seen the form, so show it
$display_block = "<h1>Select an Entry</h1>";
// get parts of records
$get_list = "select id, concat_ws(', ', l_name, f_name)
as display_name
from master_name order by l_name, f_name";
$get_list_res = mysql_query($get_list) or die (mysql_error());
if (mysql_num_rows($get_list_res) < 1)
{
// no records
$display_block .="<p><em>Sorry, no records to select</em></p>";
}
else
{
// has records, so get the results and print in a form
$display_block .= "
<form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
<p><strong>Select a Record to View:</strong><br>
<select name=\"sel_id\">
<option value=\"\">-- Select One --</option>";
while ($recs = mysql_fetch_array($get_list_res))
{
$id = $recs['id'];
$display_name = stripslashes($recs['display_name']);
$display_block .= "<option value=\"$id\">
$display_name</option>";
}
$display_block .= "
</select>
<input type=\"hidden\" name=\"op\" value=\"view\">
<p>
<input type=\"button\" id=\"button1\"
value=\"Return to Main Menu\"
onclick=\"window.location='http://localhost/TestSams19.378 - Creating a Menu.php' \" />
<! I added this button, so that it returns us to the main Menu.>
<input type=\"submit\" name=\"submit\"
value=\"View Selected Entry\">
</p>
</FORM>";
}
}
else if ($_POST[op] == "view")
{
//check for required fields
if ($_POST[sel_id] == "")
{
header("Location: selentry.php");
exit;
}
$display_block .= "
<form method=\"post\" action=\"$_SERVER[PHP_SELF]\">";
//get master_info
$get_master = "select concat_ws(' ', f_name, l_name) as display_name
from master_name where id = $_POST[sel_id]";
$get_master_res = mysql_query($get_master);
$display_name = stripslashes(mysql_result($get_master_res, 0,
'display_name'));
$display_block = "<h1>Showing Record for $display_name</h1>";
//get last_name
$get_lastname = "select l_name
from master_name
where id = $_POST[sel_id]";
$get_lastname_res = mysql_query($get_lastname);
if (mysql_num_rows($get_lastname_res) > 0)
{
$display_block .= "<P><strong>Last Name:</strong><br>
<u1>";
while ($lastname_info = mysql_fetch_array($get_lastname_res))
{
$last_name = $lastname_info[l_name];
$display_block .= "<input type=\"text\" name=\"l_name\" size=30 maxlength=75 value=\"$last_name\">";
}
$display_block .= "</u1>";
}
// print_r($name_of_variable);
// You can use the above command when you want to find what valua a variable has.
To be continued on the following message due to size limitations...