Jump to content

Trouble Making Single Page Form Processing


ooleyguy

Recommended Posts

I learned FORTRAN in college lo these many years ago and now have a web project that I'm helping out a local charity with. Here's what we've done so far:

 

We've created an extremely simple MySQL database with a table called "people" that has two fields, "id" and "name". This list represents the people who currently need the charity's assistance. Their contact info is unimportant at this juncture.

 

What I'm trying to do is to create a page that has a list of people in text boxes with change and delete buttons and a top text box that will add a new person.

 

The page will accept the addition, change, or delete and then re-display it with the updated database contents. Sample screen shown.

 

1q1ic8.jpg

 

This is the code I've written so far. I can add people using a two page system, but no matter what tutorial I use, it's not working. Once I can get people added using this form, I'm pretty sure I can get change and delete to work on my own. Thank you in advance for any assistance you can give or other resources I need to look to.

<?php require_once("_includes/db_connection.php"); ?>
<?php require_once("_includes/functions.php"); ?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Edit People</title>
</head>

<body>

	<?php 
	
		if (isset($_POST['add'])) {
			$name = mysql_prep($_POST["name"]);
			
			$query  = "INSERT INTO people (";
			$query .= " name";
			$query .= ") VALUES (";
			$query .= " '{$name}'";
			$query .= ")";
		
			$result = mysqli_query($connection, $query);
			
		} elseif (isset($_POST['add'])) {
			// Some code to update the field
			
		} elseif (isset($_POST['add'])) {
			// some code to delete the field
			
		} else {
			$query  = "SELECT * ";
			$query .= "FROM people";
			$result = mysqli_query($connection, $query);
			if (!$result) {
				die("Database query failed.");
			}
		}
	?>

	<form id="crud" name="crud" action="edit_list.php" method="post">
	
	<?php 
		echo "<input type=\"text\" name=\"name\" value=\"\"> <input type=\"button\" name=\"add\" value=\"Add\"><br />";
		
		while($row = mysqli_fetch_assoc($result)) {
			echo "<input type=\"text\" name=\"name\" value=\"{$row['name']}\">
			<input type=\"button\" name=\"change\" value=\"Change\">
			<input type=\"button\" name=\"delete\" value=\"Delete\"><br />";
		}
		
	?>	
	
	</form>

</body>
</html>

<?php mysqli_close($connection); ?>
Link to comment
Share on other sites

Name is a mysql reserved word

 

Use backticks `name` or rename it

 

No, it is a keyword but not reserved. Example

mysql> CREATE TABLE nametest (name varchar(30));
Query OK, 0 rows affected (0.27 sec)

mysql> INSERT INTO nametest VALUES ('Peter'),('Paul'),('Mary');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> SELECT name FROM nametest;
+-------+
| name  |
+-------+
| Peter |
| Paul  |
| Mary  |
+-------+
3 rows in set (0.00 sec)
Link to comment
Share on other sites

your SELECT query is part of the 'get method' code that determines what to display on the page. it should not be conditional. your 'post method' form processing code should be near the top of your file and come before any html markup.

 

see the following post for a suggested single page code layout that will help make this process fool proof - http://forums.phpfreaks.com/topic/297824-database-issues-and-working/?do=findComment&comment=1519095

 

also, when asking for help, you must tell or show us exactly what result you got, even if you got a blank page, since we are not sitting there with you and don't know what "it's not working" means.

Link to comment
Share on other sites

Ok, in response to the several suggestions, my code has been changed somewhat. All the database code has been stripped from the <body>.

<?php require_once("_includes/db_connection.php"); ?>
<?php require_once("_includes/functions.php"); ?>

<?php 

	if (isset($_POST['add'])) {
		$person = $_POST["person"];
		$insert_query  = "INSERT INTO 'people'('person') VALUES ('{$person}')";
		$insert_result = mysqli_query($connection, $insert_query);
		
		// Testing block
		if ($insert_result) {
			echo $insert_query . "<br />";
			echo $person. "<br />";
			echo $_POST["person"];
		} else {
			echo "FAIL!";
		}
		
	} elseif (isset($_POST['change'])) {
		echo "You clicked change";
		
	} elseif (isset($_POST['delete'])) {
		echo "You clicked delete";
		
	} else {
		echo "FAIL";
	}
	
		$select_query  = "SELECT * ";
		$select_query .= "FROM people";
		$select_result = mysqli_query($connection, $select_query);
		if (!$select_result) {
			die("Database query failed.");
		}

?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Edit People</title>
</head>

<body>
	
	<form id="crud" name="crud" action="edit_list.php" method="post">
	
	<?php 		
		while($row = mysqli_fetch_assoc($select_result)) {
			echo "<input type=\"text\" name=\"person\" value=\"{$row['person']}\">
			<input type=\"submit\" name=\"change\" value=\"Change\">
			<input type=\"submit\" name=\"delete\" value=\"Delete\"><br />";
		}
		echo "<input type=\"text\" name=\"person\" value=\"\"> <input type=\"submit\" name=\"add\" value=\"Add\"><br />";
	?>	
	
	</form>

</body>
</html>

<?php mysqli_close($connection); ?>

 

Before I added the redirect test, when I typed a person's name into the blank text-box and click add, I got this result:

 

2qcgeo1.jpg

 

If I type that insert command into MySQL console, it creates a user with no errors.  However, after I added the redirect test I get redirected to the failure page. For some reason, it seems that nothing is being sent to the database. I'm obviously reading from the database, and I've tried using root access to make sure that I had all the privileges. My head is getting squishy from banging it against the desk. Should I turn error reporting on in MySQL?

Edited by ooleyguy
Link to comment
Share on other sites

PROBLEM SOLVED! YAY!

 

Just a stupid error on my part. The form submit button had name="person" and the $_POST was looking for "add".

 

Thank you for the help. The suggestions on where to put code made it easier to read, and inspired me to write functions to handle the data. Probably overkill, but hey, I'm trying to learn this stuff.

Link to comment
Share on other sites

If you used the correct if($_SERVER['REQUEST_METHOD'] == 'POST') you would not have had that problem in the first place. There are other threads in this forum that discuss why not to do it any other way.

 

Depending on the submit button will completely fail in certain circumstances with Internet Explorer.

Edited by benanamen
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.