473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Best approach to run queries only when user chooses

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);
?>
Oct 24 '05 #1
8 2119
>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
You can't have a link to a PHP function. Code inside a function
will not be executed unless something calls it. You CAN have a SUBMIT
button for a form, which the user can fill in and click on.
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
A function does not have to return a value, and in any case, you
could always supply a dummy value to return.

After a user clicks on the SUBMIT button to a form, you look at
what's in $_GET and $_POST to decide whether or not to call the
function, like the name of the submit button or whether the fields
in the form were filled in.

Gordon L. Burditt

************************************************* ******************
<?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);
?>

Oct 24 '05 #2
Thanks for the reply
After a user clicks on the SUBMIT button to a form, you look at
what's in $_GET and $_POST to decide whether or not to call the
function, like the name of the submit button or whether the fields
in the form were filled in.


I dont have any fields that need submitting. Could I put the function in the
form and if user clicks submit it calls the function if I put the functions
name in the forms 'action'?
Oct 24 '05 #3
>> After a user clicks on the SUBMIT button to a form, you look at
what's in $_GET and $_POST to decide whether or not to call the
function, like the name of the submit button or whether the fields
in the form were filled in.
I dont have any fields that need submitting.


The submit button itself can have a name.
Could I put the function in the
form
NO, you cannot put functions in HTML. Well, you can put the text
of the function, but then it won't be a function.
and if user clicks submit it calls the function if I put the functions
name in the forms 'action'?


No, you cannot put function names in HTML and expect it to do any good.

Gordon L. Burditt
Oct 24 '05 #4
>>>After a user clicks on the SUBMIT button to a form, you look at
what's in $_GET and $_POST to decide whether or not to call the
function, like the name of the submit button or whether the fields
in the form were filled in.


I dont have any fields that need submitting.

The submit button itself can have a name.

Could I put the function in the
form

NO, you cannot put functions in HTML. Well, you can put the text
of the function, but then it won't be a function.

and if user clicks submit it calls the function if I put the functions
name in the forms 'action'?

No, you cannot put function names in HTML and expect it to do any good.


While all of this is true what he could do is use ajax to call the
function and pass the parameters that way :)

--
Mike Willbanks
Zend Certified Engineer
http://www.digitalstruct.com
Oct 24 '05 #5
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

Oct 24 '05 #6
Colin
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.
You right there. I am new to web programming. I have a VB project that need
to become web enabled and I am learning as I rewrite this project for the
web. Thank you for all your information, it has been saved and will be added
to my resources for future use.

Another question. It seems that a variable can only be passed on to the next
form. How can I pass it to a third form in the line? Would I have to 'leap
frog' it from one hidden field to the next via a form's POST action or
should I use sessions.
Also if you know of a good link to a tutorial on the above and also on
filtering values in a dropdown list based on the values in preceeding
dropdown list I would be v grateful as most of my searches for such things
lead usually to dead ends

Thanks
Ian

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

Oct 25 '05 #7
Jon
Once you start to pass variables to more than just one page, I've found the
best method is to use session variables and start calling them from the
different pages. I try and limit the usage of sessions because it gets hard
to control where all of your data is located, but it's really handy if you
need to hold variables for multiple pages.

As far as the drop-down, I'm not quite sure what you're saying I guess. If
you want to populate a drop down list based on conditions, it's fairly easy
in PHP - Here's an outline of the process I would use:

<select name="nameSelect">
<?php
if/else conditions here
if($a==3){
echo "<option>Option from if condition</option>";
}else{
echo "<option>Option from else condition</option>";
}
?>

That's kind of the process I use - you are basically echoing the values in
the select list - if this were coming from a DB, you'd basically run this in
a loop of some sort likely using mysql_fetch_array
(while($row=mysql_fetch_array($queryResults)){
psuedocode from above here
}

Does that help?

"Ian Davies" <ia********@virgin.net> wrote in message
news:1b***************@newsfe2-win.ntli.net...
Colin
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.


You right there. I am new to web programming. I have a VB project that
need
to become web enabled and I am learning as I rewrite this project for the
web. Thank you for all your information, it has been saved and will be
added
to my resources for future use.

Another question. It seems that a variable can only be passed on to the
next
form. How can I pass it to a third form in the line? Would I have to 'leap
frog' it from one hidden field to the next via a form's POST action or
should I use sessions.
Also if you know of a good link to a tutorial on the above and also on
filtering values in a dropdown list based on the values in preceeding
dropdown list I would be v grateful as most of my searches for such things
lead usually to dead ends

Thanks
Ian

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


Oct 25 '05 #8
Thanks everyone for your feedback. Ive used hidden fields and will read up
on sessions. Still need some good links to info about drop down lists though
Ian

"Ian Davies" <ia********@virgin.net> wrote in message
news:fM******************@newsfe7-gui.ntli.net...
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);
?>

Oct 25 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

52
by: Andy Dingley | last post by:
I'm using this at present: <p title="Publication date" ></p> Works fine on screen, but Fangs/Jaws just reads it as "left bracket twenty-eight slash zero slash two thousand five fifteen colon...
17
by: | last post by:
I have an app that retrieves data from an Access database. At the moment I have the SQL string as a Const in my app. I understand this is not best practice. I don't want the user to have access to...
2
by: Lars Netzel | last post by:
I want to build a Function for users to be able to Choose some shortcut combinations themselves.. for example, I provide the function they can choose the combination of keys in some kind of...
4
by: Dave | last post by:
(My apologies for posting this on two forums. I have just found out the other one was the incorrect location) I am writing a VB.NET 2003 web application to operate on my company's intranet. It...
20
by: Keith G. Murphy | last post by:
I'm trying to get a feel for what most people are doing or consider best practice. Given a mod_perl application talking to a PostgreSQL database on the same host, where different users are...
7
by: Steve | last post by:
I am building an object library for tables in a database. What is the best practice for creating objects like this? For example, say I have the following tables in my database: User: - Id -...
14
by: Patrick A | last post by:
All, I have an Access DB. On a nightly basis, I want to look at an Other DB (not Access, but SQL) and: + Add any new records from Other.Clients into Access.Clients Is this something I...
2
by: Ashley | last post by:
hey, what's up............................. "Neil" <nospam@nospam.netwrote in message news:8YFwj.10509$0o7.1113@newssvr13.news.prodigy.net...
10
by: Brendan Miller | last post by:
What would heavy python unit testers say is the best framework? I've seen a few mentions that maybe the built in unittest framework isn't that great. I've heard a couple of good things about...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.