/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Passing value from script to script

Hello:

I’m having some trouble and I’ve stared at these scripts for hours and I can’t seem to find what’s causing the problem. I was hoping an objective second pair of eyes could help me out. The solution is probably quite simple and I’m missing the obvious.

I have two scripts. For simplicity, let’s call them script 1 and script 2. Script 1 produces a list of rates and prices for a selected loan program. In addition, script 1 also adds an Edit link.

The Edit link is going to bring you to script 2. The Edit link pass a variable called “rateid” which is an auto-incremented field. Basically it’s the row for the record. Well, when I click Edit, I receive one of my error messages that is in the script. [I]This page has been accessed in error[/I]. This line of code appears at the top of script 2 which is highlighted in red down below.

So, if I want to edit record #2, script 1 passes rateid = 2 to script 2 which should show a form in which editing can be done. The error message appears. For some reason, the rateid from script 1 isn’t passing to script 2 and I can’t seem to understand why.

Can someone help me out? I have the code for both scripts.


Script 1 (Edit link and passes rateid to script 2) – this script works – I highlighted the line of code in blue which should pass the rateid.

[CODE]
<?php

require_once (‘mysql_connect.php’);

if ( (isset($_GET[‘id’])) && (is_numeric($_GET[‘id’])) ) {
$id = $_GET[‘id’];
}
else {
echo ‘<h1>Page Error</h1>
<p>This page has been accessed in error.</p><p><br /><br /></p>’;
exit();
}

$query = “SELECT mortgage_type.id, mortgage_type.type, rates.rate_id, rates.mtg_type_id, rates.rate,
rates.price, rates.date_entered, rates.time_entered
FROM mortgage_type
LEFT JOIN rates
ON mortgage_type.id = rates.mtg_type_id
WHERE mortgage_type.id = $id”;

$result = @mysql_query ($query) or die (mysql_error());

echo ‘<h2>Current Loan Rates</h2>’;

echo ‘<table align=”center” cellspacing=”0″ cellpadding=”5″>
<tr>

<td align=”left”><b>Edit</b></td>
<td align=”left”><b>Rate</b></td>
<td align=”left”><b>Price</b></td>
</tr>
‘;

while ($row = @mysql_fetch_array($result, MYSQL_ASSOC)) {

$id = $row[‘id’];
$type = $row[‘type’];
$rateid = $row[‘rate_id’];
$mtgid = $row[‘mtg_type_id’];
$rate = $row[‘rate’];
$price = $row[‘price’];
$date = $row[‘date_entered’];
$time = $row[‘time_entered’];

echo ‘
<td align=”left[COLOR=”Blue”]”><a href=”newrate.php?rateid=’ . $rateid . ‘”>Edit</a>[/COLOR]</td>
<td align=”left”>’ . $rate . ‘</td>
<td align=”left”>’ . $price . ‘</td>
</tr>
‘;
}

echo ‘</table>’;
echo ‘<p><a href=”welcome.php”>Return to Main Menu</a><br /><br />’;

@mysql_free_result ($result); // Free up the resources.

mysql_close(); // Close the database connection.

?>
[/CODE]

—-
Script 2 (this should be the form to allow you to update a specific record based on rateid from script 1 after clicking Edit.

[CODE]
<?php

if ( (isset($_GET[‘rateid’])) && (is_numeric($_GET[‘rateid’])) ) { //retrieved from view_programs_1.php
$rateid = $_GET[‘rateid’];
} elseif ( (isset($_POST[‘rateid’])) && (is_numeric($_POST[‘rateid’])) ) { //form was submitted
$rateid = $_POST[‘rateid’];
} else { //no valid ID, kill script
echo ‘<h1″>Page Error</h1>
<p class=”error”>[COLOR=”Red”]This page has been accessed in error.[/COLOR]</p><p>< /br>< /br></p>’;
exit();
}

require_once (‘mysql_connect.php’);

if (isset($_POST[‘submitted’])) {
$errors = array();

//check for rate.
if (!is_numeric($_POST[‘rate’])) {
$errors[] = ‘You forgot to enter a rate.’;
} else {
$rate = (float) $_POST[‘rate’];
}

//check for price.
if (!is_numeric($_POST[‘price’])) {
$errors[] = ‘You forgot to enter a price.’;
} else {
$price = (float) $_POST[‘price’];
}

if (empty($errors)) {

// Make the query.
$query = “UPDATE rates SET rate=’$rate’, price=’$price’, date=CURRENT_DATE, time=CURRENT_TIME
WHERE rate_id=$rateid”;
$result = @mysql_query ($query); // Run the query.
if (mysql_affected_rows() == 1) { // If it ran OK.

// Print a message.
echo ‘<p><h2>Rates Updated.</h2></p>
<p><a href=”welcome.php”>Return to Main Menu</a><br /><br />’;

} else { // If it did not run OK.
echo ‘<span class=”bold1″>System Error</span>
<p class=”error”>The user could not be edited due to a system error. We apologize for any inconvenience.</p>’; // Public message.
echo ‘<p>’ . mysql_error() . ‘<br /><br />Query: ‘ . $query . ‘</p>
<p><a href=”welcome.php”>Return to Main Menu</a><br /><br />’; // Debugging message.
include (‘footer3.html’);
exit();
}
}
} // End of submit conditional.

// Always show the form.

// Retrieve the user’s information.
$query = “SELECT mtg_type_id, rate, price, date, time
FROM rates WHERE rate_id=$rateid”;
$result = @mysql_query ($query); // Run the query.

if (@mysql_num_rows($result) == 1) { // Valid load ID, show the form.

// Get the user’s information.
$row = @mysql_fetch_array ($result, MYSQL_NUM);

// Create the form.
echo ‘<hr><span class=”bold2″>Update Rates</span>
<form action=”newrate.php” method=”post”>
<p><label for=”rate”>Rate:</label> <input type=”text” name=”rate” size=”7″ maxlength=”10″ value=”‘ . $row[1] . ‘” /></p>
<p><label for=”price”>Price:</label><input type=”text” name=”price” size=”7″ maxlength=”10″ value=”‘ . $row[2] . ‘” /></p>

<p><label for=”blank”>&nbsp;</label><input type=”submit” name=”submit” value=”Submit” /></p>
<input type=”hidden” name=”submitted” value=”TRUE” />
<input type=”hidden” name=”rateid” value=”‘ . $rateid . ‘” />
</form>’;

} else { // Not a valid user ID.
echo ‘<span class=”bold1″>Page Error</span><hr>
<p class=”error”>This page has been accessed in error.</p><p><br /><br /></p>
<p><a href=”welcome.php”>Return to Main Menu</a><br /><br />’;
}

mysql_close(); // Close the database connection.

?>
[/CODE]

If someone can take a look at this and help me figure out what’s going on, I’ll grealy appreciate it.

Thank you for all the help.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscFeb 14.2007 — I've tested and see nothing wrong with your code as long as the variable that is passed is actually what you say it is, a number. did you check the url you are clicking on? are you positive it's a number? are you positive that its something? (i.e. if this wasn't clear, check your mysql query to see what its actually giving you)
Copy linkTweet thisAlerts:
@focus310authorFeb 14.2007 — Hi,

The field in the database table is set to INT. I believe the value for rateid should be numeric. I haven't done anything anywhere to tell it to be something else.

How could I check whether it is or isn't numeric?

By the way, how did you go about testing?
Copy linkTweet thisAlerts:
@aj_nscFeb 14.2007 — I just copied your first few if-else statements in your second script into a php file and called it from another html file with ?rateid=3 in the url.

What is the actual URL that is being displayed in the location bar when you click one of these links?

You can find out easily enough if it is the && (is_numeric... statement that is giving you the problem simply by removing these from your conditional statements and trying again


One more thing. You have two "This page has been accessed in error" messages in your second script...how do you know its the first one that's giving you the trouble?
Copy linkTweet thisAlerts:
@focus310authorFeb 14.2007 — Hi,

The URL looks like this:

http://localhost/fullfocus/Informed/newrate.php?rateid=5.

I tried removing the issnumeric bit of the code and I'm still receiving the same message.

I forgot about the error message at the end of the script. I believe the message is coming from the else at the end of the script.

The script performs the checks on rateid which should be passed from the previous script. If it's true, the form should appear with the information for that rateid.

When I run my script, the form doesn't appear. My select query states to select all the fields where the rate_id in the table is equal to the rateid which is passed from the previous script. It looks like this is what's causing the problem. But it doesn't make sense why because all the fields I'm selecting are correct in the query, they are all populated, and the rate_id field is numeric as it should be. So, if the rateid value from the previous script is coming across it should make a match.

[code=php]
// Retrieve the rate information.
$query = "SELECT rate_id, mtg_type_id, rate, price, date, time
FROM rates WHERE rate_id=$rateid";
$result = @mysql_query ($query); // Run the query.

if (@mysql_num_rows($result) == 1) { // Valid rate ID, show the form.

// Get the rate information.
$row = @mysql_fetch_array ($result, MYSQL_NUM);

// Create the form.
echo '<hr><span class="bold2">Update Rates</span>
<form action="newrate.php" method="post">
<p><label for="rate">Rate:</label> <input type="text" name="rate" size="7" maxlength="10" value="' . $row[4] . '" /></p>
<p><label for="price">Price:</label><input type="text" name="price" size="7" maxlength="10" value="' . $row[5] . '" /></p>

<p><label for="blank">&nbsp;</label><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="rateid" value="' . $rateid . '" />
</form>';

} else { // Not a valid rate ID.
echo '<span class="bold1">Page Error</span><hr>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>
<p><a href="welcome.php">Return to Load Administration Main Menu</a><br /><br />';
}

mysql_close(); // Close the database connection.
?>

[/code]


I'm not quite sure what to make of it.
Copy linkTweet thisAlerts:
@aj_nscFeb 14.2007 — I figured it would be coming from the end of the script too. This just means that something is up with the condition you have set.

To debug this and see what the problem is, remove the if else statement for now (copy it and paste it somewhere else so you can put it back later) and instead put in these statements, to tell us if there is something wrong with the mysql query you are making at the end:

[code=php]
if(!$result) {
die("Query $query could not be performed at this time <br/>". mysql_error());
}
else if(mysql_num_rows($result) == 0) {
die("No records were found matching your query");
}
else {
echo "You query returned ".mysql_num_rows($result)." records";
}
[/code]

Let me know what happens when you put this in and the error message should give us a clear idea of whats going on.
Copy linkTweet thisAlerts:
@focus310authorFeb 14.2007 — Hi,

I started to dissect my script starting with my select query right above the form.

Instead of specifying all the field names, I did a SELECT * and when I re-ran the script, it worked.

So, I knew there was a problem with my select statement. The problem, I'm ashamed to say, was a typo in a field name. The query never gave me an error message regarding an invalid column so I focused on the error message the whole time.

I fixed the typo and my script is working.

Thank you for your help.
×

Success!

Help @focus310 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...