Link to home
Start Free TrialLog in
Avatar of arraysg2008
arraysg2008

asked on

Want to be able to Add, Edit and Search in the same form using PHP and mySQL

I created a simple form with multiple buttons for this question, what I would like to do is when I press the Add button it takes the information from the three for inputs and adds it to a mySQL database. The Add is done, I am able to hit the Add button and it connects to my database and adds the fields in, the part I'm confused about is the Edit and Search buttons.

What I want to be able to do is type in a First Name or Last name or both and be able to hit search and the record populates into the input fields in the form, from there I would like to be able to edit this data and hit the Edit button and it writes the new information into that record. I am still learning PHP and SQL so please bear with me.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
 
<body>
<form action="form.php" method="post"> 
First Name: <input type="text" name="firstname" size="15"> <br>
Last Name: <input type="text" name="lastname" size="15"> <br>
Department: <input type="text" name="department" size="15"> <br>
<input type="submit" name="user" value="Add">
<input type="submit" name="user" value="Update">
<input type="submit" name="user" value="Search">
 </form>
 
<?php
switch ($_POST['user']) {
	
	case 'Add':
	echo "Add";
	break;
	
	case 'Update':
	echo "Updated";
	break;
	
	case 'Search':
	echo "Search";
	break;
}
?>
 
</body>
</html>

Open in new window

Avatar of CoyotesIT
CoyotesIT

Your code looks fine, the issue I see you having with this is obviously if you search for John, it may return several, and you are only populating one. I would recommend that you add a table into the mix for your search, and set a column to the record id, when clicked the $_POST["id"] will contain the record ID you need to select the values out for your form. Once you click the edit button your case statement should be fine by just UPDATE table SET col1 = 'value1', col2='value2' where id = $_POST["id"];

The other way I could see you accomplishing this without the table is by getting crafty with some javascript, paging through your results with a forward/backward type control given multiple results.


ASKER CERTIFIED SOLUTION
Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of arraysg2008

ASKER

This is perfect, thank you