Hello guys!
I'm trying to make a script for a school website, so the principal can send mails to all of his students, teachers or other admins, it should be very simple but the page just keep coming blank or sending me syntax errors. Right now it's showing the following error:

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in correos.php on line 1

It gets the variables from another page (which is working). It receives the options (who should get the email, $correos), the message ($mensaje) and the title ($asunto).

<?php
//Chooses the mails
//Just one group
if ($_POST['Alumnos'] empty($_POST['Maestros']) empty($_POST['Admins')])) {
    $sql = "SELECT email FROM alumnos ORDER BY nombre";
    $result = mysql_query($sql,$db);
    while ($row = mysql_fetch_row($result)) {
        $correos .= "$row[0],";
    }
} elseif ($_POST['Maestros'] && empty($_POST['Alumnos']) empty($_POST['Admins'])) {
    $sql = "SELECT email FROM maestros WHERE Admin='0' ORDER BY nombre";
    $result = mysql_query($sql,$db);
    while ($row = mysql_fetch_row($result)) {
        $correos .= "$row[0],";
    }
} elseif ($_POST['Admins'] && empty($_POST['Alumnos']) && empty($_POST['Maestros'])) {
    $sql = "SELECT email FROM maestros WHERE Admin='1' ORDER BY email";
    $result = mysql_query($sql,$db);
    while ($row = mysql_fetch_row($result)) {
        $correos .= "$row[0],";
    }
//Students and/or others
} elseif ($_POST['Alumnos'] && $_POST['Maestros'] && empty($_POST['Admins'])) {
    $sql = "(SELECT email FROM alumnos) UNION (SELECT email FROM maestros) order by email";
    $result = mysql_query($sql,$db);
    while ($row = mysql_fetch_row($result)) {
        $correos .= "$row[0],";
    }
} elseif ($_POST['Alumnos'] && $_POST['Admins'] && empty($_POST['Maestros'])) {
    $sql = "(SELECT email FROM alumnos) UNION (SELECT email FROM maestros WHERE Admin='1') order by email";
    $result = mysql_query($sql,$db);
    while ($row = mysql_fetch_row($result)) {
        $correos .= "$row[0],";
    }
//teachers and/or others
} elseif ($_POST['Maestros'] && $_POST['Alumnos'] && empty($_POST['Admins'])) {
    $sql = "(SELECT email FROM maestros WHERE Admin='0') UNION (SELECT email FROM alumnos) order by email";
    $result = mysql_query($sql,$db);
    while ($row = mysql_fetch_row($result)) {
        $correos .= "$row[0],";
    }
} elseif ($_POST['Maestros'] && $_POST['Admins'] && empty($_POST['Alumnos'])) {
    $sql = "SELECT email FROM maestros ORDER BY email";
    $result = mysql_query($sql,$db);
    while ($row = mysql_fetch_row($result)) {
        $correos .= "$row[0],";
    }
//Admins and/or others
} elseif ($_POST['Admins'] && $_POST['Alumnos'] && empty($_POST['Maestros'])) {
    $sql = "(SELECT email FROM maestros WHERE Admin='1') UNION (SELECT email FROM alumnos) order by email";
    $result = mysql_query($sql,$db);
    while ($row = mysql_fetch_row($result)) {
        $correos .= "$row[0],";
    }
} elseif ($_POST['Admins'] && $_POST['Maestros'] && empty($_POST['Alumnos'])) {
    $sql = "SELECT email FROM maestros ORDER BY email";
    $result = mysql_query($sql,$db);
    while ($row = mysql_fetch_row($result)) {
        $correos .= "$row[0],";
    }   
//Everyone 
} elseif ($_POST['Admins'] && $_POST['Maestros'] && $_POST['Alumnos']) {
    $sql = "(SELECT email FROM maestros) UNION (SELECT email FROM alumnos) order by email";
    $result = mysql_query($sql,$db);
    while ($row = mysql_fetch_row($result)) {
        $correos .= "$row[0],";
    }    
}


    

//every variable send trough: $HTTP_POST_VARS.
$asunto = $_POST['asunto'];
$mensaje = $_POST['mensaje'];
//$headers = "From: efren.camacho@yoamoamericanways.com\r\n";
//$headers .= "Reply-To: efren.camacho@yoamoamericanways.com\r\n";
//$headers .= "Return-Path: efren.camacho@yoamoamericanways.com\r\n";
//$headers .= "MIME-Version: 1.0\r\n";
//$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";//
   
//Error checking
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $correos)) {
    echo "<h4>Direccion de correo invalida!</h4>";
    echo "<a href='javascript:history.back(1);'>Regresar</a>";
} elseif (empty($asunto) or $asunto == "") {
    echo "<h4>Sin asunto</h4>";
    echo "<a href='javascript:history.back(1);'>Regresar</a>";

//Sends mail
} elseif (mail($correos,$asunto,$mensaje,"From: efren.camacho@yoamoamericanways.com")) {
    echo "<p align='center' class='input-container'><b>Correos Enviado Exitosamente!.</b></p>";
} else {
    echo "<p align='center' class='input-container'><b>Hubo un error por favor revisa los campos</b></p><br><p align='center' class='input-container'><b<a href='javascript:history.back(1);'>Regresar</a></b></p>";
}
?>

Sorry my code is so big, I know it's not very efficient, but I'm a newbie here, so I really couldn't find another way to do it :icon_redface:

Another thing it's that the page that sends the variables didn't work until I took out the comments

Thanks in advance!

Recommended Answers

All 7 Replies

Take a close look at this line:

#
if ($_POST['Alumnos'] empty($_POST['Maestros']) empty($_POST['Admins')])) {

Oh yeah it lacks the operators, I've got the code without comments and corrected as you pointed, also took out a few IF statements, yet it doesn't work.
Thanks for your reply!
Here's the new code:

<?php

if($_POST['Alumnos'] && empty($_POST['Maestros']) && empty($_POST['Admins'])) {
    $sql = "SELECT email FROM alumnos ORDER BY nombre";
    $result = mysql_query($sql,$db);
    while ($row = mysql_fetch_row($result)) {
        $correos .= "$row[0],";
    }
} elseif ($_POST['Maestros'] && empty($_POST['Alumnos']) empty($_POST['Admins'])) {
    $sql = "SELECT email FROM maestros WHERE Admin='0' ORDER BY nombre";
    $result = mysql_query($sql,$db);
    while ($row = mysql_fetch_row($result)) {
        $correos .= "$row[0],";
    }
} elseif ($_POST['Admins'] && empty($_POST['Alumnos']) && empty($_POST['Maestros'])) {
    $sql = "SELECT email FROM maestros WHERE Admin='1' ORDER BY email";
    $result = mysql_query($sql,$db);
    while ($row = mysql_fetch_row($result)) {
        $correos .= "$row[0],";
    }
} elseif ($_POST['Alumnos'] && $_POST['Maestros'] && empty($_POST['Admins'])) {
    $sql = "(SELECT email FROM alumnos) UNION (SELECT email FROM maestros) order by email";
    $result = mysql_query($sql,$db);
    while ($row = mysql_fetch_row($result)) {
        $correos .= "$row[0],";
    }
} elseif ($_POST['Alumnos'] && $_POST['Admins'] && empty($_POST['Maestros'])) {
    $sql = "(SELECT email FROM alumnos) UNION (SELECT email FROM maestros WHERE Admin='1') order by email";
    $result = mysql_query($sql,$db);
    while ($row = mysql_fetch_row($result)) {
        $correos .= "$row[0],";
    }
} elseif ($_POST['Maestros'] && $_POST['Alumnos'] && empty($_POST['Admins'])) {
    $sql = "(SELECT email FROM maestros WHERE Admin='0') UNION (SELECT email FROM alumnos) order by email";
    $result = mysql_query($sql,$db);
    while ($row = mysql_fetch_row($result)) {
        $correos .= "$row[0],";
    }
} elseif ($_POST['Maestros'] && $_POST['Admins'] && empty($_POST['Alumnos'])) {
    $sql = "SELECT email FROM maestros ORDER BY email";
    $result = mysql_query($sql,$db);
    while ($row = mysql_fetch_row($result)) {
        $correos .= "$row[0],";
    }
} elseif ($_POST['Admins'] && $_POST['Alumnos'] && empty($_POST['Maestros'])) {
    $sql = "(SELECT email FROM maestros WHERE Admin='1') UNION (SELECT email FROM alumnos) order by email";
    $result = mysql_query($sql,$db);
    while ($row = mysql_fetch_row($result)) {
        $correos .= "$row[0],";
    }
} elseif ($_POST['Admins'] && $_POST['Maestros'] && empty($_POST['Alumnos'])) {
    $sql = "SELECT email FROM maestros ORDER BY email";
    $result = mysql_query($sql,$db);
    while ($row = mysql_fetch_row($result)) {
        $correos .= "$row[0],";
    }   
} elseif ($_POST['Admins'] && $_POST['Maestros'] && $_POST['Alumnos']) {
    $sql = "(SELECT email FROM maestros) UNION (SELECT email FROM alumnos) order by email";
    $result = mysql_query($sql,$db);
    while ($row = mysql_fetch_row($result)) {
        $correos .= "$row[0],";
    }    
}
    

$asunto = $_POST['asunto'];
$mensaje = $_POST['mensaje'];
   
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $correos)) {
    echo "<h4>Direccion de correo invalida!</h4>";
    echo "<a href='javascript:history.back(1);'>Regresar</a>";
} elseif (empty($asunto) or $asunto == "") {
    echo "<h4>Sin asunto</h4>";
    echo "<a href='javascript:history.back(1);'>Regresar</a>";
} elseif (mail($correos,$asunto,$mensaje,"From: efren.camacho@yoamoamericanways.com")) {
    echo "<p align='center' class='input-container'><b>Correos Enviado Exitosamente!.</b></p>";
} else {
    echo "<p align='center' class='input-container'><b>Hubo un error por favor revisa los campos</b></p><br><p align='center' class='input-container'><b<a href='javascript:history.back(1);'>Regresar</a></b></p>";
}
?>

It throws this error:

Parse error: parse error, unexpected '{' in correos.php  on line 1

Thanks in advance.

Member Avatar for rajarajan2017

first elseif missing && between empty()

elseif ($_POST['Maestros'] && empty($_POST['Alumnos']) [B]&&[/B] empty($_POST['Admins'])) {

Try this code.instead of nine conditions.
Just for your review.
Here you have 3 fields and u r using 3*3=9 if-else, if you will have four fields then?It is not good practice for coding

if(!empty($_POST['Alumnos']))
{
	$sql = " (SELECT email FROM alumnos ORDER BY nombre) ";
}
if(!empty($_POST['Maestros']))
{
	if($sql!='') $sql.= ' UNION ';
	$sql.= " (SELECT email FROM maestros WHERE Admin='0') ";
}
if(!empty($_POST['Admins']))
{
	if($sql!='') $sql.= ' UNION ';
	$sql.= " (SELECT email FROM maestros WHERE Admin='1') ";
}
$sql.= ' ORDER BY email ';
$result = mysql_query($sql,$db);
while ($row = mysql_fetch_row($result)) 
{
	$correos .= "$row[0],";
}

If you will have less line of coding then it will reduce effort for debugging and finding any issue.
Hope this helps.

Thank you very much for your responses.
That code was amazing and elegant! I'm using it!
And yes I had some operators missing.
Thanks again.
I've reduced the problem to very first lines, where it connects to the database:

$db = mysql_connect("some_server","user","password"); mysql_select_db("database",$db);

For some reason it stops working after that line, and just throws me a blank page.
Regards!

Member Avatar for Zagga

That line of code is actually 2 seperate statements.
Try seperating the 2, defining the select statement as a variable and adding some error trapping to help identify the problem.

$db = mysql_connect("some_server","user","password") or die('Error - Could not connect to MySQL: ' . mysql_error());

$db_selected = mysql_select_db("database",$db) or die('Error - Could not select the database: ' . mysql_error());

Remember to remove the "or die" statements before you go live.


Zagga

Excellent! The code is finally working!

<?
$db = mysql_connect("h50mysql67.secureserver.net","aways","a2W4a6Y9");

$db_selected = mysql_select_db("aways",$db);



if(!empty($_POST['Alumnos'])) {
	$sql = " (SELECT email FROM alumnos ORDER BY nombre) ";
}

if(!empty($_POST['Maestros'])) {
	if($sql!='') $sql.= ' UNION ';
	$sql.= " (SELECT email FROM maestros WHERE Admin='0') ";
}

if(!empty($_POST['Admins'])) {
	if($sql!='') $sql.= ' UNION ';
	$sql.= " (SELECT email FROM maestros WHERE Admin='1') ";
}

$sql.= ' ORDER BY email ';
$result = mysql_query($sql,$db);
while ($row = mysql_fetch_row($result)) {
	$correos .= "$row[0],";
}
    
$asunto = $_POST['asunto'];
$mensaje = $_POST['mensaje'];

if($asunto == "" or empty($asunto)) {
    $asunto = "Sin Asunto";
}
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $correos)) {
    echo "<h4>Direccion de correo invalida!</h4>";
    echo "<a href='javascript:history.back(1);'>Regresar</a>";
}
   
mail($correos,$asunto,$mensaje,"From: efren.camacho@yoamoamericanways.com");

echo "<br><br><p align='center' class='input-container'><b>Correos Enviado Exitosamente!.</b></p>";

?>

That's it, Zagga's code did the trick, Rajarajan and Will noted much of the mistakes I did when writing it. Vibhadevit further simplified the code making it much more cleaner.
The problem was that it wouldn't connect to database, right at the beginning of the code.
Thank you guys very much for your inputs and time!
Regards!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.