dongtuojuan8998 2011-02-18 15:54
浏览 95
已采纳

PHP:简单的表单发布到数据库

I've been so out of touch with using PHP outside of content management systems that I've forgot some of the basic syntax etc. What I'm trying to build is a simple form that collects some user data and sends it to a database table called 'creathive_applications'.

Here is my HTML form:

                <form action="<?php bloginfo('home'); ?>" method="post">
                    <fieldset id="membershipform">                  
                        <ul class="clearfix">
                            <li id="li-status">
                                <span>I am a:</span>
                                <menu>
                                    <li><label for="student"><input disabled="disabled" type="radio" name="status" id="student" checked="checked" value="Graduate" /> Graduate</label></li>
                                    <li><label for="student2"><input disabled="disabled" type="radio" name="status" id="student2" value="Undergraduate" /> Undergraduate</label></li>
                                </menu>
                            </li>
                            <li id="li-firstname">
                                <label for="firstname">First Name</label> <input name="firstname" disabled="disabled" type="text" placeholder="First Name" id="firstname" title="First Name" />
                            </li>
                            <li id="li-lastname">
                                <label for="lastname">Last Name</label> <input name="lastname" disabled="disabled" type="text" placeholder="Last Name" id="lastname" title="Last Name" />
                            </li>
                            <li id="li-email">
                                <label for="email">Email address</label> <input name="email" disabled="disabled" type="text" placeholder="Email address" id="email" title="Email address" />
                            </li>
                            <li id="li-url">
                                <label for="url">URL</label> <input name="url" disabled="disabled" type="text" placeholder="URL of something you've made" id="url" title="URL of something you've made" />
                            </li>
                            <li id="li-buttons">
                                <input name="submit" type="submit" value="Send Application &#9658;" title="Send Application" onclick="alert('Invites available from March 2011');" />
                            </li>
                        </ui>
                    </fieldset>
                </form>

and here is the PHP jazz:

if(isset($_POST['submit']))

{

    $status = $_POST['status'];
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $email = $_POST['email'];
    $url = $_POST['url'];

    $host = '####';
    $username = '####';
    $pass = '####';

    mysql_connect($host,$username,$pass);
    mysql_select_db($username);

    $query = "INSERT INTO creathive_applications VALUES (NULL,'".$status."','".$firstname."','".$lastname."','".$email."','".$url."')";

    $result = mysql_query($query);

}

Can anyone help me fix this as I can't remember how to run my SQL statement and send the data to the database :/ Also I've added the database username and password as well as host but what about the database name? Where does that go again?

I know this is a rather n00b question, but any help would be much appreciated.

THANKS A LOT

  • 写回答

3条回答 默认 最新

  • dongliao3450 2011-02-18 16:15
    关注

    Well, with no error it's impossible to tell what to fix.
    However, there are some obvious things to do.
    See the comments:

    <?
    //show all possible errors. should be ALWAYS set to that level
    error_reporting(E_ALL); 
    echo "landed at form handler<br>";
    
    // sometimes buttons not being sent or gets misspelled
    if($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        echo "here goes POST processing<br>";
        $host = '####';
        $username = '####';
        $pass = '####';
    
        mysql_connect($host,$username,$pass);
        mysql_select_db($username);
    
        // all strings should be escaped
        // and it should be done after connecting to DB
        $status    = mysql_real_escape_string($_POST['status']);
        $firstname = mysql_real_escape_string($_POST['firstname']);
        $lastname  = mysql_real_escape_string($_POST['lastname']);
        $email     = mysql_real_escape_string($_POST['email']);
        $url       = mysql_real_escape_string($_POST['url']);
    
        $query = "INSERT INTO creathive_applications 
                  VALUES (NULL,'$status','$firstname','$lastname','$email','$url')";
    
        echo $query;
        // always run your queries this way to be notified in case of error
        $result = mysql_query($query) or trigger_error(mysql_error().". Query: ".$query);
        var_dump($result);
    }
    

    in case you still see no error, add this line temporarily to the top of the script

    ini_set('display_errors',1);
    

    and remove it immediately after you get the problem solved.

    EDIT added some debug info.
    If you see none of it's messages, you're sending your form to the wrong URL. If you see some of them, post it here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图