Welcome! Log In Create A New Profile

Advanced

GET and POST

Posted by Mac 
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
avatar
Mac
GET and POST
September 02, 2011 12:16PM
Note that when you use a form, you can set the method as POST or GET.
Depending on what you set it as, you need to catch the form variables sent with $_POST or $_GET.
When you send information with a link (as opposed to a form) such as on p.8 of tut 102

Language: PHP
echo "<a href=edit.php?book_id=$book_id&update=yes>Edit</a>";
Then you can only catch the form variables with $_GET, as in

Language: PHP
$book_id=$_GET[';book_id';];

There is another use for $_GET that you may not know about. When the form variables are send with GET, then in the URL on the receiving page you see the exact combinations that was sent - e.g. ....edit.php?book_post=2&update=yes. For example - have a look at the URL of this topic

Language: HTML
http://osprey.unisa.ac.za/phorum/read.php?564,155263
See? 564,155263 was passed to this page (Here they did no associated it with a name as they handle it differently, but you get the point). Now you can see what you've sent was in fact sent, and therefore you know where to look for errors.

Note - GET is a security risk. Use it for testing - when you upload, change it all to POST>
Re: GET and POST
September 02, 2011 09:56PM
Thanks for this...
Anonymous User
Re: GET and POST
September 03, 2011 12:25PM
I got these problem, i think i managed to sort the database one.

From the list page: echo " href='edit.php?car_id = $car_id'>Edit";

I used the link to catch the car_id but when i try to use it in the edit page it produces these error " Notice: Undefined index: car_id in C:\Program Files\EasyPHP-5.3.3\www\edit.php on line 9".

The code looks like these:

//connection

$car_id = $_GET['car_id'];

$sql = "SELECT car_name, model, year, price FROM cars WHERE car_id = '$car_id'";
$result = mysql_query($sql);

//while loop
avatar
Mac
Re: GET and POST
September 04, 2011 06:08PM
The error undefined index has already been answered eslewhere.
Anonymous User
Re: GET and POST
September 13, 2011 09:07AM
I managed to workout the notices but i now have a problem with the GET method i used i do see the link on the address bar signaling that it has passed the value but still it does not return the values of the specified car_id and it does not display the form. here is my program


From the list page: echo " href='edit.php?car_id = $car_id'>Edit

//database connection

$car_id = $_GET['car_id'];

$sql = "SELECT car_name, model, year, price FROM cars WHERE car_id = '$car_id'";
$result = mysql_query($sql)

// while loop
{
//form
}

what surprises me is that when i use the $car_id that is on the address bar it does display the form and the corresponding row, now i get the feeling that the GET method is not functioning at all.
avatar
Mac
Re: GET and POST
September 13, 2011 10:12AM
Post your code here using formatted code please!

It appears correct, so the error is just hiding somewhere, so do tests.

Do you see in the URL on the receiving page (edit.php) that the value has been passed e.g. edit.php?car_id=12? i.e. it is the value of $car_id that is being passed
Do an echo just to test it is caught correctly
Are you sure the information is in the database? Use the num_rows function to see how many rows exist with this car_id
Language: PHP
$car_id = $_GET[';car_id';]; echo "car_id = $car_id";   $sql = "SELECT car_name, model, year, price FROM cars WHERE car_id = ';$car_id';"; $result = mysql_query($sql) $num_rows=mysql_num_rows($sql);   echo "$num_rows"; //shows how many rows were found   // while loop { //form }
Re: GET and POST
September 13, 2011 11:36AM
Thanks Mac.
Re: GET and POST
September 14, 2011 08:53AM
Yes sir everything is fine i managed to work it out i have spotted my errors they were minor once so they were frustrating me but im ok now i think im pulling through the project and im going to upload it soon and expect a feedback from you smile., thank you.
avatar
Mac
Re: GET and POST
September 14, 2011 11:02AM
MOKIRI Wrote:
-------------------------------------------------------
> Yes sir everything is fine i managed to work it
> out i have spotted my errors they were minor once
> so they were frustrating me but im ok now i think
> im pulling through the project and im going to
> upload it soon and expect a feedback from you
> smile., thank you.

That is the nature of programming - small errors that makes you believeyour whole program is wrong.
Re: GET and POST
September 15, 2011 12:22AM
Hi Mac

wrt to the above Im a little confused...I also used a link to catch the car_id and when I tried using the echo statement above to test if its caught correctly it works, but it does not print the data from the table in the form...In the TUT it gives an example which I use in my form to print the information from my database eg:
Language: PHP
<td>Manufacturer</td><td><input name="manufacturer" type="text" value="<?php echo ';$manufacturer';;?>"> </input></td
but it does not print the info, its just prints "$manufacturer"

below is my code so far

Language: PHP
<?php $car_id= $_GET[';car_id';]; $manufacturer = $_GET[';manufacturer';]; $make = $_GET[';make';]; $year = $_GET[';year';]; $price = $_GET[';price';]; $sql = "SELECT manufacturer, make, year, price FROM cars WHERE car_id = ';$car_id';"; $result = mysql_query($sql)   ?> // then i have my form <form Method="POST" ACTION="edit.php"> <table border="2" align="center"> <tr> <td>Manufacturer</td><td><input name="manufacturer" type="text" value="<?php echo ';$manufacturer';;?>"> </input></td> </tr>

Also in your code above there is a while loop, should I be using it to populate the info into my form similar to the list page?

Thanks for all the help so far
avatar
Mac
Re: GET and POST
September 15, 2011 06:54AM
That is because you are using a single quote.

Language: PHP
$manufacturer = "BMW"; value="<?php echo ';$manufacturer';;?>"> //will print $manufacturer value="<?php echo "$manufacturer";?>"> //will print BMW. String are in double quotes

See this post in the PHP certificate course
http://osprey.unisa.ac.za/phorum/read.php?506,151411,151544#msg-151544

Language: PHP
$value=';21';; //or $value="21"; or $value=21; // numeric value value="<?php echo "$value";?>"> //will print the 21 value="<?php echo ';$value';; ?>"> //will print $value
Re: GET and POST
September 15, 2011 10:48AM
Yes I have tried double quotes as well, then instead it doesn't print anything in my form. Is the method im using to catch the rest of the data in the db(manufacturer,make,year adn price) and print it in the form correct?
avatar
Mac
Re: GET and POST
September 15, 2011 11:02AM
Why are you passing the data, then selecting it from the db?

Language: PHP
$car_id= $_GET[';car_id';]; //this is OK $manufacturer = $_GET[';manufacturer';]; //you did not pass this so why put it here? $make = $_GET[';make';]; //you did not pass this so why put it here? $year = $_GET[';year';]; //you did not pass this so why put it here? $price = $_GET[';price';]; //you did not pass this so why put it here?   $sql = "SELECT manufacturer, make, year, price FROM cars WHERE car_id = ';$car_id';"; //you only passed the car_id, and use that to get the rest of the information from the db. $result = mysql_query($sql) //here you get the values of manufacturer, make, etc.

Clearly then your sql query is faulty and returns empty variables.
Re: GET and POST
September 16, 2011 12:10AM
ok I get what you saying...after a bit of research and lots of reading I have this...

Language: PHP
$car_id= $_GET[';car_id';];   $query = "SELECT manufacturer, make, year, price FROM cars WHERE car_id = ';$car_id';"; $result = mysql_query($query);   while($row = mysql_fetch_array(result)){ $manufacturer = $row[';manufacturer';]; $make = $row[';make';]; $year = $row[';year';]; $price = $row[';price';]; } //I tried putting an echo statement here to test the variable, eg the $manufacturer but it prints nothing


Language: PHP
//Then I have my form <input name="manufacturer" type="text" value="<?php echo "$manufacturer";?>"></input>
avatar
Mac
Re: GET and POST
September 16, 2011 07:05AM
Yes - that is also what the tut letter advises you to do - pass the id then get the rest of the information, insert it into the form.

The code appears correct.

Language: PHP
$car_id= $_GET[';car_id';]; //does the URL show the id e.g. edit.php?car_id=12 // if so, echo "$car_id"; to see if it is caught. // is the connection to the db working? The "or die..." part will tell you if there is a problem   mysql_connect("localhost", "admin", "1admin") or die(mysql_error()); mysql_select_db("test") or die(mysql_error());   $query = "SELECT manufacturer, make, year, price FROM cars WHERE car_id = ';$car_id';"; $result = mysql_query($query) or die mysql_error()); // another or die test   while($row = mysql_fetch_array(result)){ $manufacturer = $row[';manufacturer';]; $make = $row[';make';]; $year = $row[';year';]; $price = $row[';price';]; //and so on
Re: GET and POST
September 16, 2011 12:12PM
Yes the URL does show the id and yes I did an echo to test it and it works. The connection to the db is working because I have this:
Language: PHP
$connection = mysql_connect ("localhost","root","") or die ("Couldn';t connect to server");   $db = mysql_select_db("carsales", $connection) or die ("Couldn';t select database");
in my include ('connect.php'winking smiley; so it would have print an error already.

Am I calling and printing the variable correctly in my form?

<input name="manufacturer" type="text" value="<?php echo "$manufacturer";?>"></input>
Re: GET and POST
September 16, 2011 12:20PM
thanks a lot sir the forums really helped me a lot
Re:UPDATE
September 16, 2011 12:32PM
Hi Mac,I have a problem when it comes to updating data in the edit page,when i try to update i get this error message:
Notice: Undefined index: disc_id in C:\Program Files\EasyPHP-5.3.3\www\My portfolio\edit.php on line 37
The problem is that i know i have defined the variable and i know that i'm using the correct information from the list [ Using information from disc number [ 1 ]

] with this statement echo "<b>Using information from disc number [ $disc_id ] </b>";

HERE IS MY CODE:
$sql="UPDATE my_discs SET category='$category', artist_name='$artist_name', artist_surname='$artist_surname',album_name='$album_name',
title_track='$title_track',production_label='$pro_label',publisher='$publisher',year='$year',price='$price'
WHERE disc_id='$disc_id'";
avatar
Mac
Re: GET and POST
September 16, 2011 01:54PM
There is a topic on this.
Re: GET and POST
September 16, 2011 03:13PM
Mac did you check my last post?
Anonymous User
Re: GET and POST
September 16, 2011 03:20PM
Halalalalala mac... mac... mac... how is the super washing powder doing? is every one ok?smoking smiley
Re: GET and POST
September 17, 2011 03:21PM
Hi mac

Language: PHP
while($row = mysql_fetch_array(result)){ $manufacturer = $row[';manufacturer';]; $make = $row[';make';]; $year = $row[';year';]; $price = $row[';price';]; //I tried putting and echo here for $manufacturer but it does not print anything.
The echo in the value field of my form does not print anything in the text box of my form, please help...also will it be better to put the while loop inside the form?
Re: GET and POST
September 18, 2011 11:44PM
Hi mac

Im sorted..turns out to be just a syntax erroreye popping smiley
avatar
Mac
Re: GET and POST
September 19, 2011 06:45AM
OK good. I'd like to have the odd day off, so excuse me if I did not respond on Saturday smiling smiley

It is normally a typo.
Re: GET and POST
September 19, 2011 04:08PM
I got a problem with the edit page when i use POST it does not display the row in the form but GET seems to be working fine. BUT my major problem is with updating i don't know if i should use these thread for my comments or start a new one so i will be excused for that.

Here is my code and i theorically think it must update but it doesnt. i don't know if im wrong
Language: PHP
$car_id = $_GET[';car_id';];   if(isset($_REQUEST["update"])){       $sql = "UPDATE cars SET car_name=';$car_name';, model=';$model';, year=';$year';, price=';$price'; WHERE car_id=';$car_id';"; $results = mysql_query($sql); }   // select query display fine   //while loop{   } } ?> <form method="GET" action="edit.php"><center> Car name : <input type= "text" > Model : <input type= "text" > Year : <input type= "text" > Price : <input type= "text"> <input type="submit" name="update" value="Update"> </form>

Or am i losing the logic?
Re: GET and POST
September 19, 2011 05:22PM
Make sure you have included the hidden input element in your form. That could be the problem.
avatar
Mac
Re: GET and POST
September 20, 2011 06:51AM
Yes, good tip. Do tests

Language: PHP
//is the form method set to POST or GET? //assuming it is GET, include a hidden form element to pass the id //catch the id $car_id = $_GET[';car_id';]; //do an for yourself echo to see it is caught echo "car id = $car_id";
Re: GET and POST
September 20, 2011 08:44AM
Hey Mokiri. Your code might be right but the lines may not be in order. Remember the first thing you must see in your browser when you click the edit link is
a form with details for a specific car_id. I suggest you comment out the whole script on you edit and only leave the $_GET['car'] statement. When you run your
code you should get what you expect. To cut the story short, write a piece of code then check if it works. Its gnna be stressful if you write a very long code and
it doesnt work.
Re: GET and POST
September 20, 2011 09:37AM
thnks for the tip tatefu, but i did the code like you are suggesting, i first test if the car_id was successfully catched so i found that it was displaying the right id when i echo it like Mac suggested, then i echoed the row and it displayed the row correctly in the form, the problem i think is where to update i then qouted the update button and put my update query to update when the button is clicked but to no luck. i will try some more testing and changing the position of the update query and see what it does but thanks for the tip.
Re: GET and POST
September 20, 2011 10:49AM
Mokiri dont panic you almost there. Do this for me; make sure your form has a hidden input element. That hidden input element must have
a car_id directly form your db. In other words you have to copy the car_id with other car details but you dont display it. The car_id you copied from the db
to the form is the one you goin to use to update the db. The car_id you got from the edit link in the in the list table is there to help you copy the right car
details from the db. Only use it for that reason.

Therefore, when you click update, all the names of the input elements(from the form) must be handed to the script together with car_id in the hidden input element.
Remember you first use the $_POST['name_must_be_the_same_as_with_the_one_in_the_form'] with all the variables you gonna need in the script, in your case
you will have car_id, car_model etc. The bottom line is the car_id MUST be on the form.

Once you done with this my friend you going see how easy this thing is.

Gooodluck.
Re: GET and POST
September 20, 2011 02:44PM
Thnx man
Re: GET and POST
September 20, 2011 04:31PM
Tatefu you are the man the hidden input element was indeed the problem, thanks a loooooooooooooooot.
these phorum really helps thanks to you Mac.
Re: GET and POST
September 21, 2011 10:04AM
You welcomed!
Re: GET and POST
September 22, 2011 09:06AM
Dr Mac i hear you are saying we must use POST when we upload because GET is a security risk of which i agree, but im struggling with replacing the GET with POST on my programs. The GET method works fine but the POST one doest not work and displays nothing on the form on the edit page for me to update. So is it compulsary to use POST or i can just upload using the GET method?
avatar
Mac
Re: GET and POST
September 22, 2011 09:30AM
Use get if you want to. As long as you knwo there is a security risk.

If your FOR with GET works, then POST shoud also work - since all you are changing is method=post in the form itself, and on the receivng page $_GET is replaced by $_POST
Re: GET and POST
September 23, 2011 12:45PM
seems like everybody is doin the same code for edit and delete will this not be considerd copyingconfused smiley.....? and u gave us some examples on the tut 102 of how we must code our application if for instance most us follow the same example to code our application wont you say we copied from each other...?
avatar
Mac
Re: GET and POST
September 26, 2011 06:42AM
No, you can use my example. I know what to look for.
Re: GET and POST
September 26, 2011 01:36PM
hala..mac..am almost done but thr is this one problem...when i delete a row it deletes automatic without asking me to confirm with yes or no and i try a lot of ways to display it n i even went online to look for tutorial and seems thy r showing me how to delete streight without askin the user to confirm....i dont know how to put the statement.....i try somethin lyk this...
Language: PHP
//get the id   //select frm the database using the id   //while loop {   //get the fields using id }   if (isset($_GET[';id';])== "Yes"){   //put the delete code }
Re: GET and POST
September 26, 2011 03:57PM
first create a confirm page with yes or no, if yes then delete else back to list.php
Re: GET and POST
September 26, 2011 03:59PM
When I try to substitute GET with POST, the code doesn't give desired results.
avatar
Mac
Re: GET and POST
September 27, 2011 06:52AM
rixile Wrote:
-------------------------------------------------------
> hala..mac..am almost done but thr is this one
> problem...when i delete a row it deletes automatic
> without asking me to confirm with yes or no and i
> try a lot of ways to display it n i even went
> online to look for tutorial and seems thy r
> showing me how to delete streight without askin
> the user to confirm....i dont know how to put the
> statement.....i try somethin lyk this...
>
Well, you must figure out how to do it smiling smiley

Language: PHP
the link is send e.g. ?id=$id&delete=yes   if delete ==yes { do you want to delete? <a href=... ?delete=absolutely&id=$id>Yes <a href=...list.php>No</a> }elseif delete==absolutely { delete }
avatar
Mac
Re: GET and POST
September 27, 2011 06:53AM
Mphixifiy Wrote:
-------------------------------------------------------
> When I try to substitute GET with POST, the code
> doesn't give desired results.

????? When I start my car it won't start. Help me.

That is what you are asking me!! There could be a 100 reasons! Give samples of your code!
Re: GET and POST
September 29, 2011 11:10AM
thnx mac.....am sorted...now all is left is to upload it online...
Re: UPDATING
September 30, 2011 02:51PM
Hi Mac, i have a problem with my update page,all it does is pass the information that is updated to the addres bar.I dont know where i went wrong with this page cuz it shows that it is updating but fail to pass the updated data to the form.Here is my code:


if(isset($_GET['disc_id'])){ // IF THE ID IS SENT, GET THE INFO FROM THE DB, FILL THE FORM

$disc_id =$_GET['disc_id'];

$SQL= "SELECT * FROM my_discs WHERE disc_id='$disc_id'";
$result =mysql_query($SQL)or die("query failed:" .mysql_error());

while($rows=mysql_fetch_array($result)){

$category = $rows['category'];
$artist_name = $rows['artist_name'];
$artist_surname = $rows['artist_surname'];
Language: PHP
 
now here is the update sql:
Language: PHP
elseif($_submit[code="php"]   =$_GET[';update';]) // IF THE FORM WAS SUBMITTED WITH UPDATED INFORMATION, THEN UPDATE THE DB { $SQL="UPDATE my_discs SET category=';$category'; WHERE disc_id=';$disc_id';" ; $result=mysql_query($SQL)or die("query failed:" .mysql_error());     }

please help i have been on this page for the past two weeks.confused smiley
avatar
Mac
Re: GET and POST
October 03, 2011 07:08AM
Go and have a look a this part. Never seen such code in my life.

Language: PHP
if($_submit[code="php"] =$_GET[';update';])
Re: GET and POST
October 03, 2011 08:57AM
i have uploaded my files on the freewebhosting area and all seem to work well...but when a user enters my site he sees all the eight script on mysite so i decided to create a script called index.php and in it a put a code to refer(header: registration)etc.. a user to the registration page....is this allow or i my just live then the way the were and if allow..do i have to include this index file in my portfolio.....smileys with beer
avatar
Mac
Re: GET and POST
October 03, 2011 10:13AM
Yes, you can start with an index.php file.

You should have created a new topic on this though hot smiley
Re: DELETE
October 03, 2011 11:36AM
Hi mac,Ive tried that code for deleting which has yes nad no links,but when i run the page i get errors such as this:


Notice: Use of undefined constant delete - assumed 'delete' in C:\Program Files\EasyPHP-5.3.3\www\www\delete.php on line 44

Notice: Use of undefined constant delete - assumed 'delete' in C:\Program Files\EasyPHP-5.3.3\www\www\delete.php on line 46

Here is my code:
Language: PHP
if(isset($_GET[';disc_id';])){ $disc_id =$_GET[';disc_id';];   if (delete=="yes") { $error="do you want to delete? <a href=';delete.php';>Yes </a><a href=';list.php';>No</a></p>"; }elseif (delete=="absolutely") {   $sql= "SELECT * FROM my_discs WHERE disc_id=';$disc_id';"; $sql="DELETE FROM my_discs WHERE disc_id=';$disc_id';";//delete all from the table named my_discs $result = mysql_query($sql)or die("query failed:" .mysql_error());     }
Please help i'm only left with this page.
avatar
Mac
Re: GET and POST
October 03, 2011 12:05PM
undefined index has been dealt with already.

In any event

Language: PHP
if (delete=="yes") { //this is not valid find the mistake
Re: GET and POST
October 03, 2011 12:54PM
I know these is not the correct thread to post my comment but i did try to start a new one there seem to be a problem but here is my comment, I have send my assignment 2 but it bounced back and i got the undelivered message alert, have you changed the delivery address Dr Mac?
Sorry, only registered users may post in this forum.

Click here to login