wulfgar Posted January 4, 2007 Share Posted January 4, 2007 Hi guys, basically I'm creating a project (which was going well until now). Its a student database where users can search for students, view their details and edit their details.I'm utilizing the apache server, PHP and an access database (with the odbc drivers).Now, i require help with the section of editing a students details.When a user clicks edit, a dynamic page is shown where each field within the database is shown in a unique text box; this text box for each field contains the current value of that students field within the database.For example ::FirstName: (text box with current value)LastName: (text box with current value)etc.Now, when a user wishes to alter a field, all witch is required is for the new data to be entered into the text box and a submit button to be clicked, where a script is called to query the database, and make the correct alterations.Thus far i have it working quite well, the only problem in which arises is when a user makes multiple alterations at one time, then clicks submit. When this occurs, the only alteration which is made is the very first field (column) within the database for that student.This is the gist of the code thus far for the script which is called when a user clicks the submit button.[code]<?echo "<html><head><link href=\"stylesheet.css\" rel=\"stylesheet\" type=\"text/css\"><title>Detail Change</title></head><body>";//Here are the variables which hold the values of each field within the database before any alterations are made.$LName = $_REQUEST['lastname'];$FName = $_REQUEST['firstname'];$Add1 = $_REQUEST['address1'];$Add2 = $_REQUEST['address2'];$Ph1 = $_REQUEST['phone1'];$Ph2 = $_REQUEST['phone2'];$WrkN = $_REQUEST['workNum'];$Mob = $_REQUEST['mobNum'];$Dob = $_REQUEST['dob'];$Email = $_REQUEST['email'];$Par1 = $_REQUEST['parent1'];$Par2 = $_REQUEST['parent2'];//Here are the variables which contain the data in which the user enters into the text box's on the edit page.$LNInput = $_POST['surInput'];$FNInput = $_POST['firInput'];$Add1Input = $_POST['add1Input'];$Add2Input = $_POST['add2Input'];$PH1Input = $_POST['ph1Input'];$PH2Input = $_POST['ph2Input'];$WrkInput = $_POST['wrkNInput'];$MobInput = $_POST['mobInput'];$DobInput = $_POST['dobInput'];$EmailInput = $_POST['emailInput'];$Par1Input = $_POST['par1Input'];$Par2Input = $_POST['par2Input'];//Here is my connection to the database$conn = odbc_connect('student_db', '', '') or die('Database Initialization Failed!');//Each alternate query for changing the data within the database field to the user's entry into the text box; as you can see a composite //key has been used in each query, made up of the first and last names of the student.$chLN = "UPDATE stu_db SET L_Name = '$LNInput' WHERE F_Name = '$FName' AND L_Name = '$LName'";$chFN = "UPDATE stu_db SET F_Name = '$FNInput' WHERE F_Name = '$FName' AND L_Name = '$LName'";$chAdd1 = "UPDATE stu_db SET Address1 = '$Add1Input' WHERE F_Name = '$FName' AND L_Name = '$LName'";$chAdd2 = "UPDATE stu_db SET Address2 = '$Add2Input' WHERE F_Name = '$FName' AND L_Name = '$LName'";$chPh1 = "UPDATE stu_db SET Phone1 = '$PH1Input' WHERE F_Name = '$FName' AND L_Name = '$LName'";$chPh2 = "UPDATE stu_db SET Phone2 = '$PH2Input' WHERE F_Name = '$FName' AND L_Name = '$LName'";$chWrk = "UPDATE stu_db SET WorkNumber = '$WrkInput' WHERE F_Name = '$FName' AND L_Name = '$LName'";$chMob = "UPDATE stu_db SET Mobile = '$MobInput' WHERE F_Name = '$FName' AND L_Name = '$LName'";$chDob = "UPDATE stu_db SET DOB = '$DobInput' WHERE F_Name = '$FName' AND L_Name = '$LName'";$chEmail = "UPDATE stu_db SET Email = '$EmailInput' WHERE F_Name = '$FName' AND L_Name = '$LName'";$chPar1 = "UPDATE stu_db SET Parent1 = '$Par1Input' WHERE F_Name = '$FName' AND L_Name = '$LName'";$chPar2 = "UPDATE stu_db SET Parent2 = '$Par2Input' WHERE F_Name = '$FName' AND L_Name = '$LName'";//Here is an example of the code i am using in order to change the fields within the database with the data in which the user is entering //into the text boxes. (Note: Only two different fields have been used, whereas in the finished code there will be more).if ($LName != $LNInput){ odbc_exec($conn,$chLN); echo "change made"; odbc_close($conn);}if ($FName != $FNInput){ odbc_exec($conn,$chFN); echo "change made"; odbc_close($conn);}//Some buttons for user friendliness.echo "<table border='0' cellspacing='0' cellpadding='0'> <tr> <td><input type='button' class='button' value='Search' onclick='history.go(-3); window.reload();'></td> <td><input type='button' class='button' value='Back' onclick='history.back();'></td> </tr> </table>"; echo "</body></html>";?>[/code]Now i can obviously see that the IF Statements are only propagating once when the user clicks submit, meaning that only one field is being altered and other changes made are not being analyzed; i think i would have to use some sort of looping structure.. i just cant seem to figure it out :-[I appreciate any help, and if you have further questions about the code or any other code associated with this script please dont hesitate to ask..ThanksJames. Link to comment https://forums.phpfreaks.com/topic/32817-help-changing-multiple-fields-in-a-database-using-php/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.