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

当我在提交表单中调用它时,我的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?

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>

Seems like you are not passing any parameters to the $_POST array. You need to pass the name attribute to your input fields. Since you are going to run a PHP server-side function you need to use the action attribute.

Like this:

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

  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>
    

    I hope It will Work For you :)