【问题标题】:php script working on locally on the system but not on the serverphp 脚本在系统本地工作,但不在服务器上
【发布时间】:2017-04-12 22:35:33
【问题描述】:

请帮助我查看下面的代码,看看哪里有错误,因为代码在我的系统上本地运行,但在服务器上运行不佳。它可以将图像移动到文件夹中,但数据不会移动到数据库中。我已经检查并检查过,但不能真正将我的手放在错误上,我很快就会非常感谢某种帮助。谢谢。

<?php


include_once("../php/db_connection.php");
include_once("php/admin_session.php");

?>
<?php
 // ADD NEW PROPERTY
 $error_msg = '';
if(isset($_POST['title'])){
$title = $_POST['title'];
$names = $_POST['name'];
$description = $_POST['description'];
$price = $_POST['price'];
$category = $_POST['category'];
$location = $_POST['location'];
$map = $_POST['map'];
$img = "../rent_images/$names";

if(isset($_FILES['product_image'])){
    foreach($_FILES['product_image']['tmp_name'] as $key => $tmp_name ){
        $file_name = $key.$_FILES['product_image']['name'][$key];
        $file_size =$_FILES['product_image']['size'][$key];
        $file_tmp =$_FILES['product_image']['tmp_name'][$key];
        $file_type=$_FILES['product_image']['type'][$key];  
        if($file_size > 2097152){
            $error_msg ='File size must be less than 2 MB';
        }       
        $desired_dir=$img;
        if(empty($error_msg)==true){
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir", 0755);        // 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) ;               
                }
        }
    }
    $sql_addProduct = mysqli_query($connection, "INSERT INTO rents(id,title,name,description,price,category,location,map,date_added) VALUES('','$title','$names','$description','$price','$category','$location','$map',now())");
    if(!$sql_addProduct){
        $error_msg = '<div class="atention">Couldn\'t upload the images or property details, please try again</div>';
    }else{
       header("location: property_list.php");
       exit();
    }
}
}
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Admin Panel</title>
<link rel="stylesheet" type="text/css" href="style/bootstrap.css">
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen"/>

<script type="text/javascript" src="js/jquery-1.11.3.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript">
    $(function(){
        var pull = $('#pull');
        menu = $('nav ul');
        menuHeight = menu.height();

        $(pull).on('click', function(e){
            e.preventDefault();
            menu.slideToggle();
        });
    });
</script>
<script type="text/javascript">
    $(window).resize(function(){
        var w = $(window).width();
        if(w > 320 && menu.is(':hidden')){
            menu.removeAttr('style');
        }
    });
</script>
</head>

<body>
<div class="container">
<div>
    <?php include_once("template/header.php"); ?>
</div>
<div class="row">
    <p><a href="property_list.php"> Click Here Update Property</a></p>
    <table width="100%" cellpadding="0" cellspacing="0" border="0">
            <tr>
                <td valign="top" width="100%">
                    <section id="main_content">
                        <h1>Add Product</h1>
                        <?php echo $error_msg; ?>
                        <form action="" method="post" enctype="multipart/form-data">
                            <input type="text" class="input-login" name="title" placeholder="Title" maxlength="100"><br><br>
                            <input type="text" class="input-login" name="name" placeholder="Name" maxlength="100"><br><br>
                            <textarea cols="50" rows="10" name="description" placeholder="Detailed description"></textarea><br><br>
                            <input type="text" class="input-login" name="price" placeholder="Price" maxlength="15"><br><br>
                            <input name="category" class="input-login" placeholder="Category" maxlength="50"><br><br>
                            <input name="location" class="input-login" placeholder="location" maxlength="50"><br><br>
                            <textarea cols="50" rows="10" name="map" class="input-login" placeholder="Insert Google map address"></textarea><br><br>
                            <input type="file" name="product_image[]" multiple title="Select property images"><br>
                            <br/>
                            <button id="submitBTN">Add Product</button>
                        </form>
                    </section>
                </td>
            </tr>
        </table>
</div>
<div>
    <?php include_once("../template/footer.php"); ?>
</div>
</div>
</body>
</html>

【问题讨论】:

  • 您可能对间接目录名称有疑问。 Apache 可能在不同的目录中执行它
  • 请详细说明。

标签: javascript java php html css


【解决方案1】:

看起来您的 db-connection 或 admin_session 包含之一是错误的。假设您将它们放在同一个文件夹中。

【讨论】:

  • db-connection 适用于项目中的所有脚本,这就是为什么它被放置在主文件夹中而 admin-session 仅适用于后端的原因。这对代码的执行有影响吗?
【解决方案2】:

打开您的数据库配置文件并更改您的用户、主机和密码

../php/db_connection.php

【讨论】:

  • 你的意思是把它改成我在服务器上创建的那个吗?如果这就是你的意思,我已经这样做了,除非你的意思是我应该再次改变它。本地系统上的用户、主机和密码与服务器不同。
  • 你能显示你的 ../php/db_connection.php 文件吗?不是密码之类的东西,请用另一个密码而不是真实密码发布,我只是想看看你文件的 sintax
  • 好的,连接如下所示,因为我已经更新了它,但在服务器端仍然出现同样的错误:connect_error){ echo '连接不正常';出口(); } ?>
  • 为什么选择本地主机?在服务器上创建一个新数据库并将其更改为新配置
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-28
  • 1970-01-01
  • 2016-08-04
相关资源
最近更新 更多