duangou1868 2016-03-31 22:29
浏览 47

表单提交仅在Wordpress中打破的PHP代码

I have a simple form for submitting some data into the MySQL DB. On local machine works just fine, but inside a Wordpress page template doesn't work anymore, without getting me any error. The form is inside a page "sitename.com/upload" and i get redirected after submit to the same page (as shown in the link bar), but with 404 page content. I tried without get_header();and get_footer();tags because I thought it may conflict with some variables from wp, but I got the same result. Here is the code:

 <?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('','','');
 }
  • 写回答

1条回答 默认 最新

  • doudou_3636 2016-03-31 22:55
    关注

    Problem solved: Wordpress has something important internal reserved for "name" parameter.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题