| | |
Different function for different buttons in php-mysql question
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 10
Reputation:
Solved Threads: 1
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..
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..
•
•
Join Date: Aug 2007
Posts: 55
Reputation:
Solved Threads: 9
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
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:
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.
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 ?
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:
You could do this:
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)
<form action="process.php" method="post"> <input type="submit" name="button" value="Add"> <input type="submit" name="button" value="Delete"> </form>
php Syntax (Toggle Plain Text)
<?php if($_POST['button'] == "Add") { // Do the add stuff } else if($_POST['button'] == "Delete") { // Do the delete stuff } else { // Default action. // This would happen if the page was requested directly, bypassing the form. // Or, if the form was submitted using the enter key. (In most browsers) } ?>
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
And use [code] tags!
•
•
Join Date: Oct 2009
Posts: 10
Reputation:
Solved Threads: 1
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.
Also guide me how can i make a check to check whether database of given name exists or not.
php Syntax (Toggle Plain Text)
<?php $con = mysql_connect("localhost","root","omomom"); if (!$con) { die('Could not connect: ' . mysql_error()); } if($_POST[Button1]=="ADD") { add(); } else if($_POST[Button1]=="CANCEL") { cancel(); } else if($_POST[Button1]=="DELETE") { delete(); } else if($_POST[Button1]=="PREVIOUS") { previous(); } else if($_POST[Button1]=="NEXT") { next(); } function add() { // Create database if (mysql_query("CREATE DATABASE info123",$con)) { echo "Database created"; } else { echo "Error creating database: " . mysql_error(); } // Create table mysql_select_db("info123", $con); $sql = "CREATE TABLE members ( Name varchar(15), Father'sName varchar(15), CollegeID varchar(10), NetID varchar(10), Email-ID varchar(30) )"; $sql="INSERT INTO info123 (Name,Father'sName,CollegeID,NetID,Email-ID) VALUES ('$_POST[Text1]','$_POST[Text2]','$_POST[Text5]','$_POST[Text4]','$_POST[Text6]')"; /*if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }*/ echo "record added"; } mysql_close($con) ?>
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)
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
It would be better, in your case, to just open the connection inside the function, rather then outside it.
#2 The names
If you want to use them in your query, you need to enclose them in back-ticks. (Note these are NOT single-quotes!)
#3 You don't actually execute either your
Although, you do have the
#4
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 ;-]
#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)
<?php $a = "Hello!"; function foo() { echo $a; // Prints nothing. Variable $a doesn't exist in this scope. } function bar() { global $a; echo $a; // Prints: "Hello!" } ?>
#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)
CREATE TABLE `example`( Field-Name ... -- This is illegal and will cause an error `Field-Name` ... -- This is OK );
#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!
And use [code] tags!
0
#9 Oct 18th, 2009
•
•
•
•
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.
Something like:
php Syntax (Toggle Plain Text)
<?php $dbLink = new mysqli("host", "usr" ,"pwd"); $dbName = "Test"; if(!$dbLink->select_db($dbName)) { $sql = "CREATE DATABASE `{$dbName}`"; $dbLink->query($sql); $dbLink->select_db($dbName) or die("Failed to set or create database: " . $dbLink->error); } // etc... ?>
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!
And use [code] tags!
![]() |
Similar Threads
- Looking for PHP / MySQL Developer (Web Development Job Offers)
- Looking for a PHP/MYSQL Programmer (Web Development Job Offers)
- Need Highly Experienced PHP/MySQL Programmer $15-$25/hour (Web Development Job Offers)
- PHP/MySQL Programmer Position (Web Development Job Offers)
Other Threads in the PHP Forum
- Previous Thread: Wild Card search not working...
- Next Thread: Help needed architecting a process
| Thread Tools | Search this Thread |
.htaccess ajax apache api array back basic beginner binary broken cakephp checkbox class cms code computing cron curl customizableitems database date delete display dynamic echo email error file files filter folder form forms function functions gc_maxlifetime google host href htaccess html image include insert integration ip java javascript joomla limit link login loop mail memmory memory menu mlm mod_rewrite multiple mysql navigation oop parsing paypal pdf php problem query radio random recursion regex remote script search server sessions sms snippet soap source space sql syntax system table thesishelp trouble tutorial update upload url validation validator variable video web xml youtube






