General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old August 26th, 2003, 11:17 PM
littleblackdog littleblackdog is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Anchorage
Posts: 118 littleblackdog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Please help with db design...

I posted a question on db design, and a problem I was having with duplicate entry... here is a post by Laidback with help...


" The primary problem you have is due to poor database design. You need to have a primary key set or even a non-primary, but unique index. If you have this then your data will not be duplicated. You would get an error on this page, but you will not have duplicate data. You can fix the error simply...All you need to do is create a function that checks if that key exists in the database and if it does exist craft a friendly error messages and kindly direct the user on how to re-input their data and resubmit.
"

Now I am posting the code for one of the tables to see how it should be changed:

"CREATE TABLE teachnews ( id smallint(4) unsigned NOT NULL AUTO_INCREMENT,
title varchar (50) NOT NULL,
date varchar (50) NOT NULL,
body text NOT NULL,
message1 varchar (50) NOT NULL,
message2 varchar (50) NOT NULL,
PRIMARY KEY (id), UNIQUE KEY (id));"

and here is the code for entering data into this table:

[php]<?php
session_start();
<?php
$result = mysql_query("SELECT * FROM teachs WHERE teach = '$foldername' ",$connection);
while ($myrow = mysql_fetch_array($result)) {
printf("<a href=\"%s?id=%s\">%s</a> \n", $PHP_SELF, $myrow["id"], $myrow["title"]);
printf("<a href=\"%s?id=%s&delete=yes\">(DELETE)</a><br>", $PHP_SELF, $myrow["id"]);
echo "<hr>";
}
?>
</p>
<p>&nbsp;</p></td>
</tr>
<tr>
<td colspan="6" valign="top"> <p>
<?php
@mysql_select_db($db_name,$connection);
if ($submit) {
// here if no ID then adding else we're editing
if ($id) {
$sql = "UPDATE teachs SET title ='$title',date='$date',body='$body',popup='$popup' ,popuptext='$popuptext', teacher='$foldername' WHERE id=$id";
} else {
$sql = "INSERT INTO teachs (title,date,body,popup,popuptext,teacher) VALUES ('$title','$date','$body','$popup','$popuptext','$ foldername')";
}
// run SQL against the DB
$result = mysql_query($sql);
echo "Record updated/edited!<p>";
} elseif ($delete)
{
// delete a record
$sql = "DELETE FROM teachs WHERE id=$id";
$result = mysql_query($sql);
echo "$sql Record deleted!<p>";
} else {
// this part happens if we don't press submit
if (!$id) {
// print the list if there is not editing

}
?>
<form method="post" action="<?PHP echo $PHP_SELF?>">
<?PHP
if ($id) {
// editing so select a record
$sql = "SELECT * FROM teachs WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$id = $myrow["id"];
$title = $myrow["title"];
$date = $myrow["date"];
$body = $myrow["body"];
$popup = $myrow["popup"];
$popuptext = $myrow["popuptext"];
// print the id for editing
?>
<p>
<input type=hidden name="id" value="<?PHP echo $id ?>">
<?PHP
}
?>
</p>
Title of News:
<input type="Text" name="title" value="<?PHP echo $title ?>">
<br>
<br>
Date Assigned:
<input type="Text" name="date" value="<?PHP echo $date ?>">
<br>
<br>
Message: ( Important! You must user 'Enter' key to go to next line. Else
the lines on the page will <br>
be VERY long. )<br>
<font size="2">( If you want to enter a web address into this message
box, try <em><strong>this</strong></em>: <br>
Enter your normal text, then this script: <font color="#0000FF"><a
href ="http://</font></font><font color="#FF0000" size="2">www.yourwebpage.com</font><font color="#0000FF" size="2">"></font><font color="#FF0000" size="2">YourWebName</font><font color="#0000FF" size="2"></a>
</font><font size="2"><br>
Where it says "<font color="#FF0000">yourwebpage.com</font>"
you put in the address of the site (www.google.com) and where it<br>
says "<font color="#FF0000">YourWebName</font>" you replace
with the title you want readers to see (Google). The <font color="#0000FF">blue
</font>type is HTML code!<br>
If you want to know how to change colors, use italics, and input email
links, view the Instructions / Help page.)</font> <br>
<TEXTAREA name="body" wrap cols=80 rows=10><?PHP echo $body ?></TEXTAREA>
<br>
If you wish to add more text to your Quick News, <br>
either check the radio button to add More Info. to <br>
a popup window, then include the text in the text box below.<br>
<input name="popup" type="radio" value="yes">
<br>
Add the extra long text that will apear in the popup window.<br>
<TEXTAREA name="popuptext" wrap cols=80 rows=10><?PHP echo $popuptext ?></TEXTAREA>

<input type="hidden" name="teacher" value= "<?php echo "$teachername"; ?>" >
<input type="Submit" name="submit" value="Enter information">
<a href="<?PHP echo $PHP_SELF?>">Clear and Refresh</a>
</form>
<p>&nbsp;</p>
<?PHP
}
?></p>[php]

Laidback described a way to change the script to check for an existing entry, and if so, refresh? Please help.
__________________
bow wow!

Reply With Quote
  #2  
Old August 27th, 2003, 06:20 PM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
In the teachnews table you have: PRIMARY KEY (id), UNIQUE KEY (id)

That is redundant since a primary key is already unique.
__________________
__________________________________________________ _
Wil Moore III, MCP | Integrations Specialist | Senior Consultant
Are You Listed...? | DigitallySmooth Inc.

Reply With Quote
  #3  
Old August 27th, 2003, 06:26 PM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
Quote:
Laidback described a way to change the script to check for an existing entry, and if so, refresh? Please help.
I see you have a column described as: title varchar (50) NOT NULL

This is a good candidate for a unique key
You could also use the body text, but I think title is just as good, and likely would be less costly to query and compare on.

It is quite simple to check for an existing value. Just run a query something like the following:

SELECT title from teachnews WHERE title=$_POST['title'];

Just an idea. You will have to tighten it up for yourself. I suspect this will be all you need though... You can check this query by using the mysql_num_rows function.

This would be a duplicate item if that function returns a positive number ( > 0 ).

Reply With Quote
  #4  
Old August 28th, 2003, 05:56 PM
littleblackdog littleblackdog is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Anchorage
Posts: 118 littleblackdog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
where would it go?

I have an "if" statement at the beginning for authentication that I didn't display, and one further down for "submit",

Where would you suggest I put in the query? I need a little more explaination.

Thanks laidback, I almost have it...


lbd

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Please help with db design...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway