PHP多个文件上传多个输入

问题描述:

I'm new to PHP. Now i have a problem with files upload. All files are moved, but It didn't store file's name to database. and it didn't show error. I have no idea to fix this one. Please help me out.

<form method="post" action="index.php?insert_ads" enctype="multipart/form-data">
    <input type="file" name="b1" id="b1"/>

    <b>Link</b></br>
    <input type="text" id="b1l" name="b1l" class="form-control"/></br>

    <b>Home Small</b> <b style="color: blue;">100 x 100 px</b></br>
    <input type="userfile" name="b2" id="b2"/><br>

    <b>Link</b></br>
    <input type="text" id="b2l" name="b2l" class="form-control"/></br>

    <input type="submit" name="submit" value="Publish"/>

</form></br>

<?php 

if(isset($_POST['submit'])){

    $b1 = $_FILES['b1']['name'];
    $tmp1 = $_FILES['b1']['tmp_name']; 
    $b1l = $_POST['b1l'];
    $b2 = $_FILES['b2']['name'];
    $tmp2 = $_FILES['b2']['tmp_name'];
    $b2l = $_POST['b2l'];

    move_uploaded_file($tmp1,"ads/$b1");
    move_uploaded_file($tmp2,"ads/$b2");

    $insert_posts = "insert into ads (b1,b2) value ('$b1','$b2')";
    $run_posts = mysql_query($insert_posts);
}
?>

我是PHP的新手。 现在我有文件上传的问题。 所有文件都被移动,但它没有将文件的名称存储到数据库。 并没有显示错误。 我不知道要解决这个问题。 请帮帮我。 p>

 &lt; form method =“post”action =“index.php?insert_ads”enctype =“multipart / form-data”&gt; 
&lt  ; input type =“file”name =“b1”id =“b1”/&gt; 
 
&lt; b&gt;链接&lt; / b&gt;&lt; / br&gt; 
&lt; input type =“text”id =  “b1l”name =“b1l”class =“form-control”/&gt;&lt; / br&gt; 
 
&lt; b&gt; Home Small&lt; / b&gt;  &lt; b style =“color:blue;”&gt; 100 x 100 px&lt; / b&gt;&lt; / br&gt; 
&lt; input type =“userfile”name =“b2”id =“b2”/&gt;&lt;  ; br&gt; 
 
&lt; b&gt;链接&lt; / b&gt;&lt; / br&gt; 
&lt; input type =“text”id =“b2l”name =“b2l”class =“form-control”/&gt  ;&lt; / br&gt; 
 
&lt; input type =“submit”name =“submit”value =“Publish”/&gt; 
 
&lt; / form&gt;&lt; / br&gt; 
 
&lt;?  php 
 
if(isset($ _ POST ['submit'])){
 
 $ b1 = $ _FILES ['b1'] ['name']; 
 $ tmp1 = $ _FILES ['b1']  [ 'tmp_name的值'];  
 $ b1l = $ _POST ['b1l']; 
 $ b2 = $ _FILES ['b2'] ['name']; 
 $ tmp2 = $ _FILES ['b2'] ['tmp_name']; \  n $ b2l = $ _POST ['b2l']; 
 
 move_uploaded_file($ tmp1,“ads / $ b1”); 
 move_uploaded_file($ tmp2,“ads / $ b2”); 
 
 $ insert_posts  =“插入广告(b1,b2)值('$ b1','$ b2')”; 
 $ run_posts = mysql_query($ insert_posts); 
} 
?&gt; 
  code>  
  div>

Notwithstanding any issues about using mysql_query or injection attacks, there are a number of things that could be going wrong here.

One option is that the query is executing, but you haven't assigned the $b1 and $b2 variables correctly. This would be the case if rows are being added to the database, but the rows are empty (e.g., SELECT b1, b2 FROM db.ads" returns rows of '',''); in that case, you probably just aren't extracting the name attribute from the $_FILES variable correctly. You can run var_dump($_FILES); to see more information about it and figure out what you need to get.

Another possibility is that the query is not executing. Again, this may be for a couple of reasons -- maybe (somehow) it's not reaching that point in the code. You can test that like so:

$insert_posts = "insert into ads (b1,b2) value ('$b1','$b2')";
echo $insert_posts; // if this shows up, you're running the next line also
$run_posts = mysql_query($insert_posts);

Another option is that your error reporting level is not capturing an error. A likely cause of this is that you have not connected to the database -- according to the mysql_query documentation...

If no connection is found or established, an E_WARNING level error is generated.

A E_WARNING level error will allow the program to continue to execute unless you have configured your program to behave differently.

Finally, you may have a syntax error (and indeed it seems you do -- VALUES, not VALUE); according to the documentation, mysql_query returns false on error -- it does not throw an error.

You can rig it to do so by testing for false and using the mysql_error function to get the error:

$run_posts = mysql_query($insert_posts);
if ($run_posts === false) { 
  trigger_error("Error in SQL!
" + mysql_error(), E_USER_ERROR); 
}