我有一个代码,可以在特定数据库的每个表中插入数据。运行后,出现此错误
"Parse error: syntax error, unexpected '}', expecting ';' in C:\xampp\htdocs\Thesis\database\insertdata.php on line 37"
我检查代码是否有拼写错误,但我仍然没有看到任何错误,可能是因为循环错误导致
这是我的代码
$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)
}
}
答案 0 :(得分:0)
之后你错过了半结肠
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);
另外,由于建议不要使用mysql_*
,因此不建议使用PDO
来阻止sql注入。请参考to my answer here我在那里详细解释了它。
答案 1 :(得分:0)
试试这个,你在while循环中丢失了分号。
<?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);
}
}
答案 2 :(得分:0)
我mysql
已过时,您可以将其更改为mysqli
您可以使用:
<?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();
?>