Different function for different buttons in php-mysql question

Thread Solved

Join Date: Oct 2009
Posts: 10
Reputation: sumit007 is an unknown quantity at this point 
Solved Threads: 1
sumit007 sumit007 is offline Offline
Newbie Poster

Different function for different buttons in php-mysql question

 
0
  #1
Oct 15th, 2009
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..
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 55
Reputation: wilch is an unknown quantity at this point 
Solved Threads: 9
wilch wilch is offline Offline
Junior Poster in Training
 
0
  #2
Oct 15th, 2009
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
umm.. by the way how do you do it ?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: sumit007 is an unknown quantity at this point 
Solved Threads: 1
sumit007 sumit007 is offline Offline
Newbie Poster
 
0
  #3
Oct 16th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,030
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 131
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster
 
-1
  #4
Oct 16th, 2009
Post your code and we'll have a look at it.
Happy Humbugging Christmas
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 443
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training
 
2
  #5
Oct 16th, 2009
Hey.

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

Like, if you have this form:
  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:
  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. ?>
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: sumit007 is an unknown quantity at this point 
Solved Threads: 1
sumit007 sumit007 is offline Offline
Newbie Poster
 
0
  #6
Oct 17th, 2009
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.


  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)
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 443
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training
 
0
  #7
Oct 17th, 2009
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.
  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!)
  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 ;-]
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: sumit007 is an unknown quantity at this point 
Solved Threads: 1
sumit007 sumit007 is offline Offline
Newbie Poster
 
0
  #8
Oct 18th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 443
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training
 
0
  #9
Oct 18th, 2009
Originally Posted by sumit007 View Post
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:
  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.
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: sumit007 is an unknown quantity at this point 
Solved Threads: 1
sumit007 sumit007 is offline Offline
Newbie Poster
 
0
  #10
Oct 19th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC