?
Solved

Scaffolding PHP: Add an edit function

Posted on 2008-10-02
10
Medium Priority
?
1,309 Views
Last Modified: 2013-12-12
I've been trying to add an edit function to this tutorials i've got from dot Net magazine which is a scaffolding PHP system, how can i achive edit and delete for this system, I've add the full code below:


<?php
	mysql_connect("localhost", "netusah", "blabla");
	mysql_select_db("mydb");
 
	function print_header() {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Instaform!</title>
<style>
input.warn	{ border: 1px solid red; }
p.warn	{ color: red; font-weight: bold; }
</style>
</head>
 
<body>
 
<?php
	}
 
	function print_footer() {
?>
</body>
</html>
 
<?php
	}
 
	function pretty_print($string) {
		return ucfirst(str_replace("_", " ", $string));
	}
 
	function make_safe($array) {
		foreach($array as $var => $val) {
			$array[$var] = mysql_real_escape_string($val);
		}
 
		return $array;
	}
 
	function get_table_fields($table) {
		$result = mysql_query("SHOW FIELDS FROM $table;");
 
		$rows = array();
 
		while ($row = mysql_fetch_assoc($result)) {
			// if we're reading the primary key, skip it
			if ($row["Extra"] == "auto_increment") continue;
 
			$this_row = $row;
 
			if (stripos($row["Type"], "char") !== false) {
				$this_row["Type"] = "char";
			} else if (stripos($row["Type"], "text") !== false) {
				$this_row["Type"] = "text";
			} else if (stripos($row["Type"], "int") !== false) {
				$this_row["Type"] = "int";
			}
 
			$rows[$row["Field"]] = $this_row;
		}
 
		return $rows;
	}
 
	function validate_form($form, $table) {
		$problems = array();
 
		$fields = get_table_fields($table);
 
		if (count($form) != count($fields)) {
			$problems[] = "Fatal error.";
			return $problems;
		}
 
		foreach($fields as $name => $info) {
			if (!isset($form[$name])) {
				$problems[] = "Fatal error with $name.";
				return $problems;
			}
 
			if ($info["Null"] == "NO") {
				// this field must be provided!
				if (trim($form[$name]) == "") {
					$problems[$name] = "This field must be completed.";
				}
			}
		}
 
		return $problems;
	}
 
	function show_table_links($table) {
		$name = pretty_print($table);
 
		echo "<h1>$name</h1>";
		echo "<p><a href=\"create.php?type=$table\">New</a> | <a href=\"edit.php?type=$table\">Edit</a></p>";
 
		$fields = get_table_fields($table);
		$fields_query = implode(", ", array_keys($fields));
 
		echo "<table width=\"100%\">";
 
		echo "<tr>";
		foreach($fields as $field) {
			$field = pretty_print($field["Field"]);
			echo "<th>$field</th>";
		}
		echo "</tr>";
 
 
		$result = mysql_query("SELECT $fields_query FROM $table;");
 
		while ($row = mysql_fetch_assoc($result)) {
			echo "<tr>";
			foreach($row as $field) echo "<td>$field</td>";
			echo "</tr>";
		}
 
 
		echo "</table>";
	}
 
	function create_new($table) {
		echo "<h1>Create new item</h1>";
 
		$fields = get_table_fields($table);
 
		if (isset($_POST["save"])) {
			if (!isset($_POST[$table])) {
				echo "<p>Fatal error.</p>";
			} else {
				$form = make_safe($_POST[$table]);
 
				$error_list = validate_form($form, $table);
 
				if ($error_list == array()) {
					$query = "INSERT INTO $table (ID, " .
							implode(", ", array_keys($form)) .
							") VALUES ('', '" .
							implode("', '", array_values($form)) .
							"');";
 
					mysql_query($query);
 
					header("location: index.php");
					exit;
				}
			}
		}
 
		echo "<form method=\"post\">";
 
		foreach($fields as $field) {
			$pretty_name = pretty_print($field["Field"]);
 
			if (isset($_POST[$table][$field["Field"]])) {
				// value submitted already!
				$value = $_POST[$table][$field["Field"]];
			} else {
				if ($field["Default"] != "") {
					// got a default value!
					$value = $field["Default"];
				} else {
					// empty...
					$value = "";
				}
			}
 
			if (isset($error_list)) {
				if (isset($error_list[$field["Field"]])) {
					$error_class = "warn";
					$error_text = " - " . $error_list[$field["Field"]];
				} else {
					$error_class = "normal";
					$error_text = "";
				}
			} else {
				$error_class = "normal";
				$error_text = "";
			}
 
			echo "<p>$pretty_name: </p>";
 
			switch ($field["Type"]) {
				case "text":
					echo "<p class=\"$error_class\"><textarea class=\"$error_class\" name=\"{$table}[{$field["Field"]}]\">$value</textarea> $error_text</p>";
					break;
 
				default:
					echo "<p class=\"$error_class\"><input class=\"$error_class\" type=\"text\" name=\"{$table}[{$field["Field"]}]\" value=\"$value\" /> $error_text</p>";
			}
		}
 
		echo "<p><input type=\"hidden\" name=\"save\" value=\"true\" /></p>";
		echo "<p><input type=\"submit\" value=\"Create\" /></p>";
		echo "<p><a href=\"index.php\">Cancel</a></p>";
	}
?>

Open in new window

0
Comment
Question by:ostrox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
10 Comments
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 22766048
UPDATE $table SET $field = $value

DELETE from $table WHERE $where_condition
0
 

Author Comment

by:ostrox
ID: 22795886
Where do I add this?
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 22802287
Basically you'll need to create a process page for deleting a row by the ID;
therefore, on your output function you will need a new link to delete.php.
looks like you are linking to edit.php already.

Did you add the edit.php link?
                echo "<h1>$name</h1>";
                echo "<p><a href=\"create.php?type=$table\">New</a> | <a href=\"edit.php?type=$table\">Edit</a></p>";

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 22802288

                echo "<h1>$name</h1>";
                echo "<p><a href=\"create.php?type=$table\">New</a> | <a href=\"edit.php?type=$table\">Edit</a><a href=\"delete.php?type=$table\">Delete</a></p>";

Open in new window

0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 22802292
With your edit.php
READ the current roq and PREFILL the inputs with this data
Then,
You will need to run the SQL after sumbiting the new input information from edit.php
UPDATE table_name SET column_name = value [, column_name = value ...] [WHERE condition]

===

With your delete.php
You will need to run the SQL code on opening delete.php
DELETE FROM table_name [WHERE condition]
SQL Statement Syntax:
 
UPDATE table_name SET column_name = value [, column_name = value ...] [WHERE condition]
 
DELETE FROM table_name [WHERE condition]

Open in new window

0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 22802293
With your edit.php
READ the current roq and PREFILL the inputs with this data
Then,
You will need to run the SQL after sumbiting the new input information from edit.php
UPDATE table_name SET column_name = value [, column_name = value ...] [WHERE condition]

===

With your delete.php
You will need to run the SQL code on opening delete.php
DELETE FROM table_name [WHERE condition]
0
 
LVL 19

Accepted Solution

by:
NerdsOfTech earned 1500 total points
ID: 22802294
With your edit.php
READ the current ROW and PREFILL the inputs with this data
Then,
You will need to run the SQL after sumbiting the new input information from edit.php
UPDATE table_name SET column_name = value [, column_name = value ...] [WHERE condition]

===

With your delete.php
You will need to run the SQL code on opening delete.php
DELETE FROM table_name [WHERE condition]
0
 

Author Comment

by:ostrox
ID: 23025007
thanks for the replys ill get back to you soon if all worked
0

Featured Post

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question