I have access for only to a front end of a webpage.
This webpage has fields like
Name
Age
Number
For the above i have to update Number field.
Similarly there are hundreds of similar webpages with the these fields which are needed to be updated.
For example URL of this webpage is https://stackoverflow.com/id=xx
In the URL the value xx of the 'id' is unique for each webpage.
I have these values xx of the 'id' and the corresponding age which needs to be updated in the webpages iteratively and be saved.
What are ways for doing the above?
have them displayed in a single page or paginate it instead of having to view them one per page, one item per line, put the values that would be changeable in a input type=text with a corresponding submit button beside each.
well you could get url parameters by using $_POST['id'], $_GET['id'] or $_REQUEST['id'] but try to validate the values first to avoid sql injections.
if you want to get the whole url itself you could use
$_SERVER['SERVER_NAME']
$_SERVER is default variable on php it has a lot of uses just try to visit this link http://php.net/manual/en/reserved.variables.server.php.
I hope it helps. :)
Related
I have created a php site, and previously it was listing only products with defined values. I have now changed it to include an array of products for example all products WHERE id = "spotlights"
and this works great so it means I can add new products just to the database, but I still have to add the second page manually. e.g going from the product div on the main page, through to www.example.com/spotlight_1.php
Is there anyway in PHP to carry the data from my index.php e.g. the ID through to the next page? so that I can have a template product.php page, and I can use a database pull to echo the product information required.
So on index.php i click on the product with ID="1" and on the product.php page, it loads the relevant data for product 1.
I can write the php SQL/mySQL calls myself, its just the way to carry accross a value from the previous page which I dont understand
Regards
Henry
p.s.
all the IDs and things are stored in the database already as 1 to 3digit values e.g. 3 or or 93 or 254
Any advice as always is greatly appreciated
Regards
Henry
im not sure if im understading this correctly but you can pass the variable from one page to another using GET variables, or in other words, using the query string in the URL.
So, in index.php you will have links like this:
Product 1
In the second page (product.php) you can get this variable using this code:
$product_id = $_GET['p'];
And then query your database like this:
$query = "SELECT * FROM products WHERE id = '" . $product_id . "'";
$result = mysql_query($query);
Reference: http://www.w3schools.com/php/php_get.asp
Note: Be careful with the way you query your database, the previous code is only a demonstration of how to retrieve the info, but is not a secure solution. I recommend you to check PDO (http://php.net/manual/es/book.pdo.php).
You can use GET if you do not care other users to see your variables, or you can use POST to not let the users what your variables are (useful for password submissions)
One thing to mention is that if you use GET (url?key=value) you need to encode the value using PHP's utf8 enconde function, if you use POST, you don't have to worry about this.
There are a number of ways. You can put the data into session variables, you can POST the data to the 2nd page or pass the data via GET as URL parameters, or you can even save the data in a browser cookie.
Which approach you might use will likely depend on the security requirements for the data and whether you want to be able to access that data strictly via URL (in case of URL parameters).
You can either keep passing the value forward in the request scope until you no longer need it (commonly passed in the url ?var=value), or you can also use sessions if this needs to live longer than the request scope.
I'm trying to create unique urls for user's of a site I'm designing. Basically I want the url to look like this:
http://www.example.com/page.php?user_id=3
Or whatever user_id they are assigned. Basically I want user to be able to go to that url and it generates the page with the info (first name, last name, etc.) of the user from the row in the MySQL table with that user id number. I know that creating the URL would be done by $_GET but this is sort of backwards from that. I want them to be able to go to the URL and the pages gets the variables from the URL.
Sorry if this is basic, I'm having trouble searching for an answer because it always gives me the answer on how to submit a form TO the URL not FROM the URL. Thanks for all the help.
$user_id = $_GET['user_id'];
you could use this to get the variable.And then search the database for the matching id and get get all the details.
Since you want to contain all the details in you form.You could keep adding the other elements like this.
http://www.example.com/page.php?user_id=3&first_name=Max&last_name=Payne
Using $_GET[which element you want the value of]
Accessing the get variables on the page is really simple!.In your case as you want to access the user id,you can get from $_GET['user_id'] and fire the respective query to fetch the user details to populate the page.For more reference you could check here
http://php.net/manual/en/reserved.variables.get.php
I have a form which collects user info including name, location, url, email address etc etc AND company logo AND appropriate catagories they wish to be associated with (using a checkbox array in $_POST).
I then display a preview of the page (using $_SESSION and hidden form fields to hold the $_POST data) which their data will appear on BEFORE storing it in MySQL with an 'approve' button which links to the PHP page which has the MySQL INSERT query.
The problem i'm having is the preview page in between entry and submission to the database is having trouble with the logo input and the checkbox array input:
$_SESSION['clogo'] = $_POST['clogo'];
$_SESSION['cat[]'] = $_POST['cat[]'];
I get an 'undefined index' error when i try to preview these, however all other standard input is fine, and is then passed along to the insert mysql query on user approval. In addition, if i skip the 'preview page' and go straight to the insert PHP file from the original form, there is no problem, and all data is entered into the database correctly.
Can anyone help
You don't want this:
$_SESSION['cat[]'] = $_POST['cat[]'];
But this:
$_SESSION['cat'] = $_POST['cat'];
I.e., 'cat' is the index of the _POST array, and the element that it refers to is another array.
I'm guessing $_POST['cat'] is an array in itself. (I'm guessing this because of the [] following cat in your example.) If this is the case, then in your example, you're setting $_SESSION['cat[]'] equal to a variable name that doesn't exist. Try setting $_SESSION['cat'] equal to $_POST['cat'].
Remember to start the session using the start_session function. Otherwise nothing will be saved in the session.
Also worth noting is that when using some third party application they might remove all session variables. Wordpress is a good example.
$_POST[] array must be re-initialize, as you are posting to preview page and then php insert script page...
When you go directly from form to php script page ,POST have variables in it, so it must be working fine.
'cat[]' would not be the index. The index would be 'cat' which should be an array.
I am a php programmer.When a page gets loaded some records are shown from a table(in a tabular format on the php page) , on the page there are html submit buttons corresponding to whose post some other values are to be shown.
The problem is, the other values are shown along with the values which are shown when the page first loads. I want to show fresh set of values , without the default values which are shown when the page first loads.
It has to be mentioned that the default page load values which are fetched from the table is not done corresponding to any isset($_POST) unlike the other fresh values.
can you help me out?
Try using some random numbers in the query or other patterns in the query so that you get records afresh than getting the same records every time you visit the page.
Hope my understanding is right.
I have a advertisement website with alot of records stored in a mysql db.
On my main page, I have a form where users may specify their search criteria.
Action is set to myPhp.php file.
myPhp.php contains NO HTML, only php code, and it is basically something like this:
1- get values from FORM and build a SQL query
2- query MYSQL db.
3-display all results in a table using
`while($row=mysql_fetch_array($res))`
4- echo the table.
In this table which is created in PHP, I have several links also, and whenever they are clicked, a javascript function on the parent page is called, which sets a hidden input value to something, and then submits the form again with the chosen variable.
Ex: Whenever users want to go to the next page in the search results, they have to click on a 'next' link created in PHP, and then the javascript gets called, which sets a hidden input value to 'next', and then the form is submitted again, and PHP file GETS the variable from the hidden input and detects that its value is set to 'NEXT' and then displays the next results.
Is there really not another way to do all this ? (that is, a better way when it comes to performance and reliability)
Im still learning so I am very thankful for your help!
I will update this Question whenever you need more input.
Thanks
Replace the next javascript call with a link to
http://yourdomain/myPhp.php?page=2
Then check the get parameter in myPhp.php:
if(isset($_GET['page']) AND is_numeric($_GET['page'])){
$limit = (int) $_GET['page'] * MAX_RECORDS . ', ' . MAX_RECORDS;
} else {
$limit = MAX_RECORDS;
}
//...
$query .= 'LIMIT '. $limit;
Of course you have to change it as your constants change and such.
This method called paginating. Take a look at the Zend Framework's Paginator module to the better understanding.
When creating the link you can use a querystring to specify the parameter that you would like to send to the server rather than submitting a form. So if you have a hidden input in a form called next then you can accompish the same thing like this:
Next
On the myPhp.php page you can get the value of next in a similar way as when you send a form.
If these links have at least one same class across board (with a title attribute) you can attach to them to can attach all these links to a single click event and test to see what title is on the element clicked.
With that you can use a switch to trigger the right function to perform the task (I suppose using JQuery/Ajax).
This should work. At least no hidden fields.
Example:
Next
Ad Details