Ian Davies wrote:
Hello
I am trying to run a few INSERT queries only if the user clicks 'continue'
in a <a href> The queries takes variables created from SELECT queries. I
have tried to acheive this by putting The INSERT queries into a function at
the top of my php file and having the SELECT queries below it. I have a
message that pops up that includes a link to the function but I cant get
this to work and am not sure of the best approach. Can anyone help?
My striped down code is below. Im not even sure if you can just use a
function to contain code that doesnt return a value. Mine just runs some
INSERT INTO queries
************************************************** *****************
<?php
function PurchaseQuestions(){
various INSERT INTO queries here
<h1>Confirmation</h1><br>
<p><a href="logout.php"> Logout</a> or return to <a href="Questions.php">
Questions Database</a></p><br><br><br>
Congratulations <b><?php echo $_SESSION[username] ?></b>, your order has
been completed.</p>
<?php
}
?>
<?php
$OrderDate = date('d/m/Y');
Three SELECT queries here which provides variables for the
PurchaseQuestions() function
?>
<h1>Order Status</h1>
<p>You have requested <?php echo $QuestSel; ?> questions at a total cost
of <strong>£<?php echo $QuestSel*.8; ?></strong>
If you wish to proceed with the transaction click <a href=<?php echo
PurchaseQuestions() ?>> Continue</a>.<br>
<?php
}
?>
<?php
mysql_close($conn);
?>
I may be misunderstanding you - in which case I apologise for
patronising you - but it seems to me that you are not understanding how
the web works.
Anything the user does is entirely on their client machine. They can
fill in forms and pick links (and possibly completely change what's on
the page they're looking at, if you've embedded suitable client-side
scripting in it- typically Javascript). But nothing can happen on the
server (such as changing the database) until they have requested a new
page, either by picking a link or submitting a form.
Of course, if it is to do anything other than simply sending HTML, that
page must be a CGI script. But it will not have any data from the user's
page unless the data has been sent back as CGI variables - either GET or
POST. Alternatively, in PHP they can have been preserved (on the server)
from when the previous page was processed, by using a _SESSION.
If I understand correctly, what you really want is logically two
separate pages:
page1.php:
// extract the data from SQL
// display it to the user
print "<a href='page2.php?arg1=$val1&arg2=$val2'>Confirm</a>";
page2.php:
$sql = "INSERT INTO ... (arg1, arg2 ... ) VALUES ($_GET['arg1'],
$_GET['arg2'])";
// display whatever you want the page to look like
You can make them a single page if you want by (eg) making the link
href='page1.php?confirm=yes&arg1=$val1&arg2=val2'
and then starting your page with
<?php
if (array_key_exists('confirm', $_GET) && $_GET['confirm'] == 'yes') {
// update database
} else {
// send request-confirmation page
}
but you must still pass the data back from the client.
All the above assumes that it is practical and desirable to send the
data by GET. If you prefer POST (because there's too much of it, or
because you would rather it didn't appear in the URL, then you need to
use a form, even if there is nothing on it but a submit button:
<form method='POST' action = 'page2.php'>
<input type='hidden' name='arg1' value='<?php echo $val1 ?>'>
<input type='hidden' name='arg2' value='<?php echo $val2 ?>'>
<submit name='confirm'>
</form>
Then the variables will be available in the $_POST array in the next page.
Colin