提交表单时将其提交到数据库

时间:2018-10-23 17:12:10

标签: php html mysql forms mysqli

我知道之前已经有人问过这个问题,并且我已经复制了其他问题的结果,但是仍然看不到结果。

提交表格时,我试图将结果添加到数据库中。

当前,该表单将运行,但是没有结果添加到数据库中。如果取消注释echo语句,则可以看到它,因此它正在IF语句中运行。

我知道我必须丢失一些显而易见的东西,因此非常感谢您的帮助。

谢谢!

**暂时不考虑安全漏洞和草率的HTML代码。在进行调整之前,只需要使步骤1生效即可。


表格代码

<form method="post" action="post-me.php">
    <div class="form-element">
        <label for="utm_url"><b>*</b> Enter URL</label>
        <input type="text" name="utm_url" id="utm_url">
    </div>  
    <div class="form-element">
        <label for="utm_campaign"><b>*</b> Enter Campaign</label>
        <select name="utm_campaign" id="utm_campaign">
            <option selected> </option>
            <option>test-campaign-1</option>
            <option>test-campaign-2</option>
            <option>test-campaign-3</option>
        </select>
    </div>
    <div class="form-element">
        <label for="utm_source"><b>*</b> Enter Source</label>
        <select name="utm_source" id="utm_source">
            <option selected> </option>
            <option>google</option>
            <option>facebook</option>
            <option>bing</option>
        </select>
    </div>
    <div class="form-element">
        <label for="utm_medium"><b>*</b> Enter Medium</label>
        <select name="utm_medium" id="utm_medium">
            <option selected> </option>
            <option>organic</option>
            <option>referral</option>
            <option>cpc</option>                
        </select>
    </div>  
    <div class="form-element">
        <label for="utm_content">Enter Content</label>
        <input type="text" name="utm_content" id="utm_content">
    </div>
    <div class="form-element">
        <label for="utm_keyword">Enter Keyword</label>
        <input type="text" name="utm_keyword" id="utm_keyword">
    </div>  
    <div class="form-element">
        <label for="utm_date">Date Created</label>
        <input type="date" name="utm_date" id="utm_date">
    </div>  
    <div class="form-element">
        <label for="utm_description">Enter Description</label>
        <input type="text" name="utm_description" id="utm_description">
    </div>  

    <input type="submit" name="submit">

</form>

发送代码(post-me.php)

<?php 

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

#DB Variables
$db['db_host'] = "localhost";
$db['db_user'] = "BLANK";
$db['db_pass'] = "BLANK";
$db['db_name'] = "BLANK";

#Changing values into constants.
foreach($db as $key => $value){
    define(strtoupper($key), $value);
}                   
$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

#Sending form data to sql db.
mysqli_query($connection,
"INSERT INTO linkArchive (URL, Campaign, Source, Medium, Content, Keyword, Description, theDate)
VALUES ('$_POST[utm_url]', '$_POST[utm_campaign]', '$_POST[utm_source]', '$_POST[utm_medium]', '$_POST[utm_content]', '$_POST[utm_keyword]', '$_POST[utm_description]', '$_POST[utm_date]')");

// echo('IT RAN');
// header("Location: http://website.com");
// exit();
}   

?>

0 个答案:

没有答案