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
    Regular Coder
    Join Date
    Nov 2008
    Posts
    103
    Thanks
    4
    Thanked 0 Times in 0 Posts

    double query help

    can body help me how to stop double query insert in sql how to stop double query in sql .....
    Code:
    $sql = mysql_query("INSERT INTO `chatmsg` VALUES(0, '" . $idtat . "', '" . $adt . "', '" . $welcomemsg . "', '$rk', '" . date("H:i") . "'," . time() . ", '', '1');");
    }
    @mysql_query("insert into `buddylist` values(0, '$moo', '".time()."', '".time()."', '".$user['login']."', 'No', '".$ci."', '".$ap."');"));
    like same query will not be added two times.

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    how is is getting inserted twice? someone clicking a link/submit button twice? or perhaps refreshing the page?

    simplest way is to redirect the browser after the insert.

    header("location: SomeOtherPage.php");

  • #3
    Regular Coder
    Join Date
    Nov 2008
    Posts
    103
    Thanks
    4
    Thanked 0 Times in 0 Posts
    someone clicking a link/submit button twice yes click twice and sending a lot of queries just like add > next page back to add page and again doing same someone please help me

    ok let me clear all of it -

    this is the sql query which is going 2 times 3 times 4 times if the page get refresh -
    Code:
    $query_users = @mysql_query("select * from `chat_users` where login='".$moo."';");
    $data = @mysql_fetch_array($query_users);
    $rk = $data['id'];
    $idtat = "4505";
    $adt ="System";
    $welcomemsg="u have a friend request from $login at: " . $time . "";
    if($am=="z")
    {
    $sql = mysql_query("INSERT INTO `chatmsg` VALUES(0, '" . $idtat . "', '" . $adt . "', '" . $welcomemsg . "', '$rk', '" . date("H:i") . "'," . time() . ", '', '1');");
    }
    if(@mysql_query("insert into `buddylist` values(0, '$moo', '".time()."', '".time()."', '".$user['login']."', 'No', '".$ci."', '".$ap."');"));
    echo "A request has been sent to $moo<br/>";
    now if the user go back and by back button and click add as a friend it send same query again .... i want it to be by refreshing or going back and clicking the same button will not add double query again to sql and message like you have already requested
    Last edited by bijaybd; 02-10-2010 at 06:53 PM.

  • #4
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    for entering data into the db, i'd use something more organized like a setup like this.

    PHP Code:
    // my idea of organizing stuff into the database.. 
    $var_field1 "stuff"//either you write it
    $var_field2 $ci//or your own var
    $var_field3 time();// how about time?
    $var_field4 date("H:i");//anotehr one

    $sql mysql_query("INSERT into chatmsg
            set field1 = '$var_field1',
            field2 = '$var_field2',
            field3 = '$var_field3',
            field4 = '$var_field4'"
    );

    //redirec those users off this page NOW
    header('Location: index.php');
    exit;
    //kill it 

  • #5
    Regular Coder
    Join Date
    Nov 2008
    Posts
    103
    Thanks
    4
    Thanked 0 Times in 0 Posts
    here is my full page

    Code:
    <?
    
    include "session.php";
    include "dgm.php";
    
    wml_header();
    $nocache = rand(10000, 99999);
    
    $id = intval($_SESSION['id']);
    $pass = mysql_escape_string($_SESSION['pass']);
    $q = mysql_query("SELECT * FROM `chat_users` WHERE `id` = '".$id."' AND `pass` = '".$pass."' LIMIT 1;");
    if(mysql_num_rows($q) == 0)
    {
    echo "Not logged in!<br/>\n";
    echo "</small>";
    echo "</p></card></wml>";
    ob_end_flush();
    exit();
    }
    $user = mysql_fetch_assoc($q);
    $login = $user['login'];
    //////////
    echo "<card title=\"Add To Friend List\">";
    echo "<p align=\"$align\">";
    echo "<small>";
    ///////////
    
    $qr = mysql_fetch_array(mysql_query("SELECT COUNT(`id`) FROM `buddylist` where `name`='$login' AND `test`='$moo'"));
    
    if ($qr[0]!=0) {
    echo "You have already send a friend request.<br/>";
    
    
    if($rid) print "<a href=\"room.php?".SID."&amp;nocache=$nocache&amp;rid=$rid&amp;nocache=$nocache\">&#171;Chatroom</a><br/>";
    
    print "<a href=\"usercp.php?".SID."&amp;nocache=$nocache\">&#171;User Menu</a><br/>";
    
    echo "<a href=\"hall.php?".SID."&amp;nocache=$nocache\">Back To Main</a><br/>";
    echo "</small>";
    echo "</p></card></wml>";
    ob_end_flush();
    exit();
    }
    else
    {
    
    $query_users = @mysql_query("select * from `chat_users` where login='".$moo."';");
    $data = @mysql_fetch_array($query_users);
    $rk = $data['id'];
    $idtat = "4505";
    $adt ="System";
    $welcomemsg="u have a friend request from $login at: " . $time . "";
    if($am=="z")
    {
    //$sql = mysql_query("INSERT INTO `chatmsg` VALUES(0, '" . $idtat . "', '" . $adt . "', '" . $welcomemsg . "', '$rk', '" . date("H:i") . "'," . time() . ", '', '1');");
    }
    //if(@mysql_query("insert into `buddylist` values(0, '$moo', '".time()."', '".time()."', '".$user['login']."', 'No', '".$ci."', '".$ap."');"));
    echo "[page updating]A request has been sent to $moo<br/>";
    
    
    
    }
    
    if($rid) print "<a href=\"room.php?".SID."&amp;nocache=$nocache&amp;rid=$rid&amp;nocache=$nocache\">&#171;Chatroom</a><br/>";
    
    print "<a href=\"usercp.php?".SID."&amp;nocache=$nocache\">&#171;User Menu</a><br/>";
    
    echo "<a href=\"hall.php?".SID."&amp;nocache=$nocache\">Back To Main</a><br/>";
    
    //////
    echo "<br/>$copyright";
    wml_foot();
    @mysql_close();
    ob_end_flush();
    ?>
    but still cant make it work so anybody can suggest me .... redirectly works but if the guy press back button then the queries entering agian so by this way they can create floods.

  • #6
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    You could declare it using sessions.

    PHP Code:
    <?

    if (!$_SESSION['already']) {

    echo 
    "You have not been here yet!";
    //to record if they've been here
    $_SESSION['already'] = TRUE;    

    } else {
        
    echo 
    "You've already been here!";
        
    }

    ?>

  • #7
    Regular Coder
    Join Date
    Nov 2008
    Posts
    103
    Thanks
    4
    Thanked 0 Times in 0 Posts
    i am using session already for login

  • #8
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by bijaybd View Post
    i am using session already for login
    you can use multiple sessions. as many as you'd like.

  • #9
    Regular Coder
    Join Date
    Nov 2008
    Posts
    103
    Thanks
    4
    Thanked 0 Times in 0 Posts
    but when i use the sesson it does not work so i also try to do like this -

    Code:
    $cou = mysql_query("SELECT `test` FROM `buddylist` where `name`='$login' order by id desc LIMIT 1");
    
    
    if($cou[0]=="$moo[0]") {
    echo "error"<br/>";
    }
    which is selecting the test name from sql and if the moo name = test name then sql query stops but ...... still its also not working for me i know may be i am doing a silly mistake but i am not getting it
    still does not work at all

  • #10
    Regular Coder
    Join Date
    Nov 2008
    Posts
    103
    Thanks
    4
    Thanked 0 Times in 0 Posts
    ok i was making a silly mistake anyways i have done it thanks all for the support --- i also need one more helping make a lottery system using rand function --- so i can able to create the functions and rand numbers but the problem is .... how i will select 3 winners random for example 1 sinner will get 1000 rand so his posts will be autometicaly 1000+posts second one rand 7000+posts 3rd one rand 5000+posts and also this will be automaticaly every week and when the prizeses will be done all the lottery data will be delete and start new one --- sorry for my bad english guys still i am noob and learning gives hours for this but bad english so lil slow


  •  

    Posting Permissions

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