i have enters some data using form which submit data in sql table1 like this,
Fields name in table -->> id First name Lastname Date
data saved -->> 1 user 1 2011-05-10
2 user 2 2011-05-11
3 user 3 2011-05-12
now i dont want to duplicate date in database let say while inserting data using form,
Firstname : ____________
Lasename : ____________
Date : ____________ (i dont want this date to b save if the date alredy exist in database,
it should prompt user "Date already exist and to edit click here
(this will be the link to that date which is already exist so that we can edit) " )
SAVE
i user files , form.php , insert.php(so insert values in database) ,
so tell me what function should i use to solve my problem....
SIMPLE DATABASE SEARCH
Moderator: General Moderators
Re: SIMPLE DATABASE SEARCH
So you basically want a whole form coding? A pretty unreasonable ask, to be honest.
I'll start you off:
1. When the user submits the form, do a WHERE query to see if the date exists (i.e. "SELECT date FROM your_table WHERE date = '".$datePostedFromForm.'").
2. Use mysql_num_rows() to see if there is an entry for that date (use an if statement, for example).
3. If not duplicate, insert data. If not, print the form and print the message you want to display to prompt them to edit.
Good luck.
I'll start you off:
1. When the user submits the form, do a WHERE query to see if the date exists (i.e. "SELECT date FROM your_table WHERE date = '".$datePostedFromForm.'").
2. Use mysql_num_rows() to see if there is an entry for that date (use an if statement, for example).
3. If not duplicate, insert data. If not, print the form and print the message you want to display to prompt them to edit.
Good luck.
Re: SIMPLE DATABASE SEARCH
Thanks for your sweet help i got your point but still little confused please help me in this code,
<?php
include ("connect.php"); // my db connection let say it connect sucessfully
$selecteddate = $_POST['selectdate'] ; // value comming from form
$result = mysql_query("SELECT database1 FROM table1
WHERE date='$selecteddate'"); // now i am confused here what to do next
if () // what should i write in this if statement
{
}
else
{
}
?>
<?php
include ("connect.php"); // my db connection let say it connect sucessfully
$selecteddate = $_POST['selectdate'] ; // value comming from form
$result = mysql_query("SELECT database1 FROM table1
WHERE date='$selecteddate'"); // now i am confused here what to do next
if () // what should i write in this if statement
{
}
else
{
}
?>
Re: SIMPLE DATABASE SEARCH
Code: Select all
if (mysql_num_rows($result) >= 1) // what should i write in this if statement
{
//Insert it
}
else
{
//Print Form with edit message
}
Note: You should escape your variables retrieve from the user ($_POST / $_GET) with mysql_real_escape_string().
Re: SIMPLE DATABASE SEARCH
o man thanks for your help but still i am in problem , u didnt get my point here i just want to only save a data which is new to database , let say if date is newer then it must save date in database otherwise display,
"date already exist <a href=" ?? what should i use link here ?? ">click here</a> to edit data of this date"
also tell me that u have given this code,
if (mysql_num_rows($result) >= 1)
it only search that result is >= 1 , but i want that date should be not duplicate again if it exist before...
"date already exist <a href=" ?? what should i use link here ?? ">click here</a> to edit data of this date"
also tell me that u have given this code,
if (mysql_num_rows($result) >= 1)
it only search that result is >= 1 , but i want that date should be not duplicate again if it exist before...