Welcome! Log In Create A New Profile

Advanced

Extracting Data from database back to the 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
Extracting Data from database back to the form
September 26, 2012 09:29PM
Hi all!

I am struggling to extract data from database back to the form, can you help me please
avatar
Mac
Re: Extracting Data from database back to the form
September 27, 2012 07:53AM
Well, what have you tried? Post some code, not all.
Re: Extracting Data from database back to the form
September 27, 2012 05:02PM
Dear Dumi
this is just tricky but easy.
in your list page where the action is, the link that goes to the edit page set the id equal to variable id and use & sign with update and set to yes.
then within your edit page use a GET Method to catch the id and then use the id to extract the information from database to the form.

good luck
avatar
Mac
Re: Extracting Data from database back to the form
September 28, 2012 08:16AM
Yip, as per the tut letter, but if you have specific problems then post some code here.....
avatar Re: Extracting Data from database back to the form
September 28, 2012 08:56AM
38701162 - Dumi
I think you need to have something like the code below to fetch the data from the database into your form, just an example
to show you how to get the value.

Language: PHP
$query= ';select * from users where user_id = '; . $user_id; $results = mysql_query($query); $row = mysql_fetch_array($results); echo ';<input type="text" name="textBoxName" value =" ';. $row[';first_name';].'; " >';;
Re: Extracting Data from database back to the form
September 28, 2012 12:13PM
Hi mac should each and every user that logs in see the entire database or should the user see that data that he/she Inserted.
avatar
Mac
Re: Extracting Data from database back to the form
October 01, 2012 07:49AM
Remember to ask this question in general - not just of me smiling smiley That is part of what this forum is about - mutual support between students.

That you can decide.
Re: Extracting Data from database back to the form
October 01, 2012 09:17AM
Okay thanks
Re: Extracting Data from database back to the form
October 02, 2012 08:09AM
Please assist, I receive this error massage when I try to connect to the database with my connect.php page:

“Query failed: Access denied for user 'apache'@'localhost' (using password: NO)”;
Re: Extracting Data from database back to the form
October 02, 2012 08:17AM
could anyone please clarify this massage from free hosting area?

mySQL HostName is localhost only. We don't allow external connections.

// I tried the loopback (127.0.0.1) for $Hostname in my connect.php page, it gave me the same error massage when i try to log-in to my site:

“Query failed: Access denied for user 'apache'@'localhost' (using password: NO)”;
avatar
Mac
Re: Extracting Data from database back to the form
October 02, 2012 04:53PM
I will not answer these last two questions. It has NOTHING to do with the current topic. Play by the rules.
Re: Extracting Data from database back to the form
October 03, 2012 02:02PM
sorry about that, i will follow the rules

bye
Re: Extracting Data from database back to the form
October 05, 2012 05:49AM
Hi

Thanks for help, but there is an error message I am getting like this "mysql_fetch_array() expects parameter 1 to be resource, Boolean given", what could be reason? Here is my code

$query= 'select * from top_employees where employee_number ='. $employee_number;
$results = mysql_query($query);
$row = mysql_fetch_array($results);

If I remove the 'WHERE' part I can manage to get data into the form.

Thanks
avatar
Mac
Re: Extracting Data from database back to the form
October 05, 2012 07:50AM
Why is that that with EVERY post I have to remind people of the rules of using the forum? Use the formatted code button to post code here!!!!!

The WHERE part is wrong.

Language: PHP
where employee_number =';$employee_number';;
Re: Extracting Data from database back to the form
October 05, 2012 08:03AM
Hi Dumi,

You are getting your info from your FORM, thus you must stipulate the method name ie GET or POST.

So use this
Language: PHP
WHERE employee_number = ';$_POST[employee_number]';; //(or if you used GET (';$_GET[employee_number]';))
Re: Extracting Data from database back to the form
October 05, 2012 07:29PM
Thanks I will try that. Sorry Mac I will follow rules next time.

Thanks again
Re: Extracting Data from database back to the form
October 05, 2012 10:04PM
Its now extracting thank you all. One problem is the following code that will not update whats wrong please?
Language: PHP
$sql = "UPDATE top_data_capturers SET First_Name = ';$First_Name';, Surname = ';$Surname';, Facility = ';$Facility';, Section = ';$Section'; WHERE employee_number = ';$employee_number';"; $result = mysql_query($result);

Thanks
Re: Extracting Data from database back to the form
October 06, 2012 01:04PM
Well, lets start with bottom up approach...
Language: PHP
$result = mysql_query($result);
This is wrong... It should be calling your $sql statement, like this...
Language: PHP
$result = mysql_query($sql);
Then again your $sql statement for UPDATING, are you getting the info strait from your FORM or are you 1st set variables according to your FORM data?
I prefer the shorten method, get it strait from the FORM, then your $sql statement should be something like this... O remenber your WHERE part is wrong like 1st 1, I will not repeat the WHERE part look at my suggestion above.
Language: PHP
$sql = "UPDATE top_data_capturers SET First_name = \"$_POST[First_name]\", Surname = \"$_POST[Surname]\",...";
Hope this helps.

O Mac if you reading this permit me to break the rule a bit (I'll still post in proper thread also)...
I don't see any comments on Assign 1 next to me & I wanna go live with my Portfolio on same space, can you please comment on mine please or release it on myunisa please, Student no 46470107, THANKS ALOT Mac.
Re: Extracting Data from database back to the form
October 06, 2012 08:56PM
Thanks

I will show you a bit of the code I have and an error message I am getting.
Language: PHP
<?PHP include (';connect.php';);   $employee_number = $_GET[';employee_number';];   $sql = "update top_data_capturers set First_Name = ';$_POST[';First_Name';]';, Surname = ';$_POST[';Surname';], Facility = ';$_POST[';Facility';]';, Section = ';$_POST[';Section';]'; where employee_number = ';$employee_number';"; $result = mysql_query($sql);   mysql_close($db_handle); ?>   Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in E:\EasyPHP-5.3.3\www\test\test.php on line 6
avatar Re: Extracting Data from database back to the form
October 07, 2012 01:35PM
Hi Dumi
I believe the error comes from the use of the apostrophes in the SQL query.
Good way to test this is to echo the $sql variable before you try and run it.

Just remove the single quotes inside the POST variables like this:

Language: PHP
$sql = "update top_data_capturers set First_Name = ';$_POST[First_Name]';, Surname = ';$_POST[Surname], Facility = ';$_POST[Facility]';, Section = ';$_POST[Section]'; where employee_number = ';$employee_number';";

That should do the trick.
Re: Extracting Data from database back to the form
October 07, 2012 07:36PM
Hi All

I have been struggling with two things, 1. there is an error message regarding all other variables beside the primary that I use to draw data from table (posted below) 2. my sql update statement does not run, I have been changing it to try different things in variables by will not run (I am using GET method).
Language: PHP
$sql = "update top_data_capturers set First_Name = ';$_GET[First_Name]';, Surname = ';$_GET[Surname], Facility = ';$_GET[Facility]';, Section = ';$_GET[Section]'; where employee_number = ';$employee_number';"; $result = mysql_query($sql);   Notice: Undefined index: First_Name in E:\EasyPHP-5.3.3\www\test\test.php on line 70   Notice: Undefined index: Surname in E:\EasyPHP-5.3.3\www\test\test.php on line 70   Notice: Undefined index: Facility in E:\EasyPHP-5.3.3\www\test\test.php on line 70   Notice: Undefined index: Section in E:\EasyPHP-5.3.3\www\test\test.php on line 70
Re: Extracting Data from database back to the form
October 08, 2012 08:44PM
Hi Dumi,

Double check that your connected to database, then check & double check your variable names...
Language: PHP
$sql = "update top_data_capturers set First_Name = ';$_GET[First_Name]';,

The 1st First_Name after set should be the same spelling as in your Database, top_data_capturers table, where you are sending it to.
The second First_Name inside $_GET should be the same spelling as per your FORM you are getting it from.

I myself have also had numerous errors due to mis spelling or typing a variable name.
Hope this will be a solution for you.
Re: Extracting Data from database back to the form
October 08, 2012 09:17PM
Thanks

I have checked and double check my variables and they are the same both from the form and the table, I suspect that there is a trick that I am not getting. Can someone suggest the trick in this please, Mac what is your take on this I looks like I have coded mysql statement correctly but do not get the desired result, what is the problem.
avatar Re: Extracting Data from database back to the form
October 08, 2012 09:38PM
Hi Dumi
The error you got above is certainly to do with your form and how you post the data to where.
Please show us the code used in your form to assist you further.
Re: Extracting Data from database back to the form
October 09, 2012 05:46AM
Hi

Here is the code, note that the page is submitted to itself and I also include the update statement.

Language: PHP
<html> <head> <title>Update List</title> </head> <body> <h1>Update List</h1>   <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>

Language: PHP
$sql = "update top_data_capturers set First_Name = ';$_GET[First_Name]';, Surname = ';$_GET[Surname]';, Facility = ';$_GET[Facility]';, Section = ';$_GET[Section]'; where employee_number = ';$employee_number';"; $result = mysql_query($sql) or die ("Query failed: ". mysql_error());
Re: Extracting Data from database back to the form
October 09, 2012 06:33AM
Hi

I have decided to include the URL submitted when I click update, may it might provide more clue to my challenges.
Language: PHP
http://localhost/test/test.php?First_Name=+Sihle+&Surname=+Sishi+&Facility=+South+Beach+Clinic+&Section=AQP+&employee_number=%24employee_number&Submit=Update
avatar
Mac
Re: Extracting Data from database back to the form
October 09, 2012 07:21AM
Echo out the variables as a test before inserting. Also check the URL where the variables are appended.Echo test words e.g.

Language: PHP
if($_GET[';Submit';]==';Update';) { echo "Yes!"; //update query } else { echo "No!": }
Re: Extracting Data from database back to the form
October 09, 2012 09:52PM
Hi

Welcome back Mac and thanks. I looks like I am lost, I have been trying this for long time but I cannot get it right. This is now my code any challenges or suggestions, anyone?
Language: PHP
//check if the update button was clicked if (isset($_GET[';Submit';])) { $Submit = $_GET[';Submit';]; $First_Name = $_GET[';First_Name';]; $Surname = $_GET[';Surname';]; $Facility = $_GET[';Facility';]; $Section = $_GET[';Section';]; if(!$_GET[';Submit';]==';Update';) { echo "No!"; } else { echo "Yes!"; $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()); } }
avatar
Mac
Re: Extracting Data from database back to the form
October 10, 2012 09:03AM
If it echo's yes and the query is not inserting then the error must lie elsewhere e.g database structure.
avatar Re: Extracting Data from database back to the form
October 10, 2012 10:33AM
Also, you are not defining the $employee_number variable.

I would suggest to echo the $sql variable just after setting it and trying to run that in the query window on myPHPAdmin
Re: Extracting Data from database back to the form
October 10, 2012 11:52AM
Did you include the code to connect to the database."include ("connect.php"winking smiley. On your form you try to get the employee number value="<?PHP echo '$employee_number'; ?>"> but I dont see were you tryed to connect to the database.

Just for interest sake why do parse your data in the URL(using the get) instead of using post or cookies?
avatar
Mac
Re: Extracting Data from database back to the form
October 10, 2012 03:07PM
Metalaniac Wrote:
-------------------------------------------------------
> Also, you are not defining the $employee_number
> variable.
>
> I would suggest to echo the $sql variable just
> after setting it and trying to run that in the
> query window on myPHPAdmin

He probably defined it because he is not showing all the code, but you may just be correct. That would be a major oversight! Still thinks the db structure could be the problem e.g, type of field.
Sorry, only registered users may post in this forum.

Click here to login