Re: Unique entry + joined date

From: Date: Thu, 10 Aug 2000 08:03:27 +0000
Subject: Re: Unique entry + joined date
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-11085@lists.php.net to get a copy of this message
Oktay wrote:
Unique entry + joined date Greetings,
Howdy, Oktay
I am new with php and it is my first scripting experience. I am trying to code a simple script which will be a very basic URL directory. I would much appreciate if anybody could help me with my below challenges. 1. On the below code I don't know how to figure out to make url field unique in mysql.
(see SQL below)
That means there should only be a single copy of the URL in my database. 2.And also for the joined field could you recommend me a simple function that will print out the date so that it enters to mydatabase as yyyymmmdddhhhmmmss instead of 0000000000.
See the date() function. It can convert timestamps to the format you'd prefer. You don't need to format it before you enter it into your database. Here's an example: <?php print date("m/d/Y", time()); ?> You can replace time() with your datetime field that you retrieve from your table. See the documentation for formatting options. http://www.php.net/manual/function.date.php
3. Could you also please check my mysql database structure whether it is OK with mysql. I mean would a professional programmer do what I did or there is much better way to do it.
I don't consider myself a professional programmer. I'm just telling you what I'd do..
I am asking this since I would like to avoid problems in advance which may happen in the future with the MySql structure. Thank you in advance and best regards. Oktay <?
     if ((!$where) || (!$url)|| (!$title)|| (!$description)) {
     header("Location: http://www.myhost.com/add.cgi");
     exit;
     }
?>
You'll want to use trim() and empty() to check these. White-space has value. Someone could just hit the spacebar and it'd see it as non-empty. $where = trim($where); Then check for value with !empty($where) Personally, I use a function that takes a single value or array of the required fields as its argument. You can use is_array() to check to see if the supplied argument is an array or a single value.
<? /* CREATE TABLE directory ( ID int(11) DEFAULT '0' NOT NULL auto_increment, category text NOT NULL, url varchar(255) NOT NULL, title varchar(255) NOT NULL, description text NOT NULL, email varchar(50) NOT NULL, joined datetime, PRIMARY KEY (id) ); */ ?>
This works for me: CREATE TABLE directory ( ID int(11) DEFAULT '0' NOT NULL auto_increment, category text NOT NULL, url varchar(255) NOT NULL, title varchar(255) NOT NULL, description text NOT NULL, email varchar(50) NOT NULL, joined datetime, UNIQUE url (url), PRIMARY KEY (ID) ); You just wanted to make sure the url was unique, right? The above leaves your primary key as ID and makes it so that url can have no exact duplicates.
     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
     <HTML>
     <HEAD>
------8<------
     </BODY>
     </HTML>
Cheers, Brian

« previous php.general (#11085) next »