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..
wilch 5 Junior Poster in Training
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.
Edited by wilch because: [icode] tags missing
sumit007 0 Newbie Poster
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
diafol
Post your code and we'll have a look at it.
Atli 182 Posting Pro
Hey.
Simplest solution, just name all your submit buttons the same and do stuff based on the value.
Like, if you have this form:
<form action="process.php" method="post">
<input type="submit" name="button" value="Add">
<input type="submit" name="button" value="Delete">
</form>
You could do this:
<?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)
}
?>
diafol commented: never thought of doing that - nice one +3
aviralmsharma commented: Sweet, Simple and Works perfect ! +0
sumit007 0 Newbie Poster
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
$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)
?>
Edited by peter_budo because: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Atli 182 Posting Pro
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
$a = "Hello!";
function foo() {
echo $a; // Prints nothing. Variable $a doesn't exist in this scope.
}
function bar() {
global $a;
echo $a; // Prints: "Hello!"
}
?>
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!)
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 ;-]
Edited by mike_2000_17 because: Fixed formatting
sumit007 0 Newbie Poster
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.
Atli 182 Posting Pro
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
$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...
?>
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.
sumit007 0 Newbie Poster
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
Atli 182 Posting Pro
If the database and the table exist, then you are using incorrect names in your query. Make sure you are in fact using the proper names.
Post the code and we can help spot the problem.
Edited by Atli because: Spell-checker malfunction.
sumit007 0 Newbie Poster
the below mentioned code is of the add function which creates the database & table and the enter values in it.
function add()
{
// Create database
$con = mysql_connect("localhost","root","omomom");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (mysql_query("CREATE DATABASE record",$con))
{
echo "Database created</br>";
}
else
{
echo "Error creating database: " . mysql_error();
}
// Create table
mysql_select_db("record", $con);
$sql = "CREATE TABLE members
(
Name varchar(15),
FatherName varchar(15),
CollegeID varchar(10),
NetID varchar(10),
EmailID varchar(30)
Contactno varchar(10)
)";
$sql="INSERT INTO members (Name,FatherName,CollegeID,NetID,EmailID,Contactno)
VALUES
('$_POST[Text1]','$_POST[Text2]','$_POST[Text5]','$_POST[Text4]','$_POST[Text6]','$_POST[Text7]')";
echo "</br>added";
if (!mysql_query($sql,$con))
{
die('</br>Error: ' . mysql_error());
}
echo "</br>record added";
mysql_close($con);
}
when i execute it, it shows the error that record.members table doesn't exists..
Edited by peter_budo because: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Atli 182 Posting Pro
That is because you never actually execute the CREATE TABLE query, thus; it doesn't exist when you try to insert data into it.
This is what you do:
$sql = "CREATE TABLE tbl ...";
$sql = "INSERT INTO tbl"
mysql_query($sql) or die(mysql_error());
See the problem? Only the INSERT query gets executed, but not the CREATE TABLE query.
You need to do:
$sql = "CREATE TABLE tbl ...";
mysql_query($sql) or die(mysql_error());
$sql = "INSERT INTO tbl"
mysql_query($sql) or die(mysql_error());
sumit007 0 Newbie Poster
thanx my that prob. is done..
now guide me how this thing can be done?? i want to add some thing in a table and if there is a table of same fields(columns) that i want then add in that table otherwise create an another table of those fields and add data in it...
Atli 182 Posting Pro
It would be better if you post your new question in a new thread.
Generally, each thread is only meant to contain a single question. Once that question is solved, you mark the thread solved and move on to a new thread. Piling all you questions into a single thread gets confusion very fast.
sumit007 0 Newbie Poster
thanx a lot for helping me.. i'll b posting new thread very soon...
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.