943,972 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1104
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 15th, 2009
0

Different function for different buttons in php-mysql question

Expand Post »
Hi
I m making web page and for that i am using mysql and php. In tha i enter data and that is to be stored in mysql database. I m having a problem in that. I want add, delete, previous, next, cancel and update button in that. But can't make all buttons work differently.
I want all buttons performing different function.
Pls guide me what should i do...
Thanx in advance 2 all great minds who'll help..
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sumit007 is offline Offline
12 posts
since Oct 2009
Oct 15th, 2009
0
Re: Different function for different buttons in php-mysql question
First things first, you need to write simple algorithms/procedures for your desired tasks. From what i see the simple steps would be something like.
1. click add takes you to page for adding data
2. click update modifies your data
3. click delete will take you to page for deletion
4. click next, displays next record from recordset
5. click previous, displays previous record from recordset

So from the looks of things here, you will can do all these things one webpage, but for clarity we'll do it with 2 pages.

Page1.php will display the data in a single row, and will contain a form comprising of:
- textboxes, textareas, or ... depending on the data stored in the db.
- 4 buttons (Update, Next, Previous & Delete) at the bottom of the page

a) Update button(input of type submit) will just post the form to the the same page(Page1.php) and update the record and redisplay it.
b) Delete button(input of type button) will use the onclick event of the button to pass the record id via the url to the page responsible for deletion(also Page1.php). Code in the onclick event will be something like onclick="window.location='Page1.php?id=2&action=delete'" . This id in the url is set, whilst you filling up your form controls with data at the server, and is what you'll use to identify the record to be deleted.
c) & d)Previous & Next buttons (inputs of type button) will have similar code to that of the Delete button, except that the $_GET variables id and action have different values
- Previous button: <input type="button" name="previous" onclick="window.location='Page1.php?id=1&action=move'"> - Next button: <input type="button" name="next" onclick="window.location='Page1.php?id=2&action=move'"> Here, the id values are the ids of the record to display next.

NOTE: At the server when handling these navigation requests(Next & Previous) you must take care to check for the validity/existence of the next or previous record.

Page2.php will be responsible for the creation of the new data. Therefore it will just contain a form similar in structure to Page1.php but instead of updating, it will insert the data into the database. This page can also post data to itself, then after inserting the data, redirects to Page1.php.

That's the logic you can use to start of with.
Last edited by wilch; Oct 15th, 2009 at 4:47 pm. Reason: [icode] tags missing
Reputation Points: 21
Solved Threads: 15
Junior Poster in Training
wilch is offline Offline
76 posts
since Aug 2007
Oct 16th, 2009
0
Re: Different function for different buttons in php-mysql question
Well thanx 4 the attention given to my quest. I have some doubts
For onclick button i have to use javascript??
and what if i want to use $_POST in case of $_GET
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sumit007 is offline Offline
12 posts
since Oct 2009
Oct 16th, 2009
-1
Re: Different function for different buttons in php-mysql question
Post your code and we'll have a look at it.
Sponsor
Featured Poster
Reputation Points: 1048
Solved Threads: 949
Sarcastic Poster
ardav is offline Offline
6,699 posts
since Oct 2006
Oct 16th, 2009
2
Re: Different function for different buttons in php-mysql question
Hey.

Simplest solution, just name all your submit buttons the same and do stuff based on the value.

Like, if you have this form:
html Syntax (Toggle Plain Text)
  1. <form action="process.php" method="post">
  2. <input type="submit" name="button" value="Add">
  3. <input type="submit" name="button" value="Delete">
  4. </form>
You could do this:
php Syntax (Toggle Plain Text)
  1. <?php
  2. if($_POST['button'] == "Add") {
  3. // Do the add stuff
  4. }
  5. else if($_POST['button'] == "Delete") {
  6. // Do the delete stuff
  7. }
  8. else {
  9. // Default action.
  10. // This would happen if the page was requested directly, bypassing the form.
  11. // Or, if the form was submitted using the enter key. (In most browsers)
  12. }
  13. ?>
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Oct 17th, 2009
0
Re: Different function for different buttons in php-mysql question
Hii.. Thanx for considering my thread. I had done the coding same as done by Mr. Atli. I m posting raw coding of add button. It is not working.
Also guide me how can i make a check to check whether database of given name exists or not.


php Syntax (Toggle Plain Text)
  1. <?php
  2. $con = mysql_connect("localhost","root","omomom");
  3. if (!$con)
  4. {
  5. die('Could not connect: ' . mysql_error());
  6. }
  7. if($_POST[Button1]=="ADD")
  8. {
  9. add();
  10. }
  11. else if($_POST[Button1]=="CANCEL")
  12. {
  13. cancel();
  14. }
  15. else if($_POST[Button1]=="DELETE")
  16. {
  17. delete();
  18. }
  19. else if($_POST[Button1]=="PREVIOUS")
  20. {
  21. previous();
  22. }
  23. else if($_POST[Button1]=="NEXT")
  24. {
  25. next();
  26. }
  27. function add()
  28. {
  29. // Create database
  30. if (mysql_query("CREATE DATABASE info123",$con))
  31. {
  32. echo "Database created";
  33. }
  34. else
  35. {
  36. echo "Error creating database: " . mysql_error();
  37. }
  38.  
  39. // Create table
  40. mysql_select_db("info123", $con);
  41. $sql = "CREATE TABLE members
  42. (
  43. Name varchar(15),
  44. Father'sName varchar(15),
  45. CollegeID varchar(10),
  46. NetID varchar(10),
  47. Email-ID varchar(30)
  48. )";
  49.  
  50. $sql="INSERT INTO info123 (Name,Father'sName,CollegeID,NetID,Email-ID)
  51. VALUES
  52. ('$_POST[Text1]','$_POST[Text2]','$_POST[Text5]','$_POST[Text4]','$_POST[Text6]')";
  53.  
  54.  
  55. /*if (!mysql_query($sql,$con))
  56.   {
  57.   die('Error: ' . mysql_error());
  58.   }*/
  59. echo "record added";
  60. }
  61. mysql_close($con)
  62.  
  63. ?>
Last edited by peter_budo; Oct 17th, 2009 at 5:11 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sumit007 is offline Offline
12 posts
since Oct 2009
Oct 17th, 2009
0
Re: Different function for different buttons in php-mysql question
There are a few errors in that code.

#1 You open the database connection outside the add function, and then try to use it inside the function. This doesn't work, because the function operates on a different Variable Scope.
If you want to use a global variable inside a function, you need to import it using the global keyword.
php Syntax (Toggle Plain Text)
  1. <?php
  2. $a = "Hello!";
  3.  
  4. function foo() {
  5. echo $a; // Prints nothing. Variable $a doesn't exist in this scope.
  6. }
  7.  
  8. function bar() {
  9. global $a;
  10. echo $a; // Prints: "Hello!"
  11. }
  12. ?>
It would be better, in your case, to just open the connection inside the function, rather then outside it.

#2 The names Fater'sName and Email-ID , in your CREATE TABLE command, are illegal. In SQL, the single-quote serves as a string open/close char, and the dash is used as a "minus" sign (for calculations).
If you want to use them in your query, you need to enclose them in back-ticks. (Note these are NOT single-quotes!)
sql Syntax (Toggle Plain Text)
  1. CREATE TABLE `example`(
  2. Field-Name ... -- This is illegal and will cause an error
  3. `Field-Name` ... -- This is OK
  4. );

#3 You don't actually execute either your CREATE TABLE , nor your INSERT query.
Although, you do have the mysql_query call commented out there at the bottom, so I assume you tried it at some point.

#4 $_POST[Button1] should be $_POST['Button1'] .
Strings need to be quoted, and the name of the element is a string.
Even tho this doesn't cause an error (PHP fixes this in the background) you should still do this right.

O, and P.S.
Please use [code] tags when posting you code. Makes it sooo much easier to read ;-]
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Oct 18th, 2009
0
Re: Different function for different buttons in php-mysql question
A Heartiest Thanx 2 u Sir.. I'll work ahead as u said..
now please guide me how should i check whether a database named info exists or not? if it exists then perform update operation otherwise perform create database operation.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sumit007 is offline Offline
12 posts
since Oct 2009
Oct 18th, 2009
0
Re: Different function for different buttons in php-mysql question
Click to Expand / Collapse  Quote originally posted by sumit007 ...
please guide me how should i check whether a database named info exists or not? if it exists then perform update operation otherwise perform create database operation.
Simplest way would probably be to just try to open the database and see if it works.
Something like:
php Syntax (Toggle Plain Text)
  1. <?php
  2. $dbLink = new mysqli("host", "usr" ,"pwd");
  3. $dbName = "Test";
  4.  
  5. if(!$dbLink->select_db($dbName))
  6. {
  7. $sql = "CREATE DATABASE `{$dbName}`";
  8. $dbLink->query($sql);
  9.  
  10. $dbLink->select_db($dbName) or die("Failed to set or create database: " . $dbLink->error);
  11. }
  12.  
  13. // etc...
  14. ?>
Another way would be to use the SHOW DATABASES LIKE '$dbName' command and do a normal query to see if it is there.
Not sure which would be more efficient, but I suspect the former is, especially in situations where the database is likely to exist 99% of the time.

Although... I would question a design where you would have to create databases and tables on the fly. That sort of thing should generally be done before hand, and not be bloating your code.
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Oct 19th, 2009
0
Re: Different function for different buttons in php-mysql question
i m getting a problem.. i have created a database and table too. it gets created bt when i insert data into it, it shows databasename.tablename doesn't exists..
Please help me
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sumit007 is offline Offline
12 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Wild Card search not working...
Next Thread in PHP Forum Timeline: Help needed architecting a process





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC