将变量添加到数据库

时间:2016-02-05 12:23:26

标签: php

我正在尝试使用引导程序编辑器将数据添加到数据库中。我有这样的事情:

$templateName = $_POST["templateName"]; 
$emailFrom = $_POST["emailFrom"];   
$emailSubject = $_POST["emailSubject"]; 
$emailContent = $_POST["emailContent"]; 

if(mysqli_query($kon, "INSERT INTO emailtexts VALUES (NULL, '". $templateName ."', '". $emailFrom ."', '". $emailSubject ."', '". $emailContent ."')")){
    echo "Added";   
}

其中$ emailContent是这样的:这是我的变量 $ var 的内容,然后在我要显示结果的文件中我有类似的东西这个:

$var= "This is value of my variable";

$rez = mysqli_query($kon, "SELECT * FROM emailTexts WHERE id = 2");
$red = mysqli_fetch_assoc($rez);

echo "Test result : <br /><br />" . $red["content"];

我希望得到类似的东西:

  

测试结果:

     

这是我的内容,变量这是我变量的值

但我得到了

  

测试结果:

     

这是我的变量 $ var

的内容

有什么建议吗?感谢。

更新

我的php表单是:

<div class="box-body">
<p class="help-block" style="text-align:center;color:#f00;">Alle velden moeten ingevuld worden!.</p>
<div id="addedSuccess" class="alert alert-success col-lg-12 col-md-12 col-sm-12 col-xs-12" style="text-align:center;display:none;"></div>
<div id="addedError" class="alert alert-danger col-lg-12 col-md-12 col-sm-12 col-xs-12" style="text-align:center;display:none;"></div>
<form id="frmAddTemplate" role="form" action="" method="post">
    <div class="form-group col-lg-6" style="float:none;margin:10px auto;">
        <input type="text" class="form-control" id="txtTemplateName" name="txtTemplateName" placeholder="De naam van de email template">
    </div>
    <div style="clear:both;"></div>
    <div class="form-group col-lg-6" style="float:none;margin:10px auto;">
        <input type="text" class="form-control" id="txtEmailFrom" name="txtFromEmail" placeholder="Sender (from email)">
    </div>

    <div class="form-group col-lg-6" style="float:none;margin:10px auto;">
        <input type="text" class="form-control" id="txtEmailSubject" name="txtEmailSubject" placeholder="Subject">
    </div>

    <div class="form-group">
        <textarea class="bs-texteditor form-control" rows="12" placeholder="Email content" name="txtEmailContent" id="txtEmailContent"></textarea><br />
    </div>
    <div id="pleaseWait" style="float:right;margin:4px 0 0 3px;display:none;"><img src="../img/ajax-loader-small.gif" title="Please wait"/></div>
    <button type="submit" class="pull-right btn btn-success" name="btnAddTemplate" id="btnAddTemplate"><i class="fa fa-plus-circle"></i>  Save template</button>
</form>
<div style="clear:both;"></div>

我的jquery和ajax调用是:

<script type="text/javascript">
        $(document).ready(function(e) {
            $(document).on("click", "#btnAddTemplate", function(e){
                var templateName = $("#txtTemplateName").val();
                var emailFrom = $("#txtEmailFrom").val();
                var emailSubject = $("#txtEmailSubject").val();
                var emailContent = $("#txtEmailContent").val();
                $.ajax({
                    url : "adding-new-template.php",
                    type: "POST",
                    data: {templateName : templateName, emailFrom : emailFrom, emailSubject : emailSubject, emailContent : emailContent, status : "insert"},
                    beforeSend: function(){
                        $("#pleaseWait").show();
                    },
                    success: function(msg){
                        $("#pleaseWait").hide();
                        if(msg == "Added"){
                            $("#addedSuccess").html("De tamplate is succesvol toegevoegd.").fadeIn(300).fadeOut(10000);
                        }else{
                            $("#addedError").html("Er is iets misgelopen. De template is niet succesvol toegevoegd.").fadeIn(300).fadeOut(8000);
                        }
                    },

                    error: function(){
                        $("#pleaseWait").hide();
                        $("#addedError").html("Er is iets misgelopen. De template is niet succesvol toegevoegd.").fadeIn(300).fadeOut(8000);
                    }
                });
                e.preventDefault();
            });
        });

    </script>

最后,我的add-new-template.php代码是:

<?php
        session_start();
        include("config.php");
        global $kon;

        $templateName = $_POST["templateName"]; 
        $emailFrom = $_POST["emailFrom"];   
        $emailSubject = $_POST["emailSubject"]; 
        $emailContent = $_POST["emailContent"]; 
        $status = $_POST["status"];

        if($status == "insert"){
            if(mysqli_query($kon, "INSERT INTO emailtexts VALUES (NULL, '". $templateName ."', '". $emailFrom ."', '". $emailSubject ."', '". $emailContent ."')")){
                echo "Added";   
            }
        }else{
            if($status == "update"){

            }
        }
    ?>

我想添加的电子邮件内容是:

  

尊敬的,

     

您的密码已更改。您的凭据是:

     

用户名:$ username(值)新密码= $密码(值)

     

祝你好运

2 个答案:

答案 0 :(得分:0)

我相信你在$ var变量前面有额外的$符号,你最终添加了variable variable

  

变量变量采用变量的值并将其视为变量的名称。在上面的示例中,hello可以通过使用两个美元符号用作变量的名称。即。

答案 1 :(得分:0)

你可以尝试

$var= "This is value of my variable";

$rez = mysqli_query($kon, "SELECT * FROM emailTexts WHERE id = 2");
$red = mysqli_fetch_assoc($rez);

echo "Test result : <br /><br />" . str_replace("$var", $var, $red["content"]);

但它不是很灵活,你应该解释为什么你需要把var放在数据库中。

如果您正在寻找模板,可以举例说明:

<?php

function parse($text, $data)
{
    // Loop to replace all the variables known as : {{[a-z_]+}}
    while (preg_match("/{{(\w+)}}/", $text, $matches) == 1)
    {
        // Keeping trace of the text you're going to replace
        $str_to_replace = $matches[0];
        // Retrieving only the name of the variable
        $var_to_seek = $matches[1];
        // Assigning the variable found
        $new_var = "";
        if (isset($data[$var_to_seek]))
            $new_var = $data[$var_to_seek];
        // Replacing the variable in the original string.
        $text = str_replace($str_to_replace, $new_var, $text);
    }
    return $text;
}

$str = "Hello {{firstname}} {{name}}.{{unsetvar}} {{sentence}}";

echo parse($str, array("firstname" => "John", "name" => "Doe", "sentence" => "Templates are cool."));
//Output is "Hello John Doe. Templates are cool."

另外,在插入数据库之前,不要忘记转义值。