dongpo1203 2016-03-13 09:45
浏览 14
已采纳

每页重新加载时插入空数据

I have a problem about posting data to DB.When i reload the page,or go to the website,it sends null data to DB.Its because i haven't functioned the button(well,i mean an onclick event) for this.Directly wrote a php code under the form and it works if you fill the spaces such as text and textarea and click the button.Though,Im new to PHP,So what I exactly need is,learning how to function a button to insert data to database using PHP to prevent inserting data on every load of form.

Here is the code

<?php
        $host="localhost";
        $user="root";
        $password="";
        $db="mkappform";

        $con=mysqli_connect("$host","$user","$password","$db");
        if (! $con) die ("Unable to connect to database");

        $name=$_POST["name"];
        $nick=$_POST["nick"];
        $pvnick=$_POST["pvnick"];
        $age=$_POST["age"];
        $country=$_POST["country"];
        $timezone=$_POST["timezone"];
        $servers=$_POST["servers"];
        $checkban=$_POST["checkban"];
        $contact=$_POST["contact"];
        $history=$_POST["history"];
        $reasons=$_POST["reasons"];
        $whypastclans=$_POST["whypastclans"];
        $anyotherinfo=$_POST["anyotherinfo"];




        $query=mysqli_query($con,
        "INSERT INTO mkappform(name,nick,pvnick,age,country,timezone,servers,checkban,contact,history,reasons,whypastclans,anyotherinfo) 
        values('$name','$nick','$pvnick','$age','$country','$timezone','$servers','$checkban','$contact','$history','$reasons','$whypastclans','$anyotherinfo')");
        if($query == true)
        echo "Application was sent successfully!!"
        ?>
  • 写回答

2条回答 默认 最新

  • dongshuo8756 2016-03-13 10:03
    关注

    Use action attribute of your form. For example action="index.php?action=form" Because isset($_POST) && !empty($_POST) is most of time not enough if you are dealing with bigger webapp.

        if(isset($_GET["action"]
        {
           //Open database
           switch($_GET["action"])
        {
          case "form":
            //callYourForm
             form();
           break;
    
          case "anotherThing":
             anotherThing();
           break;
    //can do more things if needed..
        }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?