当我在提交表单中调用它时,我的php函数无法运行

时间:2016-03-31 06:25:39

标签: php html function

我有一个问题:我在html头中有一个函数,然后在正文中我有一个表单类型提交并运行onsubmit函数。它看起来我无法达到功能或去功能向数据库插入细节。我无法完成这个想法。请帮帮我。

<?php
function sida() { 
   $host = "localhost";
   $username = "root";
   $password = "";
   $databasename = "vinhcv_truonghoc";
   $connect = mysql_connect($host, $username, $password);
   $db = mysql_select_db($databasename);
   if (isset($_POST['comment']) && isset($_POST['name'])) {
       $comment = $_POST['comment'];
       $name = $_POST['name'];
       $q = "insert into comments values('', '$name', '$comment', CURRENT_TIMESTAMP)";
       echo $q;
       $insert = mysql_query($q);
       if (!$insert) { echo mysql_error(); }
       $id = mysql_insert_id($insert);

       $select = mysql_query("select name, comment, post_time from comments where name = '$name' and comment='$comment' and id='$id'");

       if ($row = mysql_fetch_array($select)) {
           $name = $row['name'];
           $comment = $row['comment'];
           $time = $row['post_time'];
      ?>
     <div class="comment_div"> 
       <p class="name">Posted By:<?php echo $name;?></p>
       <p class="comment"><?php echo $comment;?></p>    
       <p class="time"><?php echo $time;?></p>
    </div>
  <?php
       }
       exit;
    }
}
?>

和身体中的形式:

<form method="POST" onsubmit="sida()">
  <textarea id="comment" placeholder="Write Your Comment Here....."></tetarea>
  <br>
  <input type="text" id="username" placeholder="Your Name">
  <br>
  <input type="submit" value="Post Comment">
</form>

<div id="all_comments">
  <?php
    $host = "localhost";
    $username = "root";
    $password = "";
    $databasename = "vinhcv_truonghoc";

    $connect = mysql_connect($host,$username,$password);
    $db = mysql_select_db($databasename);

    $comm = mysql_query("select name,comment,post_time from comments order by post_time desc");
    while($row = mysql_fetch_array($comm))
    {
     $name = $row['name'];
     $comment = $row['comment'];
     $time = $row['post_time'];
   ?>

   <div class="comment_div"> 
      <p class="name">Posted By:<?php echo $name;?></p>
      <p class="comment"><?php echo $comment;?></p> 
      <p class="time"><?php echo $time;?></p>
   </div>

   <?php
    }
    ?>
</div>

在正文中它可以连接到数据库来获取信息,这意味着在连接数据库时没有错误,那么为什么它不能插入数据库呢?

3 个答案:

答案 0 :(得分:1)

您需要做的是评论: -

  <form method="POST" ><!-- remove  onsubmit="sida()"-->
  <textarea id="comment" placeholder="Write Your Comment Here....." name = "comment"></textarea><!-- add name attribute -->
  <br>
  <input type="text" id="username" placeholder="Your Name" name = "username"><!-- add name attribute -->
  <br>
  <input type="submit" value="Post Comment">
 </form>

  <div id="all_comments">
  <?php
    $data = array(); // define empty array
    if(isset($_POST["comment"]) && isset($_POST["username"])){ // check with posted value not button value
        $host="localhost";
        $username="root";
        $password="";
        $databasename="vinhcv_truonghoc";
        $i = 0; // DEFINE COUNTER
        $connect=mysqli_connect($host,$username,$password,$databasename); // mysql_* is deprecated so use mysqli_* or PDO
        if($connect){ // IF CONNECTION ESTABLISHED
        $comment = mysqli_real_escape_string($connect,$_POST['comment']); // Prevent from SQL Injection
        $username = mysqli_real_escape_string($connect,$_POST['username']); // Prevent from SQL Injection
            $query = mysqli_query ($connect,"INSERT INTO comments (username,comment) VALUES ('".$username."','".$comment."')"); // check and change table name as well as column name
            if($query){
                echo "Inserted Successfully";
            }else{
                echo "Problem occur in insertion because of".mysqli_error($connect);
            }
            $comm = mysqli_query($connect,"select name,comment,post_time from comments order by post_time desc");
            if($comm){ // IF QUERY EXECUTED
                while($row=mysqli_fetch_array($comm)){
                    $data[$i]["name"]       =   $row['name']; // ASSIGN VALUES TO THE ARRAY
                    $data[$i]["comment"]    =   $row['comment'];
                    $data[$i]["time"]       =   $row['post_time'];
                    $i++;
                }
            }else{
                echo "Query execution failed because of".mysqli_error($connect);
            }
        }else{
            echo'connection problem because of'.mysqli_connect_error();
        }
    }else{
        echo "All fields are need to fill properly";

    }
   ?>
    <?php foreach ($data as $dat){?> <!-- ITERATE THROUGH ARRAY -->
           <div class="comment_div"> 
              <p class="name">Posted By:<?php echo $data['name'];?></p>
              <p class="comment"><?php echo $data['comment'];?></p> 
             <p class="time"><?php echo $data['time'];?></p>
           </div>

    <?php } ?>
  </div>

答案 1 :(得分:0)

好像你没有将任何参数传递给$_POST数组。您需要将name属性传递给输入字段。由于您要运行PHP服务器端功能,因此需要使用action属性。

像这样:

 <form method="POST" action="sida()">
  <textarea name="comment" id="comment" placeholder="Write Your Comment Here....."></textarea>
  <br>
  <input name="name" type="text" id="username" placeholder="Your Name">
  <br>
  <input type="submit" value="Post Comment" name="submit">
 </form>

答案 2 :(得分:0)

  1.     $host="localhost";
        $username="root";
        $password="";
        $databasename="vinhcv_truonghoc";
    
    
           function connect()   {
        global $host,$username,$password,$databasename;
        $con=mysql_connect($host,$username,$password) or die ("ConnectionFailed");
        mysql_select_db($databasename,$con) or exit ("Failed to connect");
        return $con;       }    
    
    
      function iud($query) //Insert,Update,Delete   {
        $con=connect();
        $result=mysql_query($query,$con);
        $n=mysql_affected_rows($con);
        mysql_close($con);
        return $n;   }  
    
    
      function select ($query) //Select   {
        $con=connect();
        $result=mysql_query($query,$con);
        mysql_close($con);    return $result;   }   
    
    
    
    
      if(isset($_REQUEST['submit']))   {
        $name=$_REQUEST['name'];
        $comment=$_REQUEST['comment'];
        $query="INSERT INTO `comments`(`name`, `comment`, `post_time`)
        VALUES('$name','$comment',CURRENT_TIMESTAMP)";
        $n=iud($query); 
        echo $n;       }
    
      ?>
    
    
      <html>   <head></head>   <body>   <form method="GET">    <table>  
    <tr>   <td>Name</td>   <td><Input type="text" name="name" id=""
    class=""   placeholder="Name.."></td>      </tr>
           <tr>   <td>Comment</td>   <td><textarea name="comment" rows="5" cols="40" placeholder="Comment  
    
      Here.."> </textarea></td>    </tr>
           <tr>   <td></td>   <td><Input type="submit" name="submit" id="" class="" value="submit">
    
      </td>    </tr>
           </table>   </form>
           <!-- Showing Result -->     <table>   <?php   $query="Select * from comments ORDER BY coid DESC"; //Coid Is A Primary Key  
    $result=select($query);   $n=mysql_num_rows($result);   if($n>0)   {
    while($data=mysql_fetch_array($result))     extract($data);   }    ?>  
    <tr>       <td><?php echo @$name; ?></td>      </tr>   <tr>   <td><?php
    echo @$comment; ?></td>    </tr>   </table>
               </body>     </html>
    

    我希望它对你有用:)