Welcome! Log In Create A New Profile

Advanced

Updating table using form

Posted by 38701162 - Dumi 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Updating table using form
October 08, 2012 09:26PM
Hi

Guys please help me I cannot update my table using the form, where is the trick? Please

Thanks
avatar
Mac
Re: Updating table using form
October 09, 2012 07:22AM
Please help me, my car won't start.... now on on earth can I be helped based on the information I provided????
Re: Updating table using form
October 10, 2012 12:18PM
Give us more info Dumi. When trying to update your user information you should ask yourself the following question. Is my user logged on? Did I save the user user_id?. After having the user id run a SELECT SQL query to get the user information that he/she entered. When the update button is clicked get all changes made and replace the previous infomation using the UPDATE query.
Re: Updating table using form
October 10, 2012 10:13PM
Hi

I can see that I am not doing myself a favour, here is the full code for you to assist me please.
Language: PHP
<?PHP include (';connect.php';); //connecting to database ?>   <html> <head> <title>Update List</title>   </head> <body> <h1>Update List</h1> <?PHP $employee_number = $_GET[';employee_number';]; //catching primary key   $openTab = "<table>"; $closeTab = "</table>"; $openRow = "<tr>"; $closeRow = "</tr>"; $openTbData = "<td>"; $closeTbData = "</td>";   //retrieving data from database back to the form $query= "select * from top_data_capturers where employee_number = ';$employee_number';"; $results = mysql_query($query); $row = mysql_fetch_array($results);   echo $openTab; echo $openRow; echo $openTbData; echo ';Name: ';; echo $closeTbData; echo $openTbData; echo ';<input type="text" name="First_Name" value =" ';. $row[';First_Name';].'; " ><br>';; echo $closeTbData; echo $closeRow; echo $openRow; echo $openTbData; echo ';Surname: ';; echo $closeTbData; echo $openTbData; echo ';<input type="text" name="Surname" value =" ';. $row[';Surname';].'; " ><br>';; echo $closeTbData; echo $closeRow; echo $openRow; echo $openTbData; echo ';Facility: ';; echo $closeTbData; echo $openTbData; echo ';<input type="text" name="Facility" value =" ';. $row[';Facility';].'; " ><br>';; echo $closeTbData; echo $closeRow; echo $openRow; echo $openTbData; echo ';Section: ';; echo $closeTbData; echo $openTbData; echo ';<input type="text" name="Section" value =" ';. $row[';Section';].'; " >';; echo $closeTbData; echo $closeRow; echo $closeTab;   //check if the update button was clicked if(isset($_GET[';Submit';])){ $Submit=$_GET[';Submit';]; if(!$_GET[';Submit';]==';Update';) { echo "Click update!"; } else { $query="update top_data_capturers set First_Name=';$First_Name'; where employee_number=';employee_number';"; mysql_query($query) or die("Cannot update"); } }   mysql_close($db_handle); ?> <form action="test.php" Method="get"> <input type="hidden" name="employee_number" value="<?PHP echo ';$employee_number';; ?>"> <input type="Submit" name="Submit" value="Update" /> </form> <a href=';read1.php'; >Return to List</a> </body> </html>

The fields to update are First_Name, Surname, Facility and Section, these are varchar data type.
avatar
Mac
Re: Updating table using form
October 11, 2012 08:04AM
Why define HTML tags as variables - just type it directly? Wasteful practice as it is.

Well, I am not going to retype your code - it is not my portfolio. But the form opening tag must be right on top, else the information is not sent.

Language: PHP
//catch employee number as send from previous page   if submit==update //catch form data sent //update statement else //<form> opening tag //get data from db using employee number //show data in form //hidden employee number as send from previous page //</form> closing tag
Re: Updating table using form
October 11, 2012 02:29PM
1.
Dumi you did not initialize $First_Name and employee_number is does not have the dollar sign.
Language: PHP
$query="update top_data_capturers set First_Name=';$First_Name'; where employee_number=';employee_number';";
try this to test you code
$First_Name = "Dumi.com"
Language: PHP
$query="update top_data_capturers set First_Name=';$First_Name'; where employee_number=';$employee_number';";
2.
did not execute the query. Add this to your code
Language: PHP
$result = mysql_query($query);
avatar
Mac
Re: Updating table using form
October 11, 2012 03:13PM
Well spotted the missing $. I fear that is not the only reason though, hence the logic suggested. But yes, more often than not is not spotting these small typos that drives one mad. Which is why I stress doing echo's of everything. One can also echo the $query.
avatar Re: Updating table using form
October 12, 2012 10:27PM
The big problem here is the form-tags placement. You are alot of input fields that do not fall in between the form tags, thus, will not get send with the form information.
Move the opening form tag up to just below the <h1> tag. That should do the trick.
Re: Updating table using form
October 13, 2012 09:05PM
Hi

The form tag seem to be in the place you saying, I am sorry but please keep trying brother I might get it at the end. Its just that I changed too much up to confusion. You can still look at the code especially opening <form> tag.
Language: PHP
<html> <head> <title>Update List</title>   </head> <body> <h1>Update List</h1> <form action="update.php" Method="get"> <input type="hidden" name="employee_number" value="<?PHP echo ';$employee_number';; ?>"> <input type="Submit" name="Submit" value="Update" /> </form> <a href=';read1.php'; >Return to List</a> </body> </html>   <?PHP include (';connect.php';);   $employee_number = $_GET[';employee_number';];   //retrieving data from database back to the form $query= "select * from top_data_capturers where employee_number = ';$employee_number';"; $results = mysql_query($query); $row = mysql_fetch_array($results);   echo ';<br>';. ';Name: ';. ';<input type="text" name="First_Name" value =" ';. $row[';First_Name';].'; " ><br>';; echo ';Surname: ';. ';<input type="text" name="Surname" value =" ';. $row[';Surname';].'; " ><br>';; echo ';Facility: ';. ';<input type="text" name="Facility" value =" ';. $row[';Facility';].'; " ><br>';; echo ';Section: ';. ';<input type="text" name="Section" value =" ';. $row[';Section';].'; " >';;   if (isset($_GET[';$Submit';])) { if ($Submit == ';yes';) { $First_Name = $_GET[';First_Name';]; $Surname = $_GET[';Surname';]; $Facility = $_GET[';Facility';]; $Section = $_GET[';Section';];   $sql = "update top_data_capturers set First_Name = ';$First_Name';, Surname = ';$Surname';, Facility = ';$Facility';, Section = ';$Section'; where employee_number = ';$employee_number';"; $result = mysql_query($sql); } }   mysql_close($db_handle);   ?>
Re: Updating table using form
October 15, 2012 02:18PM
try to put the or die() method on the mysql_query() method so that you can see if the query to running.
Re: Updating table using form
October 16, 2012 09:12PM
Hi everyone

I'm sorry but I don't have a choice but to ask for help. My code now has one error ("Submit undefined"winking smiley and printed ("could not update"winking smiley, it looks like stamen ts between if statement do not execute and therefore could not update. How to solve this?

Language: PHP
<?PHP include (';connect.php';);   $employee_number = $_GET[';employee_number';];   //retrieving data from database back to the form $query= "select * from top_data_capturers where employee_number = ';$employee_number';"; $results = mysql_query($query) or die("Query failed: ". mysql_error());   while ($row = mysql_fetch_array($results)) { $employee_number = $row[';employee_number';]; echo "<table border=';1';>"; echo "<colgroup span=';2'; width=';120';>"; echo "<tr>"; echo "<th>Employee_Number</th><th><input type=';text'; name=';employee_number'; value=';$row[employee_number]'; ></tr>"; echo "<tr><th>First_Name</ht><th><input type=';text'; name=';First_Name'; value=';$row[First_Name]'; ></tr>"; echo "</tr><th>Surname</th><th><input type=';text'; name=';Surname'; value=';$row[Surname]'; ></tr>"; echo "<tr><th>Facility</th><th><input type=';text'; name=';Facility'; value=';$row[Facility]'; ></tr>"; echo "<tr><th>Section</th><th><input type=';text'; name=';Section'; value=';$row[Section]'; ></td>"; echo "</tr>"; }   echo "</table>";   $employee_number = $_GET[';employee_number';]; if ($Submit == ';yes';) { $First_Name = $_GET[';First_Name';]; $Surname = $_GET[';Surname';]; $Facility = $_GET[';Facility';]; $Section = $_GET[';Section';];     $sql = "update top_data_capturers set First_Name = ';$First_Name';, Surname = ';$Surname';, Facility = ';$Facility';, Section = ';$Section'; where employee_number = ';$employee_number';"; $result = mysql_query($sql) or die("Query failed: ". mysql_error());   echo "Database updated successfully!"; } else { echo "Could not update!"; }   mysql_close($db_handle);   ?>   <html> <head> <title>Update List</title>   </head> <body> <form method="get" action="edit.php."> <input type="hidden" name="employee_number" value="<?PHP echo ';$employee_number';; ?>"> <fieldset> <input type="Submit" name="Submit" value="Update" /> <input type="hidden" name="submitted" value="TRUE" /> </fieldset> </form> <a href=';read1.php'; >Return to List</a> </body> </html>
Re: Updating table using form
October 18, 2012 10:01AM
1.Put the tag for opening the table outside the while loop.
2.Put the html code of top.
3. On this line
Language: PHP
if ($Submit == ';yes';)
You did not define $Submit.
It is better using this code to test the if the button is closed.
Language: PHP
if (isset($_GET[';submit';])
Re: Updating table using form
October 23, 2012 05:38PM
Hi

I like to thank everyone, my update code is not workingspinning smiley sticking its tongue out You all wonderful

Thanks again!
Sorry, only registered users may post in this forum.

Click here to login