echo function call 2 variables - php

Ok so I have the code for a form that is called and works but it needs two varibles grabbed from the string of a url. I have the first and the second is the same for what im doing on any page that I am creating which is alot. Here is the code at the url: collabedit.com/9g99j
Question if Get <?php echo $_GET['id']; ?> is grabbing my id string from the url how do I use this in the echo of my function I just defined at the bottom of the code? Instead of having this: echo DescriptionGet(1256124, 50874); can someone tell me how to put something like this: echo DescriptionGet(1256124, $id);
This would make it so i dont' have to enter that id value for every page I want to create.
Thanks,
Thanks everyone for your replies and I was able to figure it out on my own and actually used exactly what the first reply was.
Now I have a new question about this function. How do I make it grab the image from that same page its grabbing the form code from? I can't figure this part out and its keeping me from doing mass automation for this site.
Anyone help?

Try this:
$id = $_GET['id'];
echo DescriptionGet(1256124, $id);

You can change your function definition from:
function DescriptionGet($c, $id)
to
function DescriptionGet($c, $id=50874)
Each time when you will call DescriptionGet($c) it will behave as you passed $id=50874 but also if you need you can call DescriptionGet($c, 20) and $id in the function will be set to 20.
And in case you want to simple use $_GET['id'] as function parameter you can simple run
echo DescriptionGet(1256124, intval($_GET['id']));
you don't even need to use extra variable.

Related

How do i use GET correctly in php?

i tried to find a solution for my problem for 2 hours now, but i don't know why my code does not work.
I have a sql output which looks like this:
function output(){
while($row = $this->statement->fetch()) {
$id = $row["id"];
echo '
<tr>
<td>'.$row["comname"].'</td>
<td>'.$row["district"].'</td>
<td>'.$row["industry"].'</td>
<td>"Details"</td>
</tr>
<br>
';
}
If someone click on the link "Details" i want to give out more information about that specific company. Therefore i save the id in the url to identify which company was clicked.
To check if the Details link was clicked, i wrote this:
Edit: just added the "$id = $_GET['details']" after your hints, it looks like this now:
if (isset($_GET['details'])){
$id = $_GET['details'];
echo $id;
}
}
When i click on the link "Details" it changes the URL correctly, but it doesn't print the id. (I don't only want to print the id, i just do this to check the functionality.) Why does my code not work? Is there a second "$GET" i have to use? I really don't know what is going on.
Edit: The php-code ends here, there is nothing i do afterwards.
Edit2: I tried print_r($_GET) and it looks like, the id is not even in the $GET-Array. Also the if (isset($_GET['details'])) statement is not executed.
Thank you!
You should print the $_GET['details']:
if (isset($_GET['details'])){
echo $_GET['details'];
}
Or put it in a variable:
if (isset($_GET['details'])){
$id = $_GET['details'];
echo $id;
}
$_GET[] is just an array of all GET parameters in the URL. You see them for example on https://www.google.com?q=stack+overflow where the parameter q is set to stack+overflow. So if you would echo out $_GET["q"] on that URL you would get stack+overflow. You can store it in a variable like $id and echo it out, but you need to set it first like $id = $_GET["details"];
EDIT: I just realized the code you have now is vulnerable to an attack called XSS or HTML Injection. Since we can specify the $_GET["details"] and so $id that is being echoed, an attacker can put HTML code or the <script> tag in there to execute dangerous JavaScript code on everyone that accesses the URL.
Luckily, there is an easy fix: just put the function htmlspecialchars() around whatever user input you echo. The echo you have here would become echo htmlspecialchars($id);

How to set $userID from queried object ID in Wordpress

I am editing a template to try and add some conditional logic to my page.
The page template shows topics related to a user.
I want to add a piece of code which will grab the user name from the page we are viewing and then use that in a string for my conditional statements.
The code I have put together is as follows, but it breaks my page so I am doing something wrong.
<?php global
// I query the ID and try and set that to the $userID - I think I am doing this wrong, but when I echo the ID it gets the correct info.
$userID = get_queried_object()->ID;
// This is the string I create using the userID which should be from the query above
$memberstatus = get_user_meta($userID,'member_status',true);
?>
later on I use IF statements to use thsi result (which i know work) so i won't post them. My problem is trying to get the above to work.
Any help?
damm, looks like when I remove 'global' from the php it works! I thought global had to be in this...ah well

Post variable not being caught in php sent from dynamically generated html

Im having a really simple issue but iv looked around and cant debug it for some reason, can someone point me in the right direction??
I have a php script which dynamically generates a link
<?php
$id = 1;
echo "<a href='http://www.example.com/page.php?id='$id'>click link</a>"
?>
On example.php I have...
$userId = $_POST['id'];
then I insert $userId query...
?>
For some reason the Post vairable is not being cause by the example.php script I can see it in the URL at the top of the page but they wont make sweet passionate php love. Any thoughts? I will mention I am doing this from within an IFRAME however I tried it simply and got the same result :(
I think you mean, on page.php you have...
If that is the case, you are sending the id parameter in a GET, not a POST. To access it in your other page you need to use:
$userId = $_GET['id'];
your variable is in $userId = $_GET['id'];.
another problem is a mess with ' symbols: should be
echo "<a href='http://www.example.com/page.php?id=$id'>click link</a>"
Sorry, but you ar sending data via GET NOT POST
access it via $_GET['id'];

CodeIgniter session value as images

In my project, I am using pagination and I used these statements to get the page number detail:
$page=$this->uri->segment(3);
$this->session->set_userdata('page',$page);
echo $this->session->userdata('page');
When I print this session value in that page itself, I get the value correctly and when I click on the particular link and then print that data, I am getting the value like 'images'.
Why is this happening?
However, when I write the statements like
$page=$this->uri->segment(2);
$this->session->set_userdata('page',$page);
echo $this->session->userdata('page');
it's working fine.
My URL is: http://localhost/CI/user/index/4
I have the same problem with you, the session variable always get value 'images'. Then i realize that: if we made a session variable, then we must call redirect() directly.
This is my example:
$page=$this->uri->segment(3);
$this->session->set_userdata('page',$page);
redirect('control/function2');
Then, you can get continue your code in the 'function2'.
Solved the issue by writing another function, setting the session and redirecting to above function like this:
function set_session(){
$id=$this->uri->segment(3);
$this->session->set_userdata('id',$id);
redirect('set_uri_session');
}
function set_uri_session(){
$id=$this->uri->segment(3);
$this->session->set_userdata('id',$id);
echo $this->session->userdata('id');
}

mysql LAST_INSERT_ID() is causing some SQL problems when passing back value retrieved

I need some help figuring out why the following scenario does not work. I'm trying to retrieve a value from the last updated ID in mysql, then pass that value via javascript over to an ajax call which calls a .php page, which also calls another function "ZEND_emaiL" in a different php page.
In the very first php page that retrieves the id from mysql LAST_INSERT_ID(), if I hard code the value "100" it works, but if I use the value returned from LAST_INSERT_ID() it causes a failure.
Here's the php code for the LAST_INSERT_ID():
$sql='SELECT LAST_INSERT_ID();';
$last_updated_id = $db->get_var( $sql );
$last_updated_id = $last_updated_id+0;//make int
echo $last_updated_id; //send output back to the ajax call
var_dump($last_updated_id); ------------->RETURNS **int 149**
if I send back a hard coded "100" like this: echo 100; then it works.
Any ideas? Thanks for your help in advance.
The following are values retrieved from the php page that contains the ZEND_email() function. I grabbed these for debugging purposes hoping it would help.
RETURN VALUES for Hard Coded:
var_dump($n_id);---------->Returns **int 100**
var_dump($sqlresult);----->Returns **resource 24**
var_dump($row);----------->Returns **array of data to parse through**
RETURN VALUES FOR Passed in Variable (Fails):
function ZEND_email($to, $from="", $subject="", $msg="", $notif_id='', $root_dir="")
{
var_dump($notif_id);---------------------->RETURNS **string '100'**
$notif_id = $notif_id+0;//convert to int
var_dump($notif_id);---------------------->RETURNS **int 100**
$n_id = $notif_id;
$xsql = $sql_str->SQL_SELECT_all_notif_attachments($account_id, $n_id);
$sqlresult=mysql_query($xsql);
$row=mysql_fetch_row($sqlresult);
var_dump($n_id);---------------->RETURNS **int 100**
var_dump($sqlresult);----------->RETURNS **resource 24**
var_dump($row);----------------->RETURNS **boolean false**
}
you are aware that you could use mysql_insert_id() ?
see http://uk.php.net/manual/en/function.mysql-insert-id.php which would give you an INT value directly,
btw, to convert a variable to integer you can use:
$foo = (int) $bar
or
$foo = intval($bar);
or
$foo = settype($bar,'int');
Hard to tell from those code snippets. Better check what's going on on the client-side.
E.g. with firebug you can both check the actual response data and step into the javascript code.
Had to scrap this code...couldn't get it all to work with the feature for the app so we dropped it. Thanks for your help.

Categories