Jump to content

Get value and store as variable


elite311

Recommended Posts

Hi I'm hoping someone can help me with this, I'm trying to get a value from my sql query and store it as variable so I can use it in the url but I'm not quite sure how to do it.

 

Right now when I click the edit button I set the id to the asset number in the url (ie edituseloc.php?id=H802) I use this to query the info related to that asset number. When I save the changes I want to redirect back to a page where the asset is located (ie job number locationinfo.php?id=215) the problem is I have to have the job number passed back to the url when it redirects and I'm not sure how to get that value into the header line with the "updated=1"

 

Right now when I run this all I get in the header is locationinfo.php?id=&updated=1 which is almost correct I just need to add the job number in as the id.

 

I have this right now:

$assetresult = $db->fetch_all_array("SELECT * FROM assets WHERE asset = '".$_GET['id']."'");

$site = $assetresult->currentjob; 

if(isset($_POST['updateit']))
{
$db->query("UPDATE assets
			SET pmcount = '".$_POST['avguse']."',
			updatedby = '{$_SESSION['username']}',
			updateddate = NOW()
			WHERE id = '".$_POST['id']."'")
			or die(mysql_error());

header("Location: locationinfo.php?id=$site&updated=1");
exit();
}

 

Link to comment
https://forums.phpfreaks.com/topic/268082-get-value-and-store-as-variable/
Share on other sites

I get:

Array ( [0] => Array ( [id] => 336 [asset] => H802 [currentjob] => 215 [pmcount] => 8 [category] => Aerial [descrip] => Scissor Lift 20' Elec [year] => 2005 [make] => Skyjack [model] => SJ3220 [serial] => 614441 [createdby] => loderd [createddate] => 2012-09-06 15:50:01 [updatedby] => loderd [updateddate] => 2012-09-06 15:50:01 ) ) 

 

I'm using Database.class.php from ricocheting.com

So, you can see from your results that $assetresult is a multidimensional array, not an object.

First, you need to turn on error_reporting set to E_ALL.

Secondly, look if your DB library has docs on how to handle results.

You'll either need to use some functions from the library, or do

$site = $assetresult[0]['currentjob'];

I copied and pasted the results of the print_r I did, this is what my code looks like now.

 

$assetresult = $db->fetch_all_array("SELECT * FROM assets WHERE asset = '".$_GET['id']."'");

$site = $assetresult[0]['currentjob'];

if(isset($_POST['updateit']))
{
$db->query("UPDATE assets
			SET pmcount = '".$_POST['avguse']."',
			updatedby = '{$_SESSION['username']}',
			updateddate = NOW()
			WHERE id = '".$_POST['id']."'")
			or die(mysql_error());

header("Location: locationinfo.php?id=$site&updated=1");
exit();
}

?>

Yup I have the print_r($site); in the code and it shows 215 which is correct.

 

The whole file looks like this:

<?php

session_start();

if($_SESSION['loggedin'] == TRUE)
if($_SESSION['auth_lvl'] > 5) { 

}else{

header("Location: index.php");

}

include('admin/includes/config.php');
include('admin/includes/database.class.php');
include('admin/includes/functions.php');

$db = new Database($db_host, $db_username, $db_password, $db_database, $db_table_prefix);
$db -> connect();

$assetresult = $db->fetch_all_array("SELECT * FROM assets WHERE asset = '".$_GET['id']."'");

$site = $assetresult[0]['currentjob'];

if(isset($_POST['updateit']))
{
$db->query("UPDATE assets
			SET pmcount = '".$_POST['avguse']."',
			updatedby = '{$_SESSION['username']}',
			updateddate = NOW()
			WHERE id = '".$_POST['id']."'")
			or die(mysql_error());

die("Location: locationinfo.php?id=$site&updated=1");
exit();
}

print_r($site);

?>

The header() redirect is inside of some post mode form processing code. There's no guarantee that at the time the form has been submitted that $_GET['id'], that is used in the select query, has a value.

 

Your code has NO validation/casting/escaping of external data to insure that any of that data even has a value, and a safe one, before attempting to use it. If all the code you have been posting requires there to be a $_GET['id']/asset value, then all that code should be inside of a conditional statement that has checked that there is an expected $_GET['id'] value.

 

edit: P.S. Your log in check code is not secure. You need an exit; statement after the header() redirect to prevent the remainder of the 'protected' code from running. All a hacker needs to do is ignore the header redirect and he can access that page the same as if he was logged in.

I understand what you mean, the value might not be there even though it shows before the form is submitted.

 

I have a lot of learning to do obviously, but your post made me think about a different solution and I managed to get it to work. Although its probably not the best way to do it, thank you 2 for helping I really appreciate it.

 

On my form I added:

<input type="hidden" name="job" value="<?php echo $assetinfo['currentjob'];?>" />

 

And I changed:

$site = $assetresult[0]['currentjob'];

to

$site = $_POST['job'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.