php - 表单提交 PHP 代码仅在 WordPress 中损坏

标签 php mysql wordpress forms

我有一个简单的表单,用于将一些数据提交到 MySQL 数据库中。在本地计算机上工作得很好,但在 WordPress 页面模板内不再工作,也不会出现任何错误。该表单位于“sitename.com/upload”页面内,提交到同一页面后我会被重定向(如链接栏中所示),但包含 404 页面内容。我尝试不使用 get_header();get_footer(); 标签,因为我认为它可能与 wp 中的某些变量冲突,但我得到了相同的结果。 这是代码:

 <?php function renderForm($name, $price, $error)
 {
 // if there are any errors, display them
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?> 
***** LONG HTML FORM IS HERE *****
 <?php 
 }


 // connect to the database
 include('connect-db.php');

 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid
 $name = mysqli_real_escape_string($connection, htmlspecialchars($_POST['name']));
 $price = mysqli_real_escape_string($connection, htmlspecialchars($_POST['price']));
 $shortdesc = mysqli_real_escape_string($connection, htmlspecialchars($_POST['shortdesc']));
 $longdesc = mysqli_real_escape_string($connection, htmlspecialchars($_POST['longdesc']));
 $current_version = mysqli_real_escape_string($connection, htmlspecialchars($_POST['current-version']));   
 $content_rating = $_POST['contentrating'];  

 if(isset($_POST['category'])) {
    $category = implode(",", $_POST['category']);   
 } else {
    $category = "";
 }

 if(isset($_POST['platform'])) {
  $platform = implode(",", $_POST['platform']); 
 } else {
  $platform = "";
 }

 if(isset($_POST['devices'])) {
  $devices = implode(",", $_POST['devices']); 
 } else {
  $devices = "";
 }

  if(isset($_POST['gamemodes'])) {
  $gamemodes = implode(",", $_POST['gamemodes']); 
 } else {
  $gamemodes = "";
 }


//FILE UPLOAD
$images = array();
if(isset($_FILES['files'])){
    $errors= array();
  foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
    $file_name =$_FILES['files']['name'][$key];
    $file_size =$_FILES['files']['size'][$key];
    $file_tmp =$_FILES['files']['tmp_name'][$key];
    $file_type=$_FILES['files']['type'][$key];  
        if($file_size > 2097152){
      $errors[]='File size must be less than 2 MB';
        } 
        $desired_dir="uploads/images";
        if(empty($errors)==true){
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir", 0700);    // Create directory if it does not exist
            }
            if(is_dir("$desired_dir/".$file_name)==true){
                move_uploaded_file($file_tmp,"uploads/images/".$file_name);
            }else{                  //rename the file if another one exist
                $file_name = time()."-".$file_name;
                $new_dir="uploads/images/".$file_name;
                 rename($file_tmp,$new_dir) ;       
            }
            $images[] = $file_name;     
        }else{
                print_r($errors);
        }
    }
  if(empty($error)){
    $imglinks = implode(" | ", $images);
  }
}


//FILE UPLOAD END


 // check to make sure both fields are entered
 if ($name == '' || $price == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 // if either field is blank, display the form again
 renderForm($name, $price, $error);
 }
 else
 {
 $sql = "INSERT INTO vr_submitted_apps ". "(name, price, shortdesc, longdesc, crtvers, rating, category, platform, devices, gamemodes, images, dtime) ". "VALUES('$name','$price','$shortdesc','$longdesc','$current_version','$content_rating','$category','$platform','$devices','$gamemodes', '$imglinks', NOW())";

 // save the data to the database
mysqli_query( $connection, $sql )
 or die(mysql_error()); 
$itemId = mysqli_insert_id($connection);
setcookie("last-inserted-id", $itemId, time() + (86400 * 3), "/"); // 86400 = 1 day

 // once saved, redirect back to the view page
header("Location: uploader.html"); 
 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','');
 }

最佳答案

问题已解决:Wordpress 为“name”参数保留了一些重要的内部内容。

关于php - 表单提交 PHP 代码仅在 WordPress 中损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36344936/

相关文章:

php - 正则表达式捕获查询字符串中的变量

php - 您知道哪些特定于PHP的反模式?

Mysql 查询连接条件不起作用

php - PHP 中的简单并发?

mysql - NODEJS node-mysql 无法在嵌套查询中传递查询结果

php - WooCommerce 购物车添加额外的 <p> 标签

php - 存储多个复选框的最佳方法,以便我以后可以轻松查询它们

php - symfony2 twig - 原始和切片过滤器一起不起作用

php - 启用 suhosin 后如何在两个站点之间共享 session ?

wordpress - wordpress 的 Multi-Tenancy 插件