为什么列中的值始终为1?

时间:2014-07-19 11:30:44

标签: php mysql

我有一个表员工,有两列

  1. Tiny 与textarea的内容

  2. 照片,带有图片名称。

  3. 现在当我在textarea中写入 Tiny 列时,值总是 1 ,但在第二个中有pics的名称。这段代码有什么问题?

    专栏Tiny的详细信息:

    Tiny Varchar(30) NULL 
    

    标记和代码:

    <div id="main">
        <h2>Posta un nuovo film o una serie</h2>
        <p>
        <form enctype="multipart/form-data" action="amministrazione.php" method="POST"> 
        <textarea id="name" name="tiny" rows="15" cols="80"></textarea><br> 
        <label for="photo">Copertina DVD | Serie  </label>
        &nbsp;&nbsp;&nbsp;<input type="file" name="photo"><br><br> 
     <input type="submit" value="Crea"> 
     <?php 
    
     //This is the directory where images will be saved 
     $target = "../image/"; 
     $target = $target . basename( $_FILES['photo']['name']); 
    
     //This gets all the other information from the form 
    
     $tiny = (isset($_POST['tiny'])); 
    
     $pic= ($_FILES['photo']['name']); 
    
    
    
     mysql_query("INSERT INTO employees VALUES ('$tiny', '$pic')") ; 
    
     //Writes the photo to the server 
     if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
     { 
    
     //Tells you if its all ok 
     echo "The file ". basename(isset($_FILES['uploadedfile']['name'])). " has been uploaded, and your information has been added to the directory"; 
     } 
     else { 
    
     //Gives and error if its not 
     echo "Sorry, there was a problem uploading your file."; 
     } 
    
     ?> 
     </form>
    
        </p>
        </div>
    

3 个答案:

答案 0 :(得分:3)

$tiny = (isset($_POST['tiny'])); 

应该是

$tiny = $_POST['tiny'];

此外:

  1. 您正在使用已弃用的mysql_ *函数
  2. 您的代码中可能有SQL注入(将&#39;放入textarea;))

答案 1 :(得分:1)

$tiny = (isset($_POST['tiny'])); 

应该是

$tiny = (isset($_POST['tiny']))  ? $_POST['tiny'] : null

答案 2 :(得分:0)

您正在使用isset()函数。这将始终返回True或False,1或0。

$tiny = (isset($_POST['tiny'])); 

http://php.net/manual/en/function.isset.php

使用它将正确设置变量。

$tiny = $_POST['tiny'];

尝试重写这样的查询,以提高安全性和稳定性。通过指定列然后指定数据,具体说明数据的位置。还可以在反引号中保护您的表格参考。

INSERT INTO `employees` (`columnname1`, `columnname2`) VALUES ('$tiny', '$pic')

最后,考虑使用当前版本的mySQLi作为您正在使用的版本已过时。

http://php.net/manual/en/book.mysqli.php

希望您能找到这个有用的