使用SPACE php mysql

时间:2017-08-01 11:28:35

标签: php mysql selection

好的,我正在尝试将多个值插入到带有空格的单个列catName中。但是我无法插入它......我的代码也是如此。假设我从下拉列表中选择了体育板球足球,并且所有三个名称都应该在列中,空格只有逗号。

<?php
//include config
require_once('includes/config.php');

//if not logged in redirect to login page
if(!$user->is_logged_in()){ header('Location: login.php'); }

require_once('includes/config2.php');
    if(isset($_FILES['files'])){
    $errors= array();
    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
    $picCourtesy = $_POST["picCourtesy"];
    $picTitle = $_POST["picTitle"];
    $catName = implode(' ',$_POST['$catName']); 

        $file_name = $key.$_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';
        }       
        $query="INSERT into gallery (`picTitle`,`picCourtesy`,`catName`,`gFILE_NAME`,`gFILE_SIZE`,`gFILE_TYPE`) VALUES('$picTitle','$picCourtesy','$catName','$file_name','$file_size','$file_type'); ";
        $desired_dir="../gallery";
        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)==false){
                move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
            }else{                                  // rename the file if another one exist
                $new_dir="$desired_dir/".$file_name.time();
                 rename($file_tmp,$new_dir) ;               
            }
         mysqli_query($conn,$query);            
        }else{
                print_r($errors);
        }
    }
    if(empty($error)){
        header('Location: index.php');
                exit;
    }
}?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Admin - Add Post</title>
  <link rel="stylesheet" href="style/normalize.css">
  <link rel="stylesheet" href="style/main.css">
</head>
<body>
<div id="wrapper">
    <?php include('menu.php');?>

<form action="" method="POST" enctype="multipart/form-data">
<p>upload single pic, Less than 2mb.</p>
<p><label>Title</label><br />
        <input type='text' name='picTitle' value='<?php if(isset($error)){ echo $_POST['picTitle'];}?>'></p>

<p><label>photo courtesy</label><br />
        <input type='text' name='picCourtesy' value='<?php if(isset($error)){ echo $_POST['picCourtesy'];}?>'></p>
<?php
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}



    $query = "SELECT * FROM category";
     $result = mysqli_query ($conn,$query);?>
      <p><label >Category:</label><br/>
     <select multiple="multiple" name="catName[]">
   <?php
    while($r = mysqli_fetch_array($result)) {
    echo '<option value='.$r['catName'].'>'.$r['catName'].'</option>';
    }
    echo "</select>";
     ?></p>                                                                                                                     
        <input type="file" name="files[]" />
        <input type="submit"/>
    </form>
    </div>
    </body>

请帮帮我,希望你能理解我的问题

1 个答案:

答案 0 :(得分:1)

只需替换

$catName = implode(' ',$_POST['$catName']); 

$catName = implode(' ',$_POST['catName']); 

错误地你只是添加&#39; $&#39;在$_POST['$catName']