Php 在表中插入数据时出错
我有一个代码,可以在特定数据库的每个表中插入数据。在我运行它之后,这个错误出现了 分析错误:语法错误,意外“}”,应为“;”在第37行的C:\xampp\htdocs\Thesis\database\insertdata.php中 我检查了输入错误的代码,但仍然没有看到任何错误,可能是因为循环错误 这是我的密码Php 在表中插入数据时出错,php,mysql,sql,insert,Php,Mysql,Sql,Insert,我有一个代码,可以在特定数据库的每个表中插入数据。在我运行它之后,这个错误出现了 分析错误:语法错误,意外“}”,应为“;”在第37行的C:\xampp\htdocs\Thesis\database\insertdata.php中 我检查了输入错误的代码,但仍然没有看到任何错误,可能是因为循环错误 这是我的密码 你错过了一个分号 do { for($i=0;$i<=30;$i++) { $section_teacher = $
你错过了一个分号
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注入。请参考,我已经在那里详细解释过了 试试看,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);
}
}
如果这个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();
?>
你在第36行漏掉了一个分号……Woweeee,我的评论是他们的answers@Jeff请使用PDO在我的答案中查找链接…:
<?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();
?>