duanrang3357 2016-03-31 06:25 采纳率: 0%
浏览 41
已采纳

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

I have a proble: I have a function in the html head, and then in the body I have a form type submit and run the function onsubmit. It seeems I cant reach the function or go to function to insert details to database. I cant complete this idea. Please help me this.

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

and the form in the body:

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

In the body it can connect to database to get information, that mean not thing wrong when connect with database, so why it cant insert to database?

  • 写回答

3条回答 默认 最新

  • duanmou9228 2016-03-31 06:50
    关注

    What you need to do is commented:-

      <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题