Undefined variable php - php

i have 2 files index.php and download.php
index.php :
<a href='download.php?id=$id'>Get file</a>
download.php :
<?php
if($id){
getting info from db
}
?>
and I get this error :
Notice: Undefined variable: id in download.php on line 2
May you help me ?

You need $id = isset( $_GET['id']) ? intval( $_GET['id']) : 0; at the top of your download script.

<?php
if(isset($_GET['id']))
{
$id = intval($_GET['id']);
// getting info from db
}
?>

In your download.php change the if statement
if(!empty($_GET['id'])) {
$id = $_GET['id'];
}

The question is what is the value of your $id on the download.php?
on download.php it should be like this:
<?php
$id = $_GET["id"];
if($id){ getting info from db }
?>

<?php
$id = $_GET['ID'];
if($id)
{
getting info from db
}
?>

try this:
index.php :
'>Get file
download.php :
<?php
$id = $_GET['id'];
if($id){ getting info from db }
?>

try if ((isset ($_GET ['id'])) && ($id = intval ($_GET ['id']))) instead.

Related

Get parameter, passed via the link in PHP

I get variable from http://localhost/match?id=1 via code:
<?
if (isset($_POST['id'])) {
$id = $_POST['id'];
$id = secure($id);
} else {
echo "error";
die();
}
And I get the error from my else statement. How to get the parameter, passed via the link?
Try this code:
<?
if (isset($_GET['id'])) {
$id = $_GET['id'];
$id = secure($id);
} else {
echo "error";
die();
}
Params, passed throught the link, are accessable trough the $_GET superglobal.
Info about $_GET on php.net.
Some explanations about $_GET vs $_POST on w3schools.com.
use POST if you get data from form. and GET if you get data by link. in your case it is link
if (isset($_POST['id'])) { ** this POST should be GET because you have http://localhost/match?id=1

Session variable cannot be passed into query

page1.php has this code
<?php
session_start();
$_SESSION['supp_id'] = 4;
?>
page2.php has this code
<?php
session_start();
include "db2.php";
$supp_id = $_SESSION['supp_id'];
var_dump($supp_id);
if(isset($_POST['accept']))
{
$cust_id = $_POST['cust_id'];
$cartype = $_POST['cartype'];
$q=mysqli_query($conn,"INSERT INTO `availablesupp` (`Cust_ID`,`Supp_ID`,`CarType`) VALUES ('$cust_id','$supp_id','$cartype')");
if($q)
echo "ok";
else
echo "error";
}
?>
$supp_id did not get passed into the query, and the database is updated with Supp_ID = 0;
However changing $supp_id = $_SESSION['supp_id'] to $supp_id = 4 works fine. var_dump produced identical results in both cases, int(4).
How do I solve this?

Using Sessions or Cookies with a Get Value

I get a field value using GET from a URL.
www.test.com?id=12345
I create a session using assigning the id to a variable.
$id= $_GET['id'];
session_start();
$_SESSION['mainid'] = $id;
if(isset($_SESSION['mainid'])) {
echo "Your session is running " . $_SESSION['mainid']; }
I then use echo session on other pages
if(isset($_SESSION['mainid'])) {
echo "Your session is running " . $_SESSION['mainid']; }
The problem is when i go back to the home page, it tries to look for the get value $id, and it is blank now as the URL doesn't contain the Feild value id=12345. How do i get around this ?
if(!isset($_SESSION)){
session_start();
}
if(!isset($_SESSION['mainid'])){
$id= $_GET['id'];
$_SESSION['mainid'] = $id;
} elseif(isset($_GET['id']) && isset($_SESSION['mainid'])) {
if($_GET['id'] != $_SESSION['mainid'] )
{
$_SESSION['mainid'] = $_GET['id'];
}
}
Well, you should rewrite your code for sure to check if the variable exists first and then start the session.
Something like this should do.
$id = isset($_GET['id']) ? $_GET['id'] : false;
if ($id) {
session_start();
$_SESSION['mainid'] = $id;
}
Just put your below code
$id= $_GET['id'];
session_start();
$_SESSION['mainid'] = $id;
inside isset
Your code should look something like below code:
if(isset($_GET['id'])){
$id= $_GET['id'];
session_start();
$_SESSION['mainid'] = $id;
}

Add and edit in one form php

I have two files .. addclient.php & Editclient.php.
I want to combine in one php form. can u please help me to do this
<?php
if(isset($_GET['id'])) {
$id = $_GET['id'];
echo "add";
} else if(isset($_GET['id'])) {
$id = $_GET['id'];
echo "edit"
}
?>
if $id already exist in the table than the row is updated otherwise it's an INSERT.
If you only have an $id : show the form with existing data in it.
If it's not a $id isn't populated : show an empty form.
<?php
if(isset($_GET['id'])) {
$id = intval($_GET['id']);
$stmt = $mysqli->prepare("SELECT id FROM table WHERE id = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
if($stmt->num_rows > 0) {
// UPDATE
// Text field with the id
echo '<input type="text" name="id" value="'. $id. '"/>';
} else {
// INSERT
// Text field with no id
echo '<input type="text" name="id"/>';
}
}
?>
This validates the integer and makes sure it's one, queries the table for that specific id, if there is more than one row with that id then you need to update, else you need to insert.
<?php
if(isset($_GET['ID'])) {
$ID= $_GET['ID'];
// Use MySQL with $ID to check if the user is existing, and this string will either be 0 or 1
$existing = 0; // 0 = New 1 = Existing
//Add
if($existing== 0) {
}
//Edit
if($existing == 1) {
}
}
?>
Try this
<?php
if(isset($_GET['id'])) {
$id = $_GET['id'];
echo "update";
}else{
$id = $_GET['id'];
echo "add"
}
?>
Can you try this,
<?php
if(isset($_GET['id'])) {
$id = $_GET['id'];
echo "Edit";
} else {
echo "Add"
}
?>
try this
if(isset($id) && !empty($id)){
echo "Update";
}else{
echo "Add";
}

PregMach with php GET method

I have a page test.php in which I have a list of names:
name1='992345'
name2='332345'
name3='558645'
name4='434544'
In another page test1.php?id=name2 and the result should be:
332345
I've tried this PHP code:
<?php
$Text=file_get_contents("test.php")
$id = $_GET["id"];
;preg_match_all('/$id.=\'([^\']+)\'/',$Text,$Match);
$fid=$Match[1][0];
echo $fid; ?>
if the code is used like this
<?php
$Text=file_get_contents("test.php")
;preg_match_all('/name3=\'([^\']+)\'/',$Text,$Match);
$fid=$Match[1][0];
echo $fid; ?>
the result is good
558645
But i wanto be able to change the name of '/name3=\' from ;preg_match_all('/name3=\'([^\']+)\'/',$Text,$Match) with GET method like this if test1.php?id=name4 the result wil bee 434544.
In '(single quote) the variable is treated as a text not as variable.
change this
preg_match_all('/$id.=\'([^\']+)\'/',$Text,$Match);
to
preg_match_all("/$id=\'([^\']+)\'/",$Text,$Match);
Try this code:
<?php
$Text=file_get_contents("test.php");
$id = $_GET["id"];
$regex = "/".$id."=\'([^\']+)\'/";
preg_match_all($regex,$Text,$Match);
$fid=$Match[1][0];
echo $fid; ?>
This will work.
EDIT:
<?php
$Text=file_get_contents("test.php");
if(isset($_GET["id"])){
$id = $_GET["id"];
$regex = "/".$id."=\'([^\']+)\'/";
preg_match_all($regex,$Text,$Match);
$fid=$Match[1][0];
echo $fid;
} else {
echo "There is no name selected.";
}
?>

Categories