Data Team

[insert_php]
//USER
global $current_user;
get_currentuserinfo();
$user=$current_user->user_login;

//CONNECTION
$connection=mysql_connect(‘localhost’,’sportteamsaroun’,’Currais1988′);
mysql_select_db(“sportteamsaroun”,$connection);
mysql_set_charset(‘utf8’);

if($user!=null)
{
//SAVE or RESAVE or LOGIN
if($_POST[‘submit_save’] || $_POST[‘submit_resave’])
{
//CORRECT DATA
$correctData = FALSE;
//DATOS
//NOME
$name = $_POST[‘nameTeam’];
$nameMinLength = 3;
$nameMaxLength = 30;
//REPRESENTANTE
$representative = $_POST[‘representative’];
$representativeMinLength = 3;
$representativeMaxLength = 30;
//SPORT
$sport = $_POST[‘sport’];
//COMMENT
$comment = $_POST[‘comment’];
$commentMinLength = 3;
$commentMaxLength = 200;
//CATEGORIAS
$categoriesArray = array();

//QUERIES
$queCategories = “SELECT * FROM sport_category_list_table”;
$queCat = mysql_query($queCategories,$connection);

while ($category=mysql_fetch_assoc($queCat))
{
if($_POST[ $category[‘id’]])
{
$categoriesArray[] = $category[‘id’];
}
}

//LOCALIZACIÓN
$country= $_POST[‘country’];
$province= “NA”;//$_POST[‘province’];
$town = $_POST[‘state’];
//ENDEREZO
$address = $_POST[‘address’];
$addressMinLength=5;
$addressMaxLength=100;
//EMAIL
$email = $_POST[’email’];
$emailMinLength = 3;
$emailMaxLength=100;
//TELÉFONO
$phone = $_POST[‘phone’];
$phoneMinLength=9;
$phoneMaxLength=13;
//WEBSITE
$website = $_POST[‘website’];
$webMinLength=6;
$webMaxLength=100;
//IDENTIFICADOR
$id=”0″;
$idCategoriesTable=”0″;

//MESSAGE
$mesEmailWrong = “Incorrect mail.
“;
$mesEmailExisting = “The email already exists.
“;
$mesConditionsNoAccepted = “Accept the terms to save data.
“;
$mesProblemSaving = “Problems saving data.
“;
$mesDataRight = “Saved properly.
“;
$mesDataWrong = “Data is wrong.
“;

$mesNameWrong = “Name does not fulfil the conditions.
“;
$mesCountryMissed = “Choose the country.
“;
$mesProvinceMissed = “Choose the province.
“;
$mesTownMissed = “Choose the town.
“;
$mesAddressMissed = “Fill address right.
“;
$mesPhoneMissed = “Fill phone right.
“;
$mesWebsiteMissed = “Fill website right.
“;

$mesTeamSavingError = “ERROR. Problems saving team data.
“;
$mesRepresentativeMissed = “Fill representative right.
“;
$mesSportMissed = “Choose sport.
“;
$mesCategoriesMissed = “Choose at least a category.
“;

$mesPasswordsDifferent = “Passwords are different.
“;
$mesPasswordForgotten = “Fill the password field and the confirmation one.
“;
$mesEmailsDifferent = “Emails are different.
“;
$mesStringsDifferent = “The fields are different.
“;
$mesPhoneMissed = “Fill the phone field.
“;
$mesPhoneLength = “Phone number can’t have so many numbers.
“;
$mesStringsDifferent = “The fields are different.
“;
$mesPhoneInvalid = “Invalid characters.
“;
$mesCommentMissed = “Write down something about the team.
“;

//////////////////EMAIL//////////////////
//FUNCIÓN para verificar lonxitude mail
$emailSameChecked = false;
$emailLengthChecked = false;
$emailChecked = false;

if($email!=null && $emailMinLength!=null && $emailMaxLength!=null)
{
$emailLengthChecked = false;
if(!empty($email))
{
if(strlen($email)<$emailMinLength || strlen($email)>$emailMaxLength)
{
$emailLengthChecked = false;
}
else
{
$emailLengthChecked = true;
}
}
}
//FUNCIÓN igualdade de emails
if($email!=null && $email!=null)
{
if(!empty($email))
{
if ($email==$email)
{
$emailSameChecked = true;
}
else
{
echo $mesStringsDifferent.”for emails.
“;
$emailSameChecked = false;
}
}
}
//FUNCIÓN para verificar mail
if($email!=null && $emailMinLength!=null && $emailMaxLength!=null)
{
if($emailLengthChecked=true)
{
if (ereg(“^([a-zA-Z0-9._]+)@([a-zA-Z0-9.-]+).([a-zA-Z]{2,4})$”,$email))
{
if($emailSameChecked == true)
{
$checkedEmail = true;
}
else
{
echo $mesEmailsDifferent;
}
}
else
{
echo $mesEmailWrong;
}
}
else
{
echo $mesEmailWrong;
}
}
//////////////////PHONE//////////////////
$phoneLengthChecked = false;
$phoneChecked = false;
//FUNCIÓN que nos comproba se a lonxitude é correcta
if($phone!=null && $phoneMinLength!=null && $phoneMaxLength!=null)
{
if(!empty($phone))
{
if(strlen($phone)<$phoneMinLength || strlen($phone)>$phoneMaxLength)
{
$phoneLengthChecked = false;
}
else
{
$phoneLengthChecked = true;
}
}
}
//FUNCIÓN Verificar teléfono
if($phone!=null && $phoneMinLength!=null && $phoneMaxLength!=null)
{
if (empty($phone))
{
echo $mesPhoneMissed;
}
else
{
if($phoneLengthChecked == false)
{
echo $mesPhoneLength;
}
else
{
if (ereg(“^([0-9])”,$phone))
{
$phoneChecked = true;
}
else
{
echo $mesPhoneInvalid;
}
}
}
}
//////////////////NAME//////////////////
$nameChecked = false;
//FUNCIÓN que nos comproba se a lonxitude é correcta
if($name!=null && $nameMinLength!=null && $nameMaxLength!=null)
{
if(!empty($name))
{
if(strlen($name)<$nameMinLength || strlen($name)>$nameMaxLength)
{
$nameChecked = false;
}
else
{
$nameChecked = true;
}
}
}
//////////////////COMMENT//////////////////
$commentChecked = false;
//FUNCIÓN que nos comproba se a lonxitude é correcta
if($comment!=null && $commentMinLength!=null && $commentMaxLength!=null)
{
if(!empty($comment))
{
if(strlen($comment)<$commentMinLength || strlen($comment)>$commentMaxLength)
{
$commentChecked = false;
}
else
{
$commentChecked = true;
}
}
}
//////////////////REPRESENTATIVE//////////////////
$representativeChecked = false;
//FUNCIÓN que nos comproba se a lonxitude é correcta
if($representative!=null && $representativeMinLength!=null && $representativeMaxLength!=null)
{
if(!empty($representative))
{
if(strlen($representative)<$representativeMinLength || strlen($representative)>$representativeMaxLength)
{
$representativeChecked = false;
}
else
{
$representativeChecked = true;
}
}
}
//////////////////ADDRESS//////////////////
$addressChecked = false;
//FUNCIÓN que nos comproba se a lonxitude é correcta
if($address!=null && $addressMinLength!=null && $addressMaxLength!=null)
{
if(!empty($address))
{
if(strlen($address)<$addressMinLength || strlen($address)>$addressMaxLength)
{
$addressChecked = false;
}
else
{
$addressChecked = true;
}
}
}
//////////////////WEBSITE//////////////////
$website = $_POST[‘website’];

$websiteChecked = false;
//FUNCIÓN que nos comproba se a lonxitude é correcta
if($website!=null && $webMinLength!=null && $webMaxLength!=null)
{
if(!empty($website))
{
if(strlen($website)<$webMinLength || strlen($website)>$webMaxLength)
{
$websiteChecked = false;
}
else
{
$websiteChecked = true;
}
}
}

//QUERIES
$queTeam = “SELECT * FROM sport_data_list_table”;
$queTea = mysql_query($queTeam,$connection);

if($_POST[‘submit_save’])
{
//Comprobamos valores
if($emailChecked == true) //mail not used
{
$foundEmail=false;

//Comprobamos si existe o usuario e o mail
while ($wantedMailTeamData = mysql_fetch_assoc($queTea)) //Comproba na base de datos
{
$idString = $wantedMailTeamData[‘id’];
if($email == $wantedMailTeamData[’email’])
{
$foundEmail=true;
}
}

$idNumber = (int)$idString; //id en número
$idNumber = $idNumber + 1; //sumarlle un
$id=(string)$idNumber;

if ($foundEmail==false) //Se non se atopa na base de datos
{
if ($nameChecked == true)
{
if ($representative==”” || $representativeChecked == true)
{
if($sport!=”sport”)
{
if($commentChecked == true)
{
if(sizeof($categoriesArray)>0)
{
if($country!=””)
{
if($town!=””)
{
if($address ==”” || $addressChecked == true)
{
if($phoneChecked == true)
{
if($websiteChecked == true)
{
$correctData = true;
}
else
{
$message = $mesWebsiteMissed;
}
}
else
{
$message = $mesPhoneMissed;
}
}
else
{
$message = $mesAddressMissed;
}
}
else
{
$message = $mesTownMissed;
}
}
else
{
$message = $mesCountryMissed;
}
}
else
{
$message = $mesCategoriesMissed;
}
}
else
{
$message = $mesCommentMissed;
}
}
else
{
$message = $mesSportMissed;
}
}
else
{
$message = $mesRepresentativeMissed;
}
}
else
{
$message = $mesNameWrong;
}
}
else
{
$message = $mesEmailExisting;
}
}
else
{
$message = $mesEmailWrong;
}

//PRINT MESSAGE
echo “

“.$message.”

“;

if ($correctData = TRUE)
{
//Team
$name = addslashes($name);
$email = addslashes($email);
$representative = addslashes($representative);
//Localización
$town = addslashes($town);
$province = addslashes($province);
$address = addslashes($address);
$phone = addslashes($phone);
$website = addslashes($website);

//INSERTAR VALORES NA TABLA XERAL
$querySaveTeam = ‘INSERT INTO sport_data_list_table(id,username,team_name,representative,id_sport,comment,country,province,town,address,phone,email,website) VALUES (“‘.$id.'”,”‘.$user.'”,”‘.$name.'”,”‘.$representative.'”,”‘.$idSport.'”,”‘.$country.'”,”‘.$province.'”,”‘.$town.'”,”‘.$address.'”, “‘.$email.'”,”‘.$phone.'”,”‘.$website.'”)’;
$querySavTea = mysql_query($querySaveTeam,$connection);

echo “
“.mysql_error().”
“;

//INSERTAR VALORES NA TABOA CATEGORÍAS
$queSportCategories = “SELECT * FROM sport_category_data_list_table”;
$queSpoCat = mysql_query($queSportCategories,$connection);

//Coller id data táboa
$idCategoriesTable;
while ($wantedIDcategories = mysql_fetch_assoc($queSpoCat)) //Comproba na base de datos
{
$idCategoriesTable = $wantedIDcategories[‘id’];
}

$idCategoriesTableCopy = $idCategoriesTable; //Copia para borrar se se precisa

foreach ($categoriesArray as $i => $value)
{
//Aumentamos
$idCategoriesTable = $idCategoriesTable + 1;

$queryInsertCategories = “INSERT INTO sport_category_data_list_table (id, id_team, id_category) VALUES (‘”.$idCategoriesTable.”‘, ‘”.$id.”‘, ‘”.$value.”‘)”;
$queDelCat = mysql_query($queryInsertCategories,$connection);

echo “
“.mysql_error().”
“;
}
//GARDAR DATOS
if($querySavTea && $queDelCat)
{
$message_data = $mesDataRight;
}
else
{
//Team
$queryDeleteTeam = “DELETE FROM sport_data_list_table WHERE ‘”.$id.”‘ = id”;
$queDelTea = mysql_query($queryDeleteTeam,$connection);

//Categorias
$value = 0;
foreach ($idCategoriesTableCopy as $i => $value)
{
//Aumentamos
$idCategoriesTableCopy = $idCategoriesTableCopy + 1;
//
$queryDeleteCategories = “DELETE FROM sport_category_data_list_table WHERE ‘”.$idCategoriesTableCopy.”‘ = id”;
$queDelCat = mysql_query($queryDeleteCategories,$connection);
}
$message_data = $mesTeamSavingError;
}
}
else
{
$message_data= $mesDataWrong;
}
echo $message_data;
}
elseif ($_POST[‘submit_resave’])
{
//Comprobamos valores
if($nameChecked == true) //mail not used
{
if ($nameChecked == true)
{
if ($representative==”” || $representativeChecked == true)
{
if($sport!=”sport”)
{
if($commentChecked == true)
{
if(sizeof($categoriesArray)>0)
{
if($country!=””)
{
if($town!=””)
{
if($address ==”” || $addressChecked == true)
{
if($phoneChecked == true)
{
if($websiteChecked == true)
{
$correctData = true;
}
else
{
$message = $mesWebsiteMissed;
}
}
else
{
$message = $mesPhoneMissed;
}
}
else
{
$message = $mesAddressMissed;
}
}
else
{
$message = $mesTownMissed;
}
}
else
{
$message = $mesCountryMissed;
}
}
else
{
$message = $mesCategoriesMissed;
}
}
else
{
$message = $mesCommentMissed;
}
}
else
{
$message = $mesSportMissed;
}
}
else
{
$message = $mesRepresentativeMissed;
}
}
else
{
$message = $mesNameWrong;
}
}
else
{
$message = $mesEmailExisting;
}

//PRINT MESSAGE
echo “

“.$message.”

“;

if ($correctData)
{
//Team
$name = addslashes($name);
$email = addslashes($email);
$representative = addslashes($representative);
//Localización
$town = addslashes($town);
$province = addslashes($province);
$address = addslashes($address);
$phone = addslashes($phone);
$website = addslashes($website);

//INSERTAR VALORES NA TABLA XERAL
$querySaveTeam = ‘UPDATE sport_data_list_table SET username=”‘.$user.'”,team_name=”‘.$name.'”,representative=”‘.$representative.'”,id_sport=”‘.intval($idSport).'”,comment=”‘.$comment.'”,country=”‘.$country.'”,province=”‘.$province.'”,town=”‘.$town.'”,address=”‘.$address.'”,phone=”‘.$phone.'”,email=”‘.$email.'”,website=”‘.$website.'” WHERE id=”‘.$id.'”‘;
$querySavTea = mysql_query($querySaveTeam,$connection);

echo mysql_error();

//INSERTAR VALORES NA TABOA CATEGORÍAS
//Query
$queSportCategories = “SELECT * FROM sport_category_data_list_table”;
$queSpoCat = mysql_query($queSportCategories,$connection);

echo mysql_error();

//Coller id data táboa
$idCategoriesTable;
while ($wantedIDcategories = mysql_fetch_assoc($queSpoCat)) //Comproba na base de datos
{
$idCategoriesTable = $wantedIDcategories[‘id’];
}

$idCategoriesTableCopy = $idCategoriesTable; //Copia para borrar se se precisa

//QUERIES
$queSportCategories = “SELECT * FROM sport_category_data_list_table WHERE id_team='”.$id.”‘”;
$queSpoCat = mysql_query($queSportCategories,$connection);

echo mysql_error();
//Array de id’s
$categoriesArrayDoNotDelete = array();

while ($category=mysql_fetch_assoc($queSpoCat))
{
$decision=FALSE;
foreach ($categoriesArray as $i => $value)
{
if($value==$category[‘id_category’])
{
$decision=TRUE;
$categoriesArrayDoNotDelete[]=$value;
}
}
if($decision==FALSE)
{
$queryDeleteCategories = “DELETE FROM sport_category_data_list_table WHERE id_team='”.$id.”‘ AND id_category='”.$category[‘id_category’].”‘”;
$queDelCat = mysql_query($queryDeleteCategories,$connection);

echo mysql_error();
}
}

foreach ($categoriesArray as $i => $valueI)
{
$decision=FALSE;
foreach ($categoriesArrayDoNotDelete as $j => $valueJ)
{
if($valueI==$valueJ)
{
$decision=TRUE;
}
}
if($decision==FALSE)
{
//Aumentamos
$idCategoriesTable = $idCategoriesTable + 1;
$queryInsertCategories = “INSERT INTO sport_category_data_list_table (id, id_category, id_team) VALUES (‘”.$idCategoriesTable.”‘, ‘”.$valueI.”‘, ‘”.$id.”‘)”;
$queDelCat = mysql_query($queryInsertCategories,$connection);

echo mysql_error();
}
}

//GARDAR DATOS
if($querySavTea)
{
$message_data = mysql_affected_rows().” “.$mesDataRight;
}
else
{
$message_data = $mesTeamSavingError;
}
}
else
{
$message_data= $mesDataWrong;
}
echo $message_data;
}
}
}
else
{
echo “
Please log in.
“;
}
[/insert_php]

[insert_php]

$query = “SELECT * FROM sport_data_list_table WHERE username='”.$user.”‘”;
$que = mysql_query($query,$connection);

//$id
$team_name = null;
$representative = null;
$id_sport = null;
$comment = null;
$country = null;
//$province = null;
$town = null;
$address = null;
$phone = null;
$email = null;
$website = null;

while($cell=mysql_fetch_assoc($que))
{
//$id
$team_name = $cell[“team_name”];
//$trimmed = trim($text, ” \t.”);
$representative = $cell[“representative”];
$id_sport = $cell[“id_sport”];
$comment = $cell[“comment”];
$country = $cell[“country”];
//$province = $cell[“province”];
$town = $cell[“town”];
$address = $cell[“address”];
$phone = $cell[“phone”];
$email = $cell[“email”];
$website = $cell[“website”];
}

[/insert_php]

*Sport Club Name:

*Sport:

[insert_php]

$query = “SELECT * FROM sport_list_table”;
$que = mysql_query($query,$connection);

echo ‘‘;

[/insert_php]

*Comment:

*Categories:

[insert_php]
//QUERIES
$queCategories = “SELECT * FROM sport_category_list_table”;
$queCat = mysql_query($queCategories,$connection);

echo “

“;
$i=0;
while ($category=mysql_fetch_assoc($queCat))
{
if($i==0 || $i==3 ||$i==6 || $i==9 || $i==12 || $i==15)
{
echo “

“;
}
echo “

“;

if($i==2 || $i==5 ||$i==8 || $i==11 || $i==14 || $i==18)
{
echo “

“;
}
$i = $i +1;
}
echo “

“;
echo ‘‘;
echo $category[‘category_en’];
echo ‘‘;
echo “

“;
[/insert_php]

Representative:

*Country:

*City:

Address:

*Email:

*Phone:

*Website:

[insert_php]
//CLOSED CONNECTION
mysql_close($connection);
[/insert_php]