Page 1 of 1

SIMPLE DATABASE SEARCH

Posted: Tue May 10, 2011 1:28 pm
by shinstar
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....

Re: SIMPLE DATABASE SEARCH

Posted: Tue May 10, 2011 1:31 pm
by oscardog
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.

Re: SIMPLE DATABASE SEARCH

Posted: Tue May 10, 2011 1:53 pm
by shinstar
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
{

}


?>

Re: SIMPLE DATABASE SEARCH

Posted: Tue May 10, 2011 2:57 pm
by oscardog

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
}

Before I look like a noob to anyone reading, I wouldn't recommend doing it like this but it's just easier to explain than setting variables and stuff.

Note: You should escape your variables retrieve from the user ($_POST / $_GET) with mysql_real_escape_string().

Re: SIMPLE DATABASE SEARCH

Posted: Wed May 11, 2011 1:09 am
by shinstar
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...