【问题标题】:Error in Inserting data's in table在表格中插入数据时出错
【发布时间】:2014-12-10 02:37:04
【问题描述】:

我有一个代码,可以在特定数据库的每个表中插入数据。运行后出现此错误

"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)
  }
}

【问题讨论】:

  • 你错过了第 36 行的分号...
  • WOWeeeee,我的评论就是他们的回答
  • @Jeff 请使用 PDO 在我的答案中找到链接... :)

标签: php mysql sql insert


【解决方案1】:

我这个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();

?>

【讨论】:

    【解决方案2】:

    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);
    

    另外建议不要使用mysql_*,因为它们已被弃用,而是使用PDO 来防止sql 注入。请参考to my answer here我已经详细解释了那里。

    【讨论】:

      【解决方案3】:

      试试这个,你在 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);
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-02-07
        • 2012-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多