Forum Moderators: coopster
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. :)
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]
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']
}
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
<?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]
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
"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!
$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>
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]
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
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]
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?
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!