php inserting into mysql table tricky question
Posted: Fri Feb 17, 2006 3:16 pm
This has been driving me nuts for 2 weeks now, the below code works 95% of the time the other 5% of the time
this part "// add payed users" doesnt get inserted anyone can think of why it wouldn't more importanly how to resolve this
here is the full code
this part "// add payed users" doesnt get inserted anyone can think of why it wouldn't more importanly how to resolve this
Code: Select all
if ($uscnt[0]>=$users_in_train)
{
mysql_query("insert into trains (intId) values ('')");
$trainid[0]++;
// add payed users
$res = mysql_query("select intMSId from users where intPriority>0");
while ($arr = mysql_fetch_array($res))
{
mysql_query("insert into aboard (intTrainId, intMSUserId) values ($trainid[0], $arr[0])");
}
}Code: Select all
if (isset($_POST[Add]))
{
// check if id is integer
if (!is_numeric($_POST[intMSUserId]))
$error = "Invalid MySpace User ID";
if (is_blocked($_POST[intMSUserId]))
$error = "Your User ID has been blocked from joining the train.";
if ($error =="")
{
// get latest train id
$res = mysql_query("select MAX(intId) from trains");
$trainid = mysql_fetch_array($res);
// check users in the train
$res = mysql_query("select COUNT(*) from aboard where intTrainId='$trainid[0]'");
$uscnt = mysql_fetch_array($res);
if ($uscnt[0]>=$users_in_train)
{
mysql_query("insert into trains (intId) values ('')");
$trainid[0]++;
// add payed users
$res = mysql_query("select intMSId from users where intPriority>0");
while ($arr = mysql_fetch_array($res))
{
mysql_query("insert into aboard (intTrainId, intMSUserId) values ($trainid[0], $arr[0])");
}
}
// check if id is already in train
$res = mysql_query("select * from aboard where intTrainId=$trainid[0] and intMSUserId='$_POST[intMSUserId]'");
$intrain = mysql_fetch_array($res);
if ($intrain[0] == "")
{
// check if user data is loaded from myspace
//$res = mysql_query("select * from users where intMSId='$_POST[intMSUserId]'");
//$msuser = mysql_fetch_array($res);
// load data from myspace
//if ($msuser[0] == "")
//{
// http://profile.myspace.com/index.cfm?fu ... dID=426705
$html = file_get_contents("http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&friendID=$_POST[intMSUserId]");
preg_match("/<span class=\"nametext\">(.*)<\/span>/", $html, $msname);
preg_match("/<td class=\"text\" width=\"193\" bgcolor=\"#ffffff\" height=\"75\" align=\"left\">\"(.*)\"<br>/", $html, $msdata);
preg_match("/has <span class=\"redbtext\">(\d+)<\/span> friends/", $html, $msfc);
preg_match("/(\d+) years old/", $html, $msyo);
preg_match("/ctl00_Main_ctl00_.*<img src=\"(.*)\" border=\"0\" \/><\/a>/", $html, $msimg);
preg_match('#<\s*br(?:\s*/)?>\s*((?:fe)?male)\s*<\s*br(?:\s*/)?>#is',$html,$sex);
$msdata[1] = mysql_escape_string($msdata[1]);
$msname[1] = mysql_escape_string($msname[1]);
if ($msname[1] != "")
{
mysql_query("delete from users where intMSId='$_POST[intMSUserId]'");
$res = mysql_query("insert into users (intMSId, txtMSName, intMSAge, intMSFriends, txtMSText, txtMSImg, sex) values ('$_POST[intMSUserId]', '$msname[1]', '$msyo[1]', '$msfc[1]', '$msdata[1]', '$msimg[1]', '$sex[1]')");
$res = mysql_query("select * from users where intMSId='$_POST[intMSUserId]'");
$msuser = mysql_fetch_array($res);
} else
{
$error = "User doesnt exists";
}
//}
// add user into train
if ($error == "")
{
mysql_query("insert into aboard (intTrainId, intMSUserId) values ($trainid[0], $_POST[intMSUserId])");
setcookie("usermysid", $_POST[intMSUserId], time()+60*60*24*30);
}
} else
{
// already in the train
$error = "You are already in this train";
}
} else
{
//$error = "Invalid MySpace User ID";
}
}