Forum Moderators: coopster

Message Too Old, No Replies

Passing a number from one page to another

         

Major Hooters

8:52 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



Hey all,

First off let me tell you I am a noob at php. Whew, now that's outa the way, let me ask you people a question.

I have a php script that generates a table out of a search page that gets the info from a mysql database. Here is my table:


<?php
$result = $db->query($query);
$num_results = $result->num_rows;
echo '<p>Number of Records found: '.$num_results.'</p>';
for ($i=0; $i < $num_results; $i++)
{
$row = $result->fetch_assoc();
//echo '<p><strong>Record #' . ($i+1) .'</p>';
echo '<table border="3">';
echo '<tr>';
echo '<tbody bgcolor="lightgreen">';
echo '<td><strong>Record #</td>';
echo '<td><strong>Name</td>';
echo '<td><strong>ID</td>';
echo '<td><strong>Server</td>';
echo '<td><strong>Event</td>';
echo '<td><strong>Database</td>';
echo '<td><strong>Timestamp</td>';
echo '<td><strong>Completed Date</td>';
echo '<td><strong>Reference</td>';
echo '</tr>';
echo '<tr>';
echo '<tbody bgcolor="lightyellow">';
echo '<td>' . ($i+1) .'</td>';
echo '<td>'.stripslashes($row['name']).'</td>';
echo '<td>'.stripslashes($row['id']).'</td>';
echo '<td>'.stripslashes($row['server']).'</td>';
echo '<td>'.stripslashes($row['event']).'</td>';
echo '<td>'.stripslashes($row['dbname']).'</td>';
echo '<td>'.stripslashes($row['datetime']).'</td>';
echo '<td>'.stripslashes($row['wdate']).'</td>';
echo '<td><a href="edit_record.php">'.stripslashes($row['refnum']).'</a></td>';
echo '</tbody></tr></table>';
echo '<strong>Description: <br /></strong>';
echo '<textarea rows="15" cols="100" "12">';
echo str_replace("<br />","",stripslashes($row['description']));
echo '</textarea>';
echo '<br />';
echo '<br />';
echo '<hr style="width: 100%; height: 2px;">';
}
?>

Now what I am trying to do is to have the refnum block be a href to that particular data entry. This would then be a page (edit_record.php) that would allow the user to edit that data specific to that record.
My problem is passing that number to the href page. I have looked at and tried using $_SESSION but to no avail. Any ideas? thanks for the help. :)

dublinmike

9:00 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



Change:
<a href="edit_record.php">
to:
<a href="edit_record.php?refnum='.$row['refnum'].'">

Then in your edit_record.php file, depending on whether you have register_globals set in your php.ini file, your refnum will be contained in either $refnum or $_GET['refnum']. Then you can just pass that into another SQL query to bring down the information associated with that particular record, i.e.
$sql = 'SELECT * FROM table WHERE refnum = '.$refnum.' LIMIT 1;';

Hope that helps.

[edited by: dublinmike at 9:08 pm (utc) on April 4, 2007]

darrenG

9:03 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



Hi,

You can use $_GET to retrieve said refnum.

To pass the refnum to $_GET:

<a href="edit_record.php?refnum='.stripslashes($row['refnum']).'">'.stripslashes($row['refnum']).'</a>

to retrieve the refnum from get in edit_record.php:

if(is_numeric($_GET['refnum'])) {
//if refnum is a number, is_numeric() is a nice way to check if it has
//been tampered
//if its a string, use addslashes()
$refnum = $_GET['refnum']
}

Major Hooters

9:15 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



Thanks for the quick replies!

I'll give those a try and let you know. It might be tomorrow before I get back with you guys, but either way I'll let you know!

Thanks again!

Major Hooters

9:21 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



As a simple test I made my edit_record.php look like this, just to see if it would passs the number:

<?php
if(is_numeric($_GET['refnum'])) {
$refnum = $_GET['refnum']
}
echo $refnum;
?>

and the page shows up blank!

[edited by: Major_Hooters at 9:42 pm (utc) on April 4, 2007]

Major Hooters

10:31 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



I was reading trying to figure out maybe why this didn't work and $_POST and $_GET are usually combined with some type of <form> wrapper, right? So with both of your examples I still cant get the refnum passed to the next page. Am I missing something? thanks again!

cameraman

11:45 pm on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Anything after the page name:
example.com/index.php?a=7&b=hello+there&refnum=6

shows up in $_GET. What you're seeing in reference to forms is when the form's method is set to 'get'.

The above are available to your script as:
$_GET['a']
$_GET['b']
$_GET['refnum']

When you modified your edit_record script, did you go to it directly from your address bar, and if so, did you tack a value on?
edit_record.php?refnum=8

Major Hooters

3:22 pm on Apr 5, 2007 (gmt 0)

10+ Year Member



When the record number (refnum) is moused over it shows the same reference number as that particular record. So that part works. It just when I did my test page to actually print the $_GET[refnum] the page shows up blank.

<?php
echo 'TEST';
if(is_numeric($_GET['ref']))
{
$refnum = $_GET['ref']
echo "Wahoo";
print_r $refnum;
}

?>

I did make a change in the table_header page to reflect the new variable ref

echo'<td><ahref="edit_record.php?ref='.stripslashes($row['refnum']).'">'.stripslashes($row['refnu
m']).'</a></td>';

but to answer your question, yes, edit_record.php?ref=638 does show up in the title bar.

I also looked at my php.ini file. it shows no register_globals. I even tried to add it as register_globals = 1 but it still didn't seem to work.

Is the web server actually looking for a page named edit_record.php?ref=638? Because not even my echo's at the start of the script is showing up. Nothing, just blank.

[edited by: Major_Hooters at 3:28 pm (utc) on April 5, 2007]

jatar_k

3:45 pm on Apr 5, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



my wild guess

your page is blank because display_errors is set to off and you are getting a parse error

my guess at the parse error is the missing semi colon, I have added it below

<?php
echo 'TEST';
if(is_numeric($_GET['ref']))
{
$refnum = $_GET['ref'];
echo "Wahoo";
print_r $refnum;
}
?>

then try again

as opposed to having TEST echo'ed first you could also dump the GET array to see what you are getting raw, just in case that is an issue

Major Hooters

3:52 pm on Apr 5, 2007 (gmt 0)

10+ Year Member



In my best Homer Simpson voice :

"D'OH"

Yes that was it. I feel like an idiot because I worked on that for about two hours yesterday. And I never even noticed the semi-colon missing.

Yes the $_GET works now and is passing the number that I want. Now, hopefully without posting for more help, I should be able to make a page to edit the named record and resubmit it. THANK YOU so much for your help!

jatar_k

4:08 pm on Apr 5, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would set display_errors to yes if I were you, makes life much easier

Major Hooters

4:11 pm on Apr 5, 2007 (gmt 0)

10+ Year Member



Don't hate me for being so noobish, but ...
where would I do that? in the php.ini file or on the web server itself. We use a SUN ONE Java web server 6.1 and we had to make a special compile of php in order for it to work with this particular web server. Got any ideas? tia

jatar_k

4:16 pm on Apr 5, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



yes, in the php.ini

talk to the admins and see what they say, the change should be straight forward

Major Hooters

4:29 pm on Apr 5, 2007 (gmt 0)

10+ Year Member



I looked at our php.ini file and it is there and on:

display_startup_errors = 1
display_errors = 1
error_log = "/tmp/php-errors.txt"
error_reporting = "E_ALL & E_NOTICE"

Wonder why it didn't show anything...

cameraman

5:15 pm on Apr 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know if it makes any difference, but mine uses 'On' and 'Off':
display_errors = On

If register_globals is on, turn it off - being new you don't have to break the habit of not using it.

Major Hooters

9:31 pm on Apr 5, 2007 (gmt 0)

10+ Year Member



Thanks again for all of the help!

Major Hooters

11:00 pm on Apr 5, 2007 (gmt 0)

10+ Year Member



OK, I made my changes and have gotten it all to work except that...

$refnum will not pass to my second query. So when I have the

$sql = "update sdata set description="'.$description.'" where refnum='.$refnum.'"

$refnum will not pass into the $sql. I did a echo $refnum before it and it always pulls blank. Where have I gone wrong? THis is driving me crazy!

I have even tried to reassign the $refnum = $_GET['ref']; but to no avail. Everything works great until I hit submit then I get this:

You are logged in as: Bubba
test test test
<----No refnum!
0 record confirmed updated and submitted into the data base.

Any ideas!?!?!


mt17# more edit_record.php
<head>

<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title></title>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<meta content="" name="author">

</head>
<body bgcolor="#EBEBEA">

<!-- ############################################################################# -->

<?php
$authUser = $_SERVER['PHP_AUTH_USER'];
$firstTwo = substr($authUser, 0, 2);
$lastPart = substr($authUser, 2);
$upperTwo = strtoupper($firstTwo);
$user = $upperTwo.$lastPart;

if(is_numeric($_GET['ref']))
{
$refnum = $_GET['ref'];
}

?>

<!-- ############################################################################# -->
<table style="text-align: left; width: 100%;" border="1" cellpadding="2" cellspacing="2" >
<tbody>

<tr>

<td style="text-align: center; background-color: rgb(51, 153, 153);"><big style="font-weight: bold;">
Edit Record Description Page:</big></td>

</tr>

</tbody>
</table>
<style="text-align: center; background-color: rgb(51, 153, 153);"><style="font-weight:
bold;">You are logged in as: <?php echo $user?>

<!-- ############################################################################# -->
<?php
if (!isset($_POST['submit'])) {

@ $db = new mysqli('localhost', 'blah', 'blah', 'blah');
if (mysqli_connect_errno())
{
echo 'ERROR: Could not connect to database. Check that the db is running and that the server is up.
';
exit;
}
?>
<form action="<?= $_SERVER['PHP_SELF']?>" method="post">
<?php
$query = "select * from sdata where refnum=$refnum";

$result = $db->query($query);
$num_results = $result->num_rows;
// echo '<p>Number of Records found: '.$num_results.'</p>';
for ($i=0; $i < $num_results; $i++)
{
$row = $result->fetch_assoc();
//echo '<p><strong>Record #' . ($i+1) .'</p>';
echo '<table border="3">';
echo '<tr>';
echo '<tbody bgcolor="lightgreen">';
//echo '<td><strong>Record #</td>';
echo '<td><strong>Name</td>';
echo '<td><strong>ID</td>';
echo '<td><strong>Server</td>';
echo '<td><strong>Event</td>';
echo '<td><strong>Database</td>';
echo '<td><strong>Timestamp</td>';
echo '<td><strong>Completed Date</td>';
echo '<td><strong>Reference</td>';
echo '</tr>';
echo '<tr>';
echo '<tbody bgcolor="lightyellow">';
//echo '<td>' . ($i+1) .'</td>';
echo '<td>'.stripslashes($row['name']).'</td>';
echo '<td>'.stripslashes($row['id']).'</td>';
echo '<td>'.stripslashes($row['server']).'</td>';
echo '<td>'.stripslashes($row['event']).'</td>';
echo '<td>'.stripslashes($row['dbname']).'</td>';
echo '<td>'.stripslashes($row['datetime']).'</td>';
echo '<td>'.stripslashes($row['wdate']).'</td>';
echo '<td>'.stripslashes($row['refnum']).'</td>';
echo '</tbody></tr></table>';
echo '<strong>Description: <br /></strong>';
echo '<textarea name="description" rows="15" cols="100" "12">';
echo str_replace("<br />","",stripslashes($row['description']));
echo '</textarea>';
echo '<br />';
}
//$result->free();
// $db->close();

echo '<input name="submit" value=" Commit Edit" type="submit" style="color: rgb(204,0,0)">';
echo '</form>';
}

if ($_POST['submit']) {
$description = $_POST['description'];

echo '<br />';echo $description;echo '<br />';echo $refnum;echo '<br />';

@ $db = new mysqli('localhost', 'blah', 'blah', 'blah');
$sql = "update sdata set description='".$description."' where refnum='.$refnum.'";
$result = $db->query($sql);
if($result === FALSE)
{
die("Query failed: $sql <br>\n".$db->error);
}
if ($result)
{
echo '<strong>';
echo $db->affected_rows.' record confirmed updated and submitted into the data base.
';
echo '</strong>';
}
$result->free();
$db->close();
}
?>
</body>

jatar_k

1:28 pm on Apr 6, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



your quotes on that query are all mismatched. I think it should be this way

$sql = "update sdata set description='".$description."' where refnum='" .$refnum. "'";

Major Hooters

2:02 pm on Apr 6, 2007 (gmt 0)

10+ Year Member



Thanks for the correction. I tried that also to no avail. It still is not passing refnum which earlier in the script was passed as $refnum = $_GET['ref']

It works in the first query, the select. It's just somewhere I either hosed up something afterward, though I cant figure out where.

The description passes but $refnum still shows up blank.

Is there some type of rule about how often you can use a $_GET or something obscure? Could the closing of the db or clearing of it cause some problems with earlier defined variables? tia!

[edited by: Major_Hooters at 2:05 pm (utc) on April 6, 2007]

jatar_k

4:01 pm on Apr 6, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I don't see you accidentally reassigning it

I don't really see a reason it should be blank

though you could change your query again
$sql = "update sdata set description='".$description."' where refnum=" .$refnum;

you don't need to surround integers with single quotes, sorry missed that

I would try echoing the value through out the script to see where it is dropping the value

Major Hooters

4:52 pm on Apr 6, 2007 (gmt 0)

10+ Year Member



well I did do that as you had suggessted and it appears that after my
if ($_POST['submit']) {
that the variable ($refnum) is not getting passed. Is there some type of $_POST/$_GET etiquette that I might not be following?

This is the output:

You are logged in as: Bubba
test test test asd sda asd asd asdasd asd
<----still no refnum
Query failed: update sdata set description='test test test asd sda asd asd asdasd asd' where refnum=
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

[edited by: Major_Hooters at 4:56 pm (utc) on April 6, 2007]

jatar_k

5:26 pm on Apr 6, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



are you sure you are passing it?

you are using both POST and GET so I am not sure if something is being confused

when the form is submitted it sends the values from the form in the POST array to the url set in the form action

are you adding the refnum to the url in the form action?
are you just posting to self?
what is the exact url in the location bar when the form is processing? do you see the refnum up there?

Major Hooters

7:06 pm on Apr 6, 2007 (gmt 0)

10+ Year Member



Yeps it passing all the way to the POST. Here is what I use from the referring page :

echo '<td><a href="edit_record.php?ref='.stripslashes($row['refnum']).'">'.stripslashes($row['refnum
']).'</a></td>';

And it is passing the ref, otherwise I wouldn't be able to even pull the record.

I have been doing some reading and such and I think that the $_GET['ref'] is not getting past the <input ...> that I have so right at this moment I am trying to add a line above my input that looks like this:

echo '<input type="hidden" name="refnum" value=ref>';
echo '<input name="submit" value=" Commit Edit" type="submit" style="color: rgb(204,0,0)">';

And then pull it out with the $_POST, like I did with description, $description = $_POST['description']
maybe like this:
$refnum = $_POST['ref']

On my first attempt it changed all of my records to the $description, but it's test data so blah and it's kinda working. I cant seem to pass the correct value to it. I tried value="$ref" and value="{$_GET['ref']} but I just dont know enough with out all this experimentation. Do you know the right way to pass a variable to a value=? thanks again for all of the help!

Major Hooters

9:00 pm on Apr 6, 2007 (gmt 0)

10+ Year Member



Wahooooo!

This is what worked:

echo '<input type="hidden" name="aref" value="'.$refnum.'">';
echo '<input name="submit" value=" Commit Edit" type="submit" style="color: rgb(204,0,0)">';

Then pullingit out with this:

$refnum = $_POST['aref'];

Thanks again for all of the help!

jatar_k

9:35 pm on Apr 6, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



nice work, I thought it might be something like that