I have a code which inserts data's in each tables in a specific database. After i run it this error come up
"Parse error: syntax error, unexpected '}', expecting ';' in C:\xampp\htdocs\Thesis\database\insertdata.php on line 37"
I check the codes for typo error but still i don't see any error on it maybe it is cause by a looping error
This is my Code
$db_name = array('morning_section_masterfile','evening_section_masterfile','afternoon_section_masterfile');
for($y=0;$y<=2;$y++)
{
$db = mysql_select_db($db_name[$y],$connectDatabase);
$tables = array('Pilot_Sections','Black_Sections');
for($a=0;$a<=1;$a++)
{
//variable making
$teacher = array ('Jane','Jeff','Liezeth','Loremas','Canada');
$Default_Lname = 'Lorems';
$Default_Fname = 'Vierzehn';
$x=0;
//Adding 30 students in one section
do
{
for($i=0;$i<=30;$i++)
{
$section_teacher = $teacher[$x];
$student_section = 'IT70'.$x.'E-C';
$student_Lname = str_shuffle($Default_Lname);
$student_Fname = str_shuffle($Default_Fname);
$table = $tables[$a];
$insert = "INSERT INTO $table (section_Teacher, student_Lastname, student_Firstname, student_Section) VALUES
('{$section_teacher}','{$student_Lname}','{$student_Fname}','{$student_section}')";
$insertdata = mysql_query($insert,$connectDatabase);
}
//check the number of students in a section section and adding another section
if($i==31)
{
$x++;
$i=0;
}
} while($x<=4)
}
}
You missed a semi colon after while
do
{
for($i=0;$i<=30;$i++)
{
$section_teacher = $teacher[$x];
$student_section = 'IT70'.$x.'E-C';
$student_Lname = str_shuffle($Default_Lname);
$student_Fname = str_shuffle($Default_Fname);
$table = $tables[$a];
$insert = "INSERT INTO $table (section_Teacher, student_Lastname, student_Firstname, student_Section) VALUES
('{$section_teacher}','{$student_Lname}','{$student_Fname}','{$student_section}')";
$insertdata = mysql_query($insert,$connectDatabase);
}
//check the number of students in a section section and adding another section
if($i==31)
{
$x++;
$i=0;
}
} while($x<=4);
Also as an advice don't use mysql_*
as they are deprecated, Use PDO
instead to prevent sql injection. Please refer to my answer here I've explained it in detail there.
try this, you are missing semicolon in while loop at end.
<?php
$db_name = array('morning_section_masterfile','evening_section_masterfile','afternoon_section_masterfile');
for($y=0;$y<=2;$y++)
{
$db = mysql_select_db($db_name[$y],$connectDatabase);
$tables = array('Pilot_Sections','Black_Sections');
for($a=0;$a<=1;$a++)
{
//variable making
$teacher = array ('Jane','Jeff','Liezeth','Loremas','Canada');
$Default_Lname = 'Lorems';
$Default_Fname = 'Vierzehn';
$x=0;
//Adding 30 students in one section
do
{
for($i=0;$i<=30;$i++)
{
$section_teacher = $teacher[$x];
$student_section = 'IT70'.$x.'E-C';
$student_Lname = str_shuffle($Default_Lname);
$student_Fname = str_shuffle($Default_Fname);
$table = $tables[$a];
$insert = "INSERT INTO $table (section_Teacher, student_Lastname, student_Firstname, student_Section) VALUES
('{$section_teacher}','{$student_Lname}','{$student_Fname}','{$student_section}')";
$insertdata = mysql_query($insert,$connectDatabase);
}
//check the number of students in a section section and adding another section
if($i==31)
{
$x++;
$i=0;
}
} while($x<=4);
}
}
I this mysql
is old, you can change it to mysqli
You can use this:
<?php
//****** MySql Connect
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Connect failed";
// if connection error script stop working
exit();
die();
}
//****** real_escape_string (Security)
$city = $mysqli->real_escape_string($city);
//****** RESULT
$query = "SQL";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
echo $row["Name"];
}
$result->free();
}
//****** Query
$mysqli->query("SQL");
//****** Close
$mysqli->close();
?>