将localhost迁移到服务器时查询数据库时出错

时间:2015-12-08 21:14:11

标签: php mysql database mysqli migration

所以我创建了一个表单,可以将地点,类别和图片添加到我的数据库中。这一切都在我的localhost上工作正常,并将数据正确插入我的数据库。但是,当我在我的网站上放置表单时,我只是得到了“错误查询数据库”。消息。

我已经更改了连接文件,因此他们也拥有关于我的服务器,数据库名称,用户名和密码的正确信息。

我以两种不同的方式连接到我的数据库。一个使用mysqli:

<?php
$dbc = mysqli_connect("localhost","root","","ssdb") or die (mysqli_connect_error() );
mysqli_set_charset($dbc, 'utf8'); 
?>

另一个使用mysql:

<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("ssdb",$con);
?>

在我的表单上,我正在使用mysqli函数连接到我的数据库以检索您可以选择的类别,以便一个应该正常工作?

我已经读过mysql在php版本7+上不起作用,但我的网站目前正在运行php 5.6..something。

我的表单预览可以在这里看到:http://stephanolsen.dk/imageupload/upload.php

我不知道它是否有帮助,但我的代码可以在下面看到:

我的表格:

<?php include('header');?>

</head>
<body>
  <div class="wrap">
    <div class="container">
      <div class="page-header">
        <h1>Tilføj sted <small><br>Skriv adresse og beskrivelse</small></h1>
      </div>
      <form id="form" method="post" name="formsub" action="formaction.php" enctype="multipart/form-data">
        <div class="form-group">
          <label for="title">Titel</label>
          <input type="text" name="title" class="form-control" id="title" placeholder="Titel">
        </div>
        <div class="form-group">
          <label for="adresse">Adresse</label>
          <input type="text" name="adresse" class="form-control" id="adresse" placeholder="Adresse">
        </div>
        <div class="form-group">
          <label for="beskrivelse">Beskrivelse</label>
          <input type="text" name="beskrivelse" class="form-control" id="beskrivelse" placeholder="Beskrivelse">
        </div>

<!-- Choose category! -->

<?php
include('includes/connectdb.php');

/* Selects id and name from the table 'category' */
$query = "SELECT id, name FROM category";
$result_category = mysqli_query($dbc,$query);
?>

<!-- creating a form -->
          <label for="Category">Category</label>
          <br />
            <!-- iterate through the WHILE LOOP  -->
            <?php while($row = mysqli_fetch_array($result_category)): ?>
              <!-- Echo out values {id} and {name}   -->
              <input type="checkbox" name="category[]" value=" <?php echo $row['id']; ?> "><?php echo $row['name'] . '<br />'; ?>
            <?php endwhile; ?>

<!-- Upload image! -->

        <br/>
            <input type="file" name="image" />
            <br/><br/>

           <input type="submit" name="Submit" value="Submit" class="btn btn-default"/>       
         </form>

    </div> <!-- END CONTAINER -->
  </div> <!-- END WRAP -->
<br />
</body>
</html>

行动档案:

<?php
  include('includes/connectdb.php');

  $beskrivelse = $_POST['beskrivelse'];
  $adresse = $_POST['adresse'];
  $title = $_POST['title'];
  $query = "INSERT INTO sted (title, beskrivelse, adresse)  VALUES ('$title','$beskrivelse', '$adresse')";
  mysqli_query($dbc,$query) or die('Error querying database.');
  $last_id = mysqli_insert_id($dbc);

  echo '<div class="wrap">';
  echo  '<div class="container">';
  echo      '<h4>Place has been added!</h4>';
  echo  '</div>';
  echo '</div>';
        ?> 

<?php
  include('includes/connect.php');
$checkbox = $_POST['category'];

  for ($i=0; $i<sizeof($checkbox);$i++) {
    $query = "INSERT INTO placecategory (place_id, category_id) VALUES ('$last_id','".$checkbox[$i]."')";
    mysql_query($query) or die(mysql_error());
  }
  echo "Category is inserted";

?>

<?php
    ini_set('mysql.connect_timeout',300);
    ini_set('default_socket_timeout',300);
?>

        <?php
      if(getimagesize($_FILES['image']['tmp_name'])== FALSE)
      {
        echo "Please select an image.";
      }
      else
      {
        $image= addslashes($_FILES['image']['tmp_name']);
        $name= addslashes($_FILES['image']['name']);
        $place_id=$last_id;
        $image= file_get_contents($image);
        $image= base64_encode($image);
        saveimage($name,$image,$place_id);
      }

        displayimage();
        function saveimage($name,$image,$place_id)
        { 
        include('includes/connect.php');
      $qry="INSERT INTO pictures (name,image,place_id) values ('$name','$image','$place_id')";
      $result=mysql_query($qry,$con);
      if($result)
      {
                //echo "<br/>Image uploaded.";
            }
            else
            {
                //echo "<br/>Image not uploaded.";
            }
    }
            function displayimage()
            {
        include('includes/connect.php');
      $qry="SELECT * FROM pictures";
      $result=mysql_query($qry,$con);
      while($row = mysql_fetch_array($result))
      {
        echo '<img height="300" width="300" src="data:image;base64,'.$row['image'].' "> ';
      }
      mysql_close($con); 
    }
  ?>

connectdb.php

<?php
# Connect on localhost for user root
# with password xxxxxxx to database userloginwebsystem 

$dbc = mysqli_connect("localhost","root","","ssdb") or die (mysqli_connect_error() );

#Display MySQL version and host


#if(mysqli_ping($dbc))
#{ echo 'MYSQL Server' .mysqli_get_server_info($dbc). ' <br> on ' . mysqli_get_host_info($dbc); }


# Set encoding to match PHP script

mysqli_set_charset($dbc, 'utf8'); 
?>

connect.php

<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("ssdb",$con);
?>

1 个答案:

答案 0 :(得分:0)

除非您有VM,否则您可能需要使用托管公司提供给您的代码(IP地址,用户名等)连接到您的SQL数据库。