Page 1 of 1
$_GET
Posted: Tue Jun 16, 2009 10:16 am
by dbdsol
I'm having a small problem, but havent found a solution by googling yet.
I have a web page meant for editing a database and I keep losing a key variable. Basically I have something like this:
lets say the page is
http://www.mypage.com/edit/php?trans=12
Code: Select all
$id = $_GET['trans'];
/* code to use this $id in order to retrieve that exact row of data from mysql */
if (!isset($_POST['submit']))
{
/* code to create a form and insert data from mysql into each text box */
}
else
{
/*
code to update the database with any/all adjustments made to the form after submit has been clicked
*/
/*
Problem: $id no longer has a value so I cannot use it to update the mysql database
*/
}
I have no idea what the scope of _GET is but every other variable I have is accessible except $id. What am I missing here? Thanks in advance.
Re: $_GET
Posted: Tue Jun 16, 2009 10:19 am
by mattpointblank
You don't have a semicolon at the end of line 2...?
Re: $_GET
Posted: Tue Jun 16, 2009 10:20 am
by dbdsol
mattpointblank wrote:You don't have a semicolon at the end of line 2...?
I do, just forgot it when I typed this in here. This isnt my exact code.
Re: $_GET
Posted: Tue Jun 16, 2009 10:22 am
by mattpointblank
On its own that code looks okay, then - you'll have to show us more specific code. It's difficult to debug your code if you don't show us what you're using...
In scope terms, $_GET is accessible on any page (included, or directly accessed) which is reached with the relevant URL parameter added, I believe. Maybe your variable name is being overwritten somewhere?
Re: $_GET
Posted: Tue Jun 16, 2009 10:46 am
by dbdsol
i'll post all thats in edit.php
Code: Select all
<html>
<body>
<?php
$host = "localhost";
$user = "user";
$password = "pass";
$db = "cashflow";
$table = "transaction";
$id = $_GET['trans'];
//open connection
$connection = mysql_connect($host, $user, $password) or die ("Unable to connect!");
// select datebase
mysql_select_db($db) or die ("Unable to select database: $db!");
// Query
$sel=mysql_query("SELECT * from $table where id='$id'")or die(mysql_error());
$row=mysql_fetch_array($sel);
if (!isset($_POST['submit'])) {
// form not submitted
// connect to database
$date=$row['t_date'];
$amount=$row['amount'];
$deposit=$row['deposit'];
$comment=$row['comment'];
?>
<form name="create" action="<?=$_SERVER['PHP_SELF']?>" method="post">
<?php
echo "Date: <INPUT TYPE=\"text\" NAME=\"date\" VALUE=\"$date\" SIZE=10>";
?>
<A HREF="#"
onClick="cal.select(document.forms['create'].date,'anchor1','MM/dd/yyyy'); return false;"
NAME="anchor1" ID="anchor1">select</A>
<br>
<br>
Amount: <input type="text" name="amount" value="<?php echo $amount; ?>">
<br>
<br>
Type: <input type="radio" name="radioset" value="1" <?php if ($deposit == 1) echo "checked=\"checked\""; ?>/>Deposit
<input type="radio" name="radioset" value="2" <?php if ($deposit == 0) echo "checked=\"checked\""; ?>/>WithDrawal
<br>
<br>
Comments: <textarea name="comments" rows="3" cols="80"><?php echo $comment; ?></textarea>
<br>
<br>
<input type="submit" name="submit">
</form>
<?php
}
else
{
$date = empty($_POST['date']) ? die ("ERROR: Enter a date") : mysql_escape_string($_POST['date']);
$new_date = date("Y/m/d", strtotime($date));
$amount = empty($_POST['amount']) ? die ("ERROR: Enter an amount") : mysql_escape_string($_POST['amount']);
$amount = floatval($amount);
$deposit = empty($_POST['radioset']) ? die ("ERROR: Check Deposit or Withdrawal : '$deposit'") : mysql_escape_string($_POST['radioset']);
if ($deposit == "2")
$deposit = 0;
$deposit = (int)$deposit;
$comment = empty($_POST['comments']) ? die ("ERROR: Enter a comment") : mysql_escape_string($_POST['comments']);
$query = "UPDATE $table SET t_date='$new_date',amount=
'$amount',deposit='$deposit',comment='$comment' WHERE id=$id";
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// close connection
mysql_close($connection);
echo "<a href=\"edit.php?trans=$id\">Click</a>";
}
?>
</body>
</html>
Re: $_GET
Posted: Tue Jun 16, 2009 10:50 am
by mattpointblank
Sorry, which bit doesn't work, specifically?
Re: $_GET
Posted: Tue Jun 16, 2009 11:06 am
by dbdsol
mattpointblank wrote:Sorry, which bit doesn't work, specifically?
On line #91 when I submit the sql $query I get this error from mysql: Error in query: UPDATE transaction SET t_date='2009/06/08',amount= '925',deposit='1',comment='Temp Power K&A' WHERE id=. 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 2
$id no longer has a value. I confirmed that $id does have a valid value everywhere before line #68.
Re: $_GET
Posted: Tue Jun 16, 2009 11:16 am
by mattpointblank
Ah, it could be because you're using it inside your form submission code - I could be wrong, but I think that once a form is submitted, the page it submits to can't reach the $_GET attributes (can anyone confirm this?). Either way, the way I normally handle this is to embed the ID value in a hidden form field, which you can then access by $_POST when the form is submitted.
Add this to your form:
<input type="hidden" name="secretID" value="<?php echo $id; ?>" />
Then when your form is processed, you can get the ID again by using
$secretID = $_POST['secretID'];
That should sort it.
Re: $_GET
Posted: Tue Jun 16, 2009 11:33 am
by dbdsol
Yes that works!
I figured the problem was something to that effect, but I was just trying to find ways to make a copy of the data in $_GET so that I could preserve it.
Is there a way to make a copy of a variables value so that when it dies you dont lose the value?
Re: $_GET
Posted: Wed Jun 17, 2009 3:15 am
by mattpointblank
You can use PHP's session functions, which persist across a session (eg, as long as the browser window is open), from page to page. They're pretty simple: Run this function to open a session:
session_start();
(has to run BEFORE any browser output, eg HTML code etc)
Then assign data to it like this:
$_SESSION['yourVariable'] = $_GET['id'];
Now on any subsequent page, you can access $_SESSION['yourVariable'] (as long as you call session_start() again on that page too). Pretty useful.