2016-03-19 3 views
0

Надеюсь, вы отлично справляетесь. У меня проблема, когда я не могу вставить данные в свою базу данных. Есть несколько причин, почему это происходит, поэтому не считайте это дублирующимся вопросом. Я проверил свой код. Для одной таблицы это сохраняет данные, но для этой таблицы. Он показывает, что одна и та же страница не найдена и данные не сохраняются в локальной базе данных. Надеюсь, ты поможешь мне, ребята. Заранее спасибо. :) Вот некоторые полезные фрагменты кода:Данные не сохраняются в моей базе данных

<?php 
include 'Header.php'; 
?> 
<style> 
    #first { 
     //margin-right: 100%; 
     //clear: both; 

    } 
    #first > img { 
     display: inline-block; 
     //float: left; 
    } 
    #first > p { 
     //float: left; 
     display: inline-block; 
     //margin-left: 60px; 
     //margin-bottom: 120px; 
    } 

</style> 
<!-- Post content here --> 

<!-- Then cmments below --> 
<h1>Comments</h1> 
<!--<?php ?> 
if (isset($_GET['id'])) { 
    $id = $_GET['id']; 
} elseif (isset($_POST['id'])) { 
    $id = $_POST['id']; 
} else { 
    echo '<p class="error"> Error has occured</p>'; 

    include 'footer.html'; 

    exit(); 
} 

$db = new Database(); 
    $dbc = $db->getConnection(); 

    $display = 10; //number of records per page 
    $pages; 

if(isset($_GET['p'])) //already calculated 
{ 
    $pages=$_GET['p']; 
} 
else 
{ 
//use select count() to find the number of users on the DB  
    $q = "select count(comment_id) from comments"; 
    $r = mysqli_query($dbc, $q); 
    $row = mysqli_fetch_array($r, MYSQLI_NUM); 
    $records=$row[0]; 

    if($records > $display) //calculate the number of pages we will need 
     $pages=ceil($records/$display); 
    else 
     $pages = 1; 
} 

//now determine where in the database to start 
if(isset($_GET['s'])) //already calculated 
    $start=$_GET['s']; 
else 
    $start = 0; 

//use LIMIT to specify a range of records to select 
// for example LIMIT 11,10 will select the 10 records starting from record 11 
    $q = "select * from users order by $orderby LIMIT $start, $display"; 

    $r = mysqli_query($dbc, $q); 

/*if ($r) 
{*/ 

$result = mysql_query("SELECT * FROM comments WHERE video_id= '" + + "'"); 
//0 should be the current post's id 
while($row = mysql_fetch_object($result)) 
{ 
?> 
<div class="comment"> 
By: <!--<?php /* echo $row->author; //Or similar in your table ?> 
    <p> 
    <?php echo $row->body; ?> 
    </p> 
    </div> 
    <?php 
    /*} */ 
?>*/--> 
<h1>Leave a comment:</h1> 
<form action="Comment.php" method="post"> 
    <!-- Here the shit they must fill out --> 
    <input type="text" name="comment" value="" /> 
    <input type="hidden" name="submitted" value="TRUE" /> 
    <input type="submit" name="submit" value="Insert"/> 
</form>'; 

<?php 
if (isset($_POST['submitted'])) { 
    $comment = ''; 

    $errors = array(); 

    if (empty($_POST['comment'])) 
     $errors[] = 'You should enter a comment to be saved'; 
    else 
     $comment = trim($_POST['comment']); 



    if (empty($errors)) { 
     include 'Comments_1.php'; 
     $comment_2 = new Comments(); 
     $errors = $comment_2->isValid(); 
     $comment_2->Comment = trim($_POST['comment']); 
     $comment_2->UserName = hamed871; 
     $comment_2->Video_Id = 1; 
     if ($comment_2->save()) { 
      echo '<div class="div_1"><div id="div_2">' . 
      '<h1>Thank you</h1><p> your comment has been' 
        . ' posted successfully</p></div></div>'; 
     } 
    } 
//First check if everything is filled in 
    /* if(/*some statements *//*) 
     { 
     //Do a mysql_real_escape_string() to all fields 

     //Then insert comment 
     mysql_query("INSERT INTO comments VALUES ($author,$postid,$body,$etc)"); 
     } 
     else 
     { 
     die("Fill out everything please. Mkay."); 
     } 
     ?> 

     id (auto incremented) 
     name 
     email 
     text 
     datetime 
     approved--> */ 
} 
    ?> 
    <!--echo '--><div id="first"> 
     <img src="http://www.extremetech.com/wp-content/uploads/2013/11/emp-blast.jpg?type=square" height="42" width="42"/> 
     <p>hamed1</p> 
    </div><!--';--> 
    <dl> 
     <dt>comment1</dt> 
     <dd>reply1</dd> 
     <dd>reply2</dd> 
    </dl> 
    <!--//} 
    /*else 
    { 

    }*/ 
    ?>--> 
    <?php 
    include 'Footer.php'; 
    ?> 

Мой комментарий Класс:

<?php 

include_once "DBConn.php"; 

class Comments extends DBConn { 
    private $tableName = 'Comments'; 
    //attributes to represent table columns 
    public $comment_Id = 0; 
    public $Comment; 
    public $UserName; 
    public $Video_Id; 
    public $Date_Time; 

    public function save() { 
     if ($this->getDBConnection()) { 
      //escape any special characters 
      $this->Comment = mysqli_real_escape_string($this->dbc, $this->Comment); 
      $this->UserName = mysqli_real_escape_string($this->dbc, $this->UserName); 
      $this->Video_Id = mysqli_real_escape_string($this->dbc, $this->Video_Id); 

      if ($this->comment_Id == null) { 
       $q = 'INSERT INTO comments(Comment, User_Id, Video_Id, Date_Time) values' . 
         "('" . $this->Comment . "','" . $this->User_Id . "','" . $this->Video_Id . "',NOW()')"; 
      } else { 
       $q = "update Comments set Comment='" . $this->Comment . "', Date_Time='" . NOW() ."'"; 
      } 


      // $q = "call SaveUser2($this->userId,'$this->firstName','$this->lastName','$this->email','$this->password')"; 

      $r = mysqli_query($this->dbc, $q); 

      if (!$r) { 
       $this->displayError($q); 
       return false; 
      } 

      return true; 
     } else { 
      echo '<p class="error">Could not connect to database</p>'; 
      return false; 
     } 

     return true; 
    } 

//end of function 
    public function get($video_id) { 
     if ($this->getDBConnection()) { 

      $q = "SELECT Comment, Date_Time, UserName FROM Comments WHERE Video='" . $userName."' order by time_stamp"; 
      $r = mysqli_query($this->dbc, $q); 

      if ($r) { 
       $row = mysqli_fetch_array($r); 
       $this->Comment = mysqli_real_escape_string($this->dbc, $this->Comment); 


       return true; 
      } 
      else 
       $this->displayError($q); 
     } 
     else 
      echo '<p class="error">Could not connect to database</p>'; 

     return false; 
    } 

    public function isValid() { 
     //declare array to hold any errors messages 
     $errors = array(); 

     if (empty($this->Comment)) 
      $errors[] = 'You should enter a comment to be saved'; 
     return $errors; 
    } 
} 
?> 

Выход шоу, когда я нажимаю кнопку вставки:
Не найдено

Запрошенный URL/IndividualProject /Comment.php не найден на этом сервере. Apache/2.4.17 (Win64) PHP/5.6.16 Сервер на локальном хосте Порт 80

+0

Вы можете вставить вывод? –

+0

@ LajosArpad Здесь я сделал bro – hamed871

+1

Попробуйте изменить эту строку '(isset ($ _ POST ['представлен']))' to '(isset ($ _ POST ['submit']))' – claudios

ответ

0

Я столкнулся с такой проблемой при работе на промежуточном сайте, потому что веб-хостинг может иметь разные ограничения и строгое ограничение. Теперь, что я сделал, это изменение имени файла, например:

Имя класса должно совпадать с именем файла, в котором он чувствителен к регистру.

Comment.php

class Comment extends DBConn { 

function __construct() { 
parent::__construct(); 
} 

//code here.. 

} 
+0

такая же ошибка произошла bro. даже после того, как я изменил его – hamed871

Смежные вопросы