【问题标题】:Unable to get Table Name using variable string MYSQL Error无法使用变量字符串获取表名 MYSQL 错误
【发布时间】:2018-10-01 21:36:19
【问题描述】:
 if ($_GET['category'] == "ebooks")
{ $tableName = $smallsubcodewithoutspace.'_ebooks';
  $sectionTitle = "Ebook";
}
elseif ($_GET['category'] == "syllabus")
{ $tableName = $smallsubcodewithoutspace.'_syllabus';
  $sectionTitle = "Syllabus";
}
elseif ($_GET['category'] == "pnotes")
{ $tableName = $smallsubcodewithoutspace.'_pnotes';
  $sectionTitle = "Practical Note";
}
elseif ($_GET['category'] == "assignments")
{ $tableName = $smallsubcodewithoutspace.'_assignments';
  $sectionTitle = "Assignment";
}
elseif ($_GET['category'] == "tnotes")
{ $tableName = $smallsubcodewithoutspace.'_tnotes';
  $sectionTitle = "Theory Notes";
}

//if form has been submitted process it
    if(isset($_POST['submit'])){

        $_POST = array_map( 'stripslashes', $_POST );

        //collect form data
        extract($_POST);

        //very basic validation
        if($contentTitle ==''){
            $error[] = 'Please enter the Content Title !';
        }



        if($contentLink ==''){
            $error[] = "Please enter the Content Link !";
        }

        if(!isset($error)){

            try {

                //insert into database
                $stmt = $db->prepare("INSERT INTO `$tableName` (contentTitle,contentLink,contentAuthor) VALUES (:contentTitle, :contentLink, :contentAuthor)") ;
                $stmt->execute(array(
                    ':contentTitle' => $contentTitle,
                    ':contentLink' => $contentLink,
                                        ':contentAuthor' => $contentAuthor
                ));

                //redirect to index page
                header('Location: add-content.php?notallowed=true');
                exit;

            } catch(PDOException $e) {
                echo $e->getMessage();
            }

        }

    }

    //check for any errors
    if(isset($error)){
        foreach($error as $error){
            echo '<div align="center" class="alertpk"><div class="alert alert-warning" role="alert">'.$error.'</div></div>';
        }
    }

实际上,当我尝试使用变量插入表名时,问题就开始了。表存在于数据库中。总共有 5 个数据库,我将根据用户的选择在其中插入数据,但是当执行表单时,会抛出错误:

SQLstate[42000]:语法错误或访问冲突 1103,不正确的表名“”

【问题讨论】:

    标签: php mysql database pdo


    【解决方案1】:

    错误INCORRECT TABLE NAME '' 错误表示您在$tableName 中没有值。您的 $_GET['category'] 没有获得可识别的值,或者 extract($_POST) 正在将 $tableName 更改为空值。

    【讨论】:

      【解决方案2】:

      我得到了解决方案,我将 tableVariables 部分移到 try 中,现在它正在工作。

      【讨论】:

        【解决方案3】:

        var 转储您的变量,发布以查看出现的值。

        【讨论】:

          猜你喜欢
          • 2013-08-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-29
          相关资源
          最近更新 更多