Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Help with php/mysql if tag already exist
Old 06-13-2010, 08:03 AM Help with php/mysql if tag already exist
ZoC
Skilled Talker

Posts: 68
Trades: 0
hello there, hope someone can help me with this ...

PHP Code:
<?php
 $dbtable  
'atx_tags';

 
$db_conn  mysql_connect($dbhost$dbuser$dbpass) or die ('Error connecting to mysql');
 
$db_found mysql_select_db($dbname);

 if(isset(
$_POST['submit'])){
  
$Tag       $_POST['Tag'];
  
$Date      date('Y-m-d');

  
$query "INSERT INTO " $dbtable " (Tag,Date) VALUES ('$Tag','$Date')";
  
mysql_query($query);

 }

?>

<form action="" method="post">
 <input name="Tag" class="input" type="text"><br>
 <input type="submit" name="submit" class="input2" value="Submit">
</form>
how i can make if Tag is already added on atx_tags to dont add again and to show echo error video already added ! ... hope someone can help me with this ...
ZoC is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-13-2010, 10:33 AM Re: Help with php/mysql if tag already exist
Junior Talker

Posts: 3
Trades: 0
Run a query to see if it exists in the table and if not display an error otherwise insert the video.

Also, you need to secure your inputs. You're inserting unvalidated data which is dangerous!
mjo1337 is offline
Reply With Quote
View Public Profile
 
Old 06-13-2010, 10:56 AM Re: Help with php/mysql if tag already exist
ZoC
Skilled Talker

Posts: 68
Trades: 0
hello there, can you give me the full command for that becouse im kinda noob with this ... and about secure just me i have access to add the tags
ZoC is offline
Reply With Quote
View Public Profile
 
Old 06-13-2010, 02:47 PM Re: Help with php/mysql if tag already exist
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
With mysql, you can use the "ON DUPLICATE KEY" keyword:
http://dev.mysql.com/doc/refman/5.0/...duplicate.html
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-13-2010, 03:22 PM Re: Help with php/mysql if tag already exist
ZoC
Skilled Talker

Posts: 68
Trades: 0
hello there, i try this command but still not working ... hope someone can tell me what is the problem ...

Code:
 if(isset($_POST['submit'])){
  $Tag       = $_POST['Tag'];
  $Date      = date('Y-m-d');
  $Color     = $color_tag;


  function tagExists($Tag)
  {
    $query = "SELECT TID FROM " . $dbtable . " WHERE Tag = " . $Tag . " LIMIT 1";
    mysql_query($query);
    return $tagExists;
  }
  
  if ($tagExists) {
   echo 'Tag Already Exist';
  } else {
   $query = "INSERT INTO " . $dbtable . " (Tag,Date,Color) VALUES ('$Tag','$Date','$Color')";
   mysql_query($query);
   echo 'Tag Added';
  }
 }
ZoC is offline
Reply With Quote
View Public Profile
 
Old 06-13-2010, 04:40 PM Re: Help with php/mysql if tag already exist
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Given that "Tag" is your primary key, change the insert query to:
PHP Code:
$query "INSERT INTO " $dbtable " (Tag,Date,Color) VALUES ('$Tag','$Date','$Color') ON DUPLICATE KEY UPDATE date=now(), Color=$color"
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Help with php/mysql if tag already exist
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.81116 seconds with 12 queries