Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    71
    Thanks
    14
    Thanked 0 Times in 0 Posts

    LIMIT 1 in INSERT INTO query

    Hello there,

    I am getting double records from one script which is not suppose to happen. I thought putting LIMIT 1 at the end of the query would work but it says "Error" when I do it.

    Here is the queries:
    PHP Code:
    $insert=mysql_query("INSERT INTO `friends` (user,friend) VALUES ('$userid','$f[from]')") or die("Error 2");
    $insert2=mysql_query("INSERT INTO `friends` (user,friend) VALUES ('$f[from]','$userid')") or die("Error 3");

    $insert=mysql_query("INSERT INTO `f_requests` (`to`,`from`,`auth_key`) VALUES ('$friend','$userid','$auth')") or die("Error 3");
    $insert_mail=mysql_query("INSERT INTO `email` (`to`,`from`,`message`,`time`,`unix`,`subject`) VALUES ('$friend','$userid','You have a friend request from $friend. Click <a href=index.php?page=confirm&auth=$auth>here</a> to confirm.','$time','$unix','New Friend Request!')") or die("Error 4"); 
    They all do it and I'm not sure why. Would someone explain why or have a suggestion to fix it? Maybe even a web page to explain it.

  • #2
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,392
    Thanks
    67
    Thanked 102 Times in 101 Posts
    Something on your page is causing the double queries, it's not the queries themselves

    So this is giving you an error?

    PHP Code:
    $insert=mysql_query("INSERT INTO `friends` (user,friend) VALUES ('$userid','$f[from]') LIMIT 1") or die("Error 2"); 

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    LIMIT is not a valid clause for an INSERT statement. You "limit" the number of inserts by how many times you run the query(or how many sets of values if you are running a multiple insert). myfayt is correct, if you are getting multiple rows you are inserting the row more than once in your code.

  • #4
    New Coder
    Join Date
    Jun 2010
    Posts
    71
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by myfayt View Post
    Something on your page is causing the double queries, it's not the queries themselves

    So this is giving you an error?

    PHP Code:
    $insert=mysql_query("INSERT INTO `friends` (user,friend) VALUES ('$userid','$f[from]') LIMIT 1") or die("Error 2"); 
    Yes that does give me an error.


    So what you both are saying is it just feels like it I guess?

  • #5
    Regular Coder
    Join Date
    Jun 2010
    Posts
    290
    Thanks
    63
    Thanked 8 Times in 8 Posts
    No. The INSERT query you have posted here will insert one row into the database. Log into MySQL at the command line and try it directly. I recommend putting trace into your code before/after the query and see how many times the trace is printed. It's not the MySQL that's at fault, there's an error in the PHP code. My rule of thumb for programming is that 99.99999% of the time, the error is mine and not a bug in the language!

  • #6
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,392
    Thanks
    67
    Thanked 102 Times in 101 Posts
    It's something in your script causing it to do double queries, such as a while loop, a foreach, loading two like functions, could be anything. It's not a mess up in the query.

    Post your whole code, or else the part that is causing the problem, and we can help you better.

  • #7
    New Coder
    Join Date
    Jun 2010
    Posts
    71
    Thanks
    14
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php
    session_start
    ();
    require_once 
    'database.php';
    if(isset(
    $_SESSION['user']))
    {
    $userid $_SESSION['user'];
    $auth $_GET['auth'];
    $sql=mysql_query("SELECT * FROM `f_requests` WHERE `auth_key`='$auth'") or die("Error 1");
    if(
    mysql_num_rows($sql)==1)
    {
    $f=mysql_fetch_assoc($sql);

    $insert=mysql_query("INSERT INTO `friends` (user,friend) VALUES ('$userid','$f[from]') LIMIT 1") or die("Error 2");
    $insert2=mysql_query("INSERT INTO `friends` (user,friend) VALUES ('$f[from]','$userid')") or die("Error 3");
                      











    }else{
    echo 
    'That request does not exist.';
    }




    }else{
    echo 
    'You must be logged in to view this page.';
    }
    Here is the full page, there is just a few IF statements, no loops,whiles,foreaches.

  • #8
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,392
    Thanks
    67
    Thanked 102 Times in 101 Posts
    Why are you using two INSERT queries for the same table, same thing? both should be made into one query.

    Also for the double query, I am betting it falls into this if(mysql_num_rows($sql)==1)
    {

  • #9
    New Coder
    Join Date
    Jun 2010
    Posts
    71
    Thanks
    14
    Thanked 0 Times in 0 Posts
    How can I do the double query? Like this?
    PHP Code:
    ("INSERT INTO `friends` (user,friend) VALUES ('$userid','$f[from]') AND (user,friend) VALUES ('$f[from]','$userid')"
    Just a guess...


    Also, so if you think the num_rows is the cause, what could I do to replace it?

  • #10
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Something like this
    PHP Code:
    $query "INSERT INTO `friends` (user,friend) VALUES ('$userid','$f[from]'),('$f[from]','$userid')"
    See the INSERT syntax manual page for more info(about a page and a half down it's discussed).


  •  

    Posting Permissions

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