2017-10-18 18 views
0

J'ai une page avec le code php suivant:PHP requête est non exécution

<?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 
    }             
    } 
?> 

ce qui est le code HTML sur la même page:

<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> 

La partie spécifique je besoin d'aide est que lorsque le « createtopicbutton » est pressé je ne reçois le résultat suivant:

<?php Topic NOT! Successfully Created?> 

ce qui suit est la table structu re de la table Sujets: Table Structure from phpmyadmin

et cela fait partie de la structure de la table globale de base de données: Linkages of tables

Jusqu'à présent, je l'ai essayé de faire en sorte que tous les supports sont bien fermés et changer la syntaxe un peu et Sans succès. Cependant la seule chose qui a fonctionné est quand j'entre des données par phpmyadmin cela fonctionne d'une manière ou d'une autre. Donc, le problème que je ressens se pose avec la forme. Pouvez-vous s'il vous plaît aider?

Répondre

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

J'ai essayé tout ce que vous avez fait ici mais je reçois le même résultat –

+0

De mon code ce qui est l'erreur étant l 'une cause ont testé ici et son travail –

+0

Ou essayez de changer l'environnement u sont des tests –