2017-10-18 3 views
0

を実行していない私は、PHPコードは次のページがあります。PHPクエリ

<?php 

    include ('databaseconnect1.php'); 

    $sql1= "SELECT Categoryid, Categoryname, Categorydescription 
      FROM Categories"; 
    $result1 = mysqli_query($db,$sql1); 

    if (!$result1){ 
    echo "<font color = 'Green' .<p> No Category Found, Contact the 
      administrator </p> </font>"; 
    } 

function getPosts() 
    { 
    $posts = array(); 
    $posts[0] = $_POST['topic_subject']; 
    $posts[1] = $_POST['date']; 
    $posts[2] = $_POST['topic_category']; 
    $posts[3] = $_SESSION['userid']; 
    return $posts; 
    } 

if (isset($_POST['createtopicbutton'])) 
    {   
$data = getPosts(); 

    $sql2 = "INSERT INTO Topics(Topic_subject, Topic_date, 
       Topic_category, Topic_by) 
      VALUES('$data[0]','$data[1]', '$data[2]', '$data[3]')"; 

    $result2 = mysqli_query($db,$sql2); 

if ($result2){ 
     echo "<font color = 'Green' .<p> Topic Successfully Created </p> 
    </font>"; 
}else{ 
    echo "<font color = 'Green' .<p> Topic NOT! Successfully Created </p> 
      </font>"; //This is the result I am getting specifically 
    }             
    } 
?> 

を、これは、同じページ上のHTMLコードです:

<form method = "post" action = "" > 
    <table cellspacing="15"> 

    <tr> 
     <th>Subject </th> 
     <td><input type = "text" name = "topic_subject" /> </td> 
    </tr> 

    <tr> 
     <th>Category </th> 
     <?php echo '<td> <select name="topic_category"> '; 

     while($row = mysqli_fetch_assoc($result1)) 
     { 
     echo '<option value="' . $row['Categoryid'] . '">' . 
      $row['Categoryname'] . '</option>'; 
     } 
    echo '</select></td>'; 
     ?> 
    </tr> 

    <tr> 
     <th>Current Date </th> 
     <td><input type = "text" name = "date" /> </td> 
    </tr> 

    <tr> 
    <th> </th> 
    <td> <input type = "submit" value = "Create Topic!" name = 
      "createtopicbutton" /> </td> 
    </tr> 

    </table> 
    </form> 

私は助けを必要と特定の部分"createtopicbutton"が押されたとき、私は次の結果しか得られないということです。

<?php Topic NOT! Successfully Created?> 

次の表は、トピックテーブルの再:Table Structure from phpmyadmin

、これは全体的なデータベースのテーブル構造の一部である:Linkages of tables

は、これまでのところ私は、ブラケットの全てが適切に閉じられていることを確認することや構文を少し変更して試してみました失敗。しかし、うまくいったのは、phpmyadminを使ってデータを入力すると、何とか動作するということだけです。だから私はそのフォームにあると感じる問題。助けてもらえますか?

答えて

0
 <?php 
@session_start(); 
$_SESSION['userid'] = 1; 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "stack"; 

// Create connection 
$db = mysqli_connect($servername, $username, $password, $dbname); 

// Check connection 
if (!$db) { 
    die("Connection failed: " . mysqli_connect_error()); 
} 
    $sql1= "SELECT Categoryid, Categoryname, Categorydescription 
      FROM Categories"; 
    $result1 = mysqli_query($db,$sql1); 

    if (!$result1){ 
    echo "<font color = 'Green' .<p> No Category Found, Contact the 
      administrator </p> </font>"; 
    } 

function safe_insert($data) 
    { 
     $data = trim($data); 
     $data = stripslashes($data); 
     $data = htmlspecialchars($data); 
     return $data; 
    } 
/* 
    you can validate more than this such as the following 
    string length 
    use preg match to only validate number but l used is_numeric ie 
       if (!preg_match("/^[0-9]*$/",$data)) { 
        return false; 
       } 
    limit post based on day 
    re structure and change mysql to date . 
    table name ie tbl_topic category_id category_description ... you get the idea 
    and many more 
    ................. happy coding 
*/ 
if (isset($_POST['createtopicbutton'])) 
    {   
     if(isset($_POST['topic_subject']) && isset($_POST['date']) && isset($_POST['topic_category'])){ 
      $state = true; 
      $errors = ''; 
      if(trim($_POST['topic_subject']) == ''){ 
       $errors .= "subject is empty"; 
       $state = false; 
      } 
      if(trim($_POST['date']) == ''){ 
       $errors .= "date is empty"; 
       $state = false; 
      } 
      if(!is_numeric(trim($_POST['topic_category']))){ 
       $errors .= "topic category is should be number"; 
       $state = false; 
      } 

      if($state){     
       $subject = mysqli_real_escape_string($db , safe_insert($_POST['topic_subject'])); 
       $date = mysqli_real_escape_string($db , safe_insert($_POST['date'])); 
       $topic= mysqli_real_escape_string($db , safe_insert($_POST['topic_category'])); 
       $user_id = mysqli_real_escape_string($db , safe_insert($_SESSION['userid'])); 
       $sql2 = "INSERT INTO Topics(Topic_subject, Topic_date, Topic_category, Topic_by) VALUES('$subject','$date', '$topic', '$user_id')"; 

       $result2 = mysqli_query($db,$sql2); 

       if ($result2){ 
        echo "<font color = 'Green' .<p> Topic Successfully Created </p> </font>"; 
       }else{ 
        echo "<font color = 'Green' .<p> Topic NOT! Successfully Created </p> 
        </font>"; //This is the result I am getting specifically 
       } 
      }else{ 
       echo $errors; 
      } 
     } 
     else{ 
      echo 'Something fishy'; 
     }             
    } 
?> 

<form method = "post" action = "" > 
    <table cellspacing="15"> 
    <tr> 
     <th>Subject </th> 
     <td><input type = "text" name = "topic_subject" /> </td> 
    </tr> 
    <tr> 
     <th>Category </th> 
     <?php echo '<td> <select name="topic_category"> '; 

     while($row = mysqli_fetch_assoc($result1)) 
     { 
     echo '<option value="' . $row['Categoryid'] . '">' . 
      $row['Categoryname'] . '</option>'; 
     } 
    echo '</select></td>'; 
     ?> 
    </tr> 
    <tr> 
     <th>Current Date </th> 
     <td><input type = "text" name = "date" /> </td> 
    </tr> 
    <tr> 
    <th> </th> 
    <td> <input type = "submit" value = "Create Topic!" name = 
      "createtopicbutton" /> </td> 
    </tr> 
    </table> 
    </form> 

    <?php 
    // sample data // 

    /* 

CREATE TABLE `categories` (
    `Categoryid` int(11) NOT NULL, 
    `Categoryname` varchar(255) NOT NULL, 
    `Categorydescription` varchar(255) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

INSERT INTO `categories` (`Categoryid`, `Categoryname`, `Categorydescription`) VALUES 
(1, 'fake 1', 'lprem,djbch schjcwdc k'), 
(2, 'fake 2', 'kjdncsjkc dcjdjkds dskjsdkj'); 


CREATE TABLE `topics` (
    `topic_id` int(11) NOT NULL, 
    `topic_subject` varchar(255) NOT NULL, 
    `topic_date` varchar(255) NOT NULL, 
    `topic_category` int(11) NOT NULL, 
    `topic_by` int(11) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

-- 
-- Dumping data for table `topics` 
-- 

INSERT INTO `topics` (`topic_id`, `topic_subject`, `topic_date`, `topic_category`, `topic_by`) VALUES 
(1, 'sweet', 'chiil', 1, 1), 
(8, 'jkfdjk', 'kjkjd', 1, 1), 
(31, 'klds', 'los', 2, 1), 
(32, 'suceess topic', 'date', 1, 1), 
(33, 'ksdl', 'sdlksda', 1, 1), 
(34, 'melody', 'sdjsjssj', 2, 1); 

-- 
-- Indexes for dumped tables 
-- 

-- 
-- Indexes for table `topics` 
-- 
ALTER TABLE `topics` 
    ADD PRIMARY KEY (`topic_id`); 

-- 
-- AUTO_INCREMENT for dumped tables 
-- 
    */ 
    ?> 
+0

私はリットル原因示されているエラーは、ここでそれをテストしている何私のコードから同じ結果 –

+0

を取得し、その作業 –

+0

いますまだあなたがここにいたすべてのものを試してみましたかuがテストしている環境を変更してみてください –