douya1974 2015-04-20 14:47
浏览 241
已采纳

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

1条回答 默认 最新

  • drfqfuhej48511519 2015-04-20 15:35
    关注

    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); 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私