SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2005
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Duplicate Values..more complicated

    Alright, so here is what I'm trying to do:

    I have two tables...one receives all the information regarding the subject and the other table holds image locations of images that the person is able to upload. The first table has a contract number, location, and date. The second table has the location of the image that they can upload (using a form where they input their contract number, location, date, and up to 3 pictures), and associates the three pictures all to the same contract number.

    Now, on the first table, there is a unique ID on the contract number that won't allow anyone to submit the same contract twice. But, the second table does NOT have this restriction on the contract number since duplicate contract numbers will be used to associate the pictures to the contract number.

    The Problem (BARE WITH ME, ALMOST OVER):
    But now, if someone goes into the form, enters in the same contract number as something that already exists in the first table, it will not input this into the first table, but will input the pictures they upload into the second table under that contract number. Therefore, instead of ending up with only 3 pictures per contract number, it can become an infinite amount.

    PLEASE HELP ME!!!!! Thank you.

    ps - i use two separate INSERT INTO functions to input information into each of the respective tables. So I need to have it that if the contract number is a duplicate in the first table, it won't upload the images into the second table and will skip the second INSERT INTO function.

  2. #2
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's just an upload form, so don't go out of your way to be super efficient. Just script it with an extra call to the database.
    PHP Code:
    $sql "SELECT COUNT(*) AS cnt FROM second_table WHERE contract_number = {$_POST['contract_number']}";

    $res mysql_query($sql);

    $row mysql_fetch_assoc($res);

    if(
    $row['cnt'] < 3)
    {
        
    //Go ahead and insert the pic
    }else
    {
        die(
    "Big Dummy!");


  3. #3
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    before you execute the second insert into you should check whether the first one executed
    PHP Code:
    $result1 mysql_query('insert into');
    if(
    $result1 && mysql_affected_rows()>0)
    {
      
    $result2 mysql_query('2nd_insert_into');

    the other way is you run a select statement first to see if the contact already exists. if it does not then insert else dont
    PHP Code:
    $result1 mysql_query("select something from table where contact='contact'");
    if(
    $result1 && mysql_num_rows($result1)>0)
    {
      
    //DO NOT INSERT
    }
    else
    {
     
    //DO THE INSERT QUERIES

    hope this helps
    ---------------------------
    Errors = Improved Programming.
    My Site

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2005
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey jaswinder & hardcoded,

    thanks for your quick replies....
    sometimes when you work on something for too long, u miss the most obvious answers...

    jaswinder, i dont know why i didnt think of checking if the contract exists in the first table and if it did, to give an error message and ask user to go back and try again.

    hardcoded, i think thats wat u meant as well.

    Do either one of u know if there is an issue with using the following (this is wat I ended up using):

    $checkduplicate = "SELECT * FROM locationclaims WHERE contractnum='$ContractNum'";
    if ($r=mysql_query($checkduplicate)) {

    while ($row=mysql_fetch_array($r)) {

    if($row['contractnum']==$ContractNum)
    {
    echo "This contract already exists in the database. Please go back and try again.";
    echo("<input type='button' value='Return to Form' onClick='history.go(-1)'>");
    exit();
    }
    }
    }

    Is there any harm in using the "onclick='history.go(-1)'"? I can't use headers to redirect since I have already posted things by this part of the php. Thoughts? Thanks agaiN!!!

  5. #5
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use history.back(-1) and good idea because then you user don't have to fill values again.

    when you are only checking to see if the contact num exists then DONT USE *. it just makes it slower.
    suppose your table has
    contactnum | contactname | blah

    thentry
    select contactname from table where contactnum=$contactnum;

    its just a lot faster than using * and specially in ths case where you only wanna check if sonmething exists or not.

    DONT pust quotes around $contactnum(i am assuming its int, or tinyint BUT not string)
    mysql can let you do that but some other databases don't allow that. so, good idea to stcik with proper sql.
    ---------------------------
    Errors = Improved Programming.
    My Site

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2005
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey thanks jaswinder,

    yeah, ur right, i shouldnt use the *...would probably slow things down when the database gets big, as you said.

    as for pushing the quotes...i have to because the contactnum is alpha-numeric

    kinda like:
    CONTRACT NUMBER = FX34948FA

    Wouldnt I have to use quotes?

  7. #7
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    YES you have to use quotes.
    in my last post, i said, i am assuming its int. so, if its not then you don't need it
    ---------------------------
    Errors = Improved Programming.
    My Site


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •