...

View Full Version : double query help



bijaybd
02-10-2010, 06:00 PM
can body help me how to stop double query insert in sql how to stop double query in sql .....


$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.

angst
02-10-2010, 06:04 PM
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");

bijaybd
02-10-2010, 06:25 PM
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 -

$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

sitNsmile
02-10-2010, 06:58 PM
for entering data into the db, i'd use something more organized like a setup like this.


// 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

bijaybd
02-10-2010, 07:31 PM
here is my full page



<?

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.

sitNsmile
02-10-2010, 07:48 PM
You could declare it using sessions.


<?

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!";

}

?>

bijaybd
02-10-2010, 09:05 PM
i am using session already for login

sitNsmile
02-10-2010, 09:19 PM
i am using session already for login

you can use multiple sessions. as many as you'd like.

bijaybd
02-10-2010, 10:29 PM
but when i use the sesson it does not work so i also try to do like this -



$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

bijaybd
02-11-2010, 12:57 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum