Get the id from the URL; PHP, MySQL - php

Hi i've been curious on how to get the id in the url like php?pid=4 and use it in an update statement in sql. Well heres my code but cant get it worked because of undefined variable id which the value is in the url.
my function.php
function update_spot(){
$id=$_GET[pid];
if (isset($_POST['update'])){
$sql="UPDATE reports SET date_time_started='$_POST[date1]' ,
date_time_finished= '$_POST[date2]',
barangay='$_POST[brgy]',
street= '$_POST[street]',
owner='$_POST[owner]',
cause='$_POST[cause]',
motive='$_POST[motive]',
firfighter='$_POST[firefighter]'.
civilian='$_POST[civilian]',
ifirefighter='$_POST[ifirefighter]',
icivilian='$_POST[icivilian]',
occupancy='$_POST[occupancy]',
ed='$_POST[ed]',
alarm='$_POST[alarm]'
where id='".$id."' ";
if (!mysql_query($sql)){ die('Error: ' . mysql_error()); } ?>
<script type='text/javascript'>alert('sucessful changed try it next time you log
in.');window.location='view_inbox.php';</script> <?php
}
}
it seems i cant get id in the url. my url show like this in the form php?pid=5

It should simply be
$id = $_GET['pid'];

Use Quotes on your $_GET array access
$pid = $_GET['pid'];
Also, you are mixing $_GET and $_POST. You should use one or the other, depending on your form's method (GET or POST).
You also want to change all the areas you access it. IE
barangay = '$_POST[brgy]';
This should be, and all other lines after it
barangay = $_POST['brgy'];

$pid = isset($_GET['pid']) ? 0 : intval($_GET['pid']) //to avoid problems in this case

#Up, in the example above it still should work, but will cause php warning saying that undefined php constant will be assumed as string.
I would dump get global and see if variable is there, eg var_dump($_GET);

Related

Make a $_GET into variable or place into URL

How do you place a $_GET['****']; into a string or make it into a variable.
For Example i have this url:
http://localhost/PhpProject2/product_page.php?rest_id=3/area=Enfield.
I want to get the area and rest_id from the url. in order to redirect another page to this exact page.
echo"<script>window.open('product_page.php?rest_id= 'put get here'/area='put get here'','_self')</script>";
I have so far done this:
if(isset($_GET['rest_id'])){
if(isset($_GET['rest_city'])){
$_GET['rest_id'] = $rest_id;
}
}
This obviously does not work, so my question is how do i make the 2 $_GET into a variable or call the $_GET into the re-direct string.
What i have tired so far
echo"<script>window.open('product_page.php?rest_id=' . $GET['rest_id'] . '/area='put get here'','_self')</script>";
How or what is the best practice?
ok, first things first. in your URL you have to separate the parameters using an ampersand "&", like this
http://localhost/PhpProject2/product_page.php?rest_id=3&area=Enfield
Also, you have to assign the $_GET value to a variable, not the other way around, like this
$rest_id = $_GET['rest_id'];
so if you create a PHP file named product_page.php and use the url i gave you, and your PHP code looks like this, it should work..
<?php
if (isset($_GET['rest_id'])){
$rest_id = $_GET['rest_id'];
}
if (isset($_GET['rest_id'])){
$area = $_GET['area'];
}
$url = 'other_page.php?rest_id=' . $rest_id . '&area=' . $area;
header("Location: $url");
?>
The question here is why do you want to redirect from this page to the other, and not send the parameters directly to the "other_page.php"????

Data wasn't able to retrieve from the previous page (PHP and MYSQL)

I researched here in stackoverflow trying to find whether someone is also encountering the same problem. I know it's kind of easy and even I really don't know what's the error because there's no problem with my query.
On the previous page, here's my code to retrieve the ID Number so I'll be able to select the data with that ID number:
<?php echo $row['place_name'];?>
I tried first to print the value of the place id and it works fine.
But when it was being called to the Package page, the data I want to show weren't displayed.
I look at the URL and it shows this after the package.php
place_id=
I don't know why it is blank, please check my code if there's missing or just wrong.
In my package page, here's the PHP code:
<?php
include("common/connect.php");
$place_id = $_GET['place_id'];
$result = mysql_query("SELECT * FROM package_items WHERE place_id = '$place_id'");
$row1 = mysql_fetch_array(mysql_query("SELECT place_name FROM packages WHERE place_id = '$place_id'"));
if($result === FALSE) {
die(mysql_error()); // for better error handling
}
?>
In HTML Code:
<h1><?php echo $row1['place_name'];?></h1>
<?php while($row=mysql_fetch_array($result)) {?>
<?php echo $row['item_title'];?>
<br>
Back
<?php } ?>
Please check my codes. Thanks.
You are not printing it.
Change
<?php $row['place_id'];?> // It will output nothing as no echo or print.
To
<?php echo $row['place_id'];?>
Rest of the code looks fine.
Three suggestions:
1)
$place_id = $_GET['place_id'];
Change to
$place_id = ! empty($_GET['place_id']) ? $_GET['place_id'] : ''; // To avoid any warning.
2) Don't feed variable from $_GET or $_POST to any SQL.
3) Don't use mysql_ functions as they are deprecated and will be remove in future versions of PHP.

Get content after question mark in PHP

I am getting a request like this and the url looks like this : www.site.com/test.php?id=4566500
Now am trying to get the id number to make the code in test page work, is there a way to do this?
<?php
echo("$id"+500);
?>
You can access these values via the $_GET array:
<?php
echo($_GET['id'] + 500);
?>
This is basic PHP. You want to use the $_GET superglobal:
echo $_GET['id'] + 500;
Do not forget to check the right setting of your Getter parameter:
if (isset($_GET['id']) && preg_match("\d+", $_GET['id'])) {
// do something with $_GET['id']
} else {
// appropriate error handling
}
Remember that anyone can set the id parameter to any value (which can lead to possible XSS attacks).
You cannot access direct url parameter without using predefined PHP super global variable like $_GET["$parameter"] OR $_REQUEST["$parameter"].
So for : www.site.com/test.php?id=4566500
<?php
$id = (int)$_GET['id']; // Or $_REQUST['id'];
if(is_numeric($id)){
echo $id + 500;
}else{
echo $id;
}
?>
For more detail :
PHP $_GET Reference
PHP $_REQUEST Reference

Undefined index: initial in C:\wamp\www\ralph\search.php on line 9...

Below is my code for a search in php. I'm having a problem to run this program. so please help me?
<html>
<body>
<?php
mysql_connect("localhost","root","");
mysql_select_db("login");
$initial = mysql_real_escape_string($_POST['initial']);
$find_videos = mysql_query("SELECT * FROM `sigup` WHERE `initial` LIKE '%initial%'");
while($row = mysql_fetch_assoc($find_videos))
{
$initial = $row['initial'];
echo "$initial<br/ >";
}
?>
</body>
</html>
Your problem is on this line:
$initial = mysql_real_escape_string($_POST['initial']);
Basically, the issue is caused whenever the POST variable initial does not exist. When the POST variable initial does not exist, the index initial will not exist in the $_POST array.
You should always check to see if a POST or GET variable exists before you attempt to use it. You can do this by using the function isset:
if(isset($_POST['initial'])){
//initial exists as a POST variable
} else{
//initial does not exist as a POST variable
}
Of course, you could also use this in ternary form:
$initial = isset($_POST['initial']) ? mysql_real_escape_string($_POST['initial']) : false;
if($initial !== false){
//Good to go
}
Some causes of POST variables not existing when you think they should:
Form element names not matching up with the POST variables that you're trying to access. Ex: Your field is called name but you're trying to access the POST variable names.
A user accessing the page directly, instead of accessing it via a form.

HREF to call a PHP function and pass a variable?

Is it possible to create an HREF link that calls a PHP function and passes a variable along with it?
<?php
function sample(){
foreach ($json_output->object ){
$name = "{$object->title}";
$id = "{$object->id}";
print "<a href='search($id)' >$name</a>";
}
}
function search($id){
//run a search via the id provide by the clicking of that particular name link
}
?>
You can do this easily without using a framework. By default, anything that comes after a ? in a URL is a GET variable.
So for example, www.google.com/search.html?term=blah
Would go to www.google.com/search.html, and would pass the GET variable "term" with the value "blah".
Multiple variables can be separated with a &
So for example, www.google.com/search.html?term=blah&term2=cool
The GET method is independent of PHP, and is part of the HTTP specification.
PHP handles GET requests easily by automatically creating the superglobal variable $_GET[], where each array index is a GET variable name and the value of the array index is the value of the variable.
Here is some demo code to show how this works:
<?php
//check if the get variable exists
if (isset($_GET['search']))
{
search($_GET['search']);
}
function Search($res)
{
//real search code goes here
echo $res;
}
?>
Search
which will print out 15 because it is the value of search and my search dummy function just prints out any result it gets
The HTML output needs to look like
anchor text
Your function will need to output this information within that format.
No, you cannot do it directly. You can only link to a URL.
In this case, you can pass the function name and parameter in the query string and then handle it in PHP as shown below:
print "<a href='yourphpscript.php?fn=search&id=$id' >$name</a>";
And, in the PHP code :
if ($_GET['fn'] == "search")
if (!empty($_GET['id']))
search($id);
Make sure that you sanitize the GET parameters.
No, at least not directly.
You can link to a URL
You can include data in the query string of that URL (<a href="myProgram.php?foo=bar">)
That URL can be handled by a PHP program
That PHP program can call a function as the only thing it does
You can pass data from $_GET['foo'] to that function
Yes, you can do it. Example:
From your view:
<p>Edit
Where 1 is a parameter you want to send. It can be a data taken from an object too.
From your controller:
function test($id){
#code...
}
Simply do this
<?php
function sample(){
foreach ($json_output->object ){
$name = "{$object->title}";
$id = "{$object->id}";
print "<a href='?search=" . $id . "' > " . $name . "</a>";
}
}
if (isset($_REQUEST['search'])) {
search($_REQUEST['search']);
}
function search($id){
//run a search via the id provide by the clicking of that particular name link
}
?>
Also make sure that your $json_output is accessible with is the sample() function. You can do it either way
<?php
function sample(){
global $json_output;
// rest of the code
}
?>
or
<?php
function sample($json_output){
// rest of the code
}
?>
Set query string in your link's href with the value and access it with $_GET or $_REQUEST
<?php
if ( isset($_REQUEST['search']) ) {
search( $_REQUEST['search'] );
}
function Search($res) {
// search here
}
echo "<a href='?search='" . $id . "'>" . $name . "</a>";
?>
Yes, this is possible, but you need an MVC type structure, and .htaccess URL rewriting turned on as well.
Here's some reading material to get you started in understanding what MVC is all about.
http://www.phpro.org/tutorials/Model-View-Controller-MVC.html
And if you want to choose a sweet framework, instead of reinventing the MVC wheel, I highly suggest, LARAVEL 4

Categories