将插入到 Mysql 的 Php 联系表单

标签: MySQL PHP HTML
发布时间: 2017/4/9 22:01:20
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

最后编辑︰ 一切作品现在将发布工作代码,下面后清理像 idealcastle 说,修正了一些语法错误一切正常,因为它应该与 javascript 验证谢谢大家

这里的 HTML 代码︰

<form name = "contact " id="contact_form" action="postcontact.php" method="post" onsubmit="return validateForm();">
                <div id ="boxc">
                    <h3>Porosia juaj ?</h3>
                    <input name="orders" type="checkbox" value="veshje">Veshje
                    <input name="orders" type="checkbox" value="mbathje">Mbathje
                    <input name="orders" type="checkbox" value="stoli">Stoli
                </div>
                <div class="row">
                    <label class="required" for="name" >Emri:</label><br />
                    <input id="name" name="name" type="text" value="" size="30" placeholder = "Emri"/><br />
                    <span id="name_validation" class="error"></span>
                </div>
                <label class="required"  >Country/State:</label><br />
                <div class = "row"id="statecmb"><select name = "state">
                     <option value="chose" selected>[choose yours]</option>
                    <option value="albania">Albania</option>
                    <option value="kosovo">Kosovo</option>
                    <option value="germany">Germany</option>
                    <option value="bangladesh">Bangladesh</option>

                </select>
                <span id="state_validation" class="error"></span></div>
                <div class="row">
                    <label class="required" for="email" >Email:</label><br />
                    <input id="email" name="email" type="text" value="" size="30"placeholder = "Email" /><br />
                    <span id="email_validation" class="error"></span>
                </div>
                <div class="row">
                    <label class="required" for="message" >Mesazhi:</label><br />
                    <textarea id="message" name="message" rows="7" cols="30" placeholder = "Mesazhi"></textarea><br />
                    <span id="message_validation" class="error"></span>
                </div>

                <input name="submit" id = "sub"type="submit" value="Submit" />
                <div class="rating">
                    <h3>Vlerso Sherbimin :</h3>
                    <input type="radio" name="rate" value="1">1
                    <input type="radio" name="rate"value="2">2
                    <input type="radio" name="rate" value="3">3
                    <input type="radio"name="rate" value="4">4
                    <input type="radio" name="rate" value="5">5
                </div>
            </form>

Javascript 文件︰

function validateForm() {
    var valid = 1;
    var email = document.getElementById('email');
    var email_validation = document.getElementById("email_validation");
    var name = document.getElementById('name');
    var name_validation = document.getElementById("name_validation");
    var message_validation = document.getElementById("message_validation");
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if (name.value === "") {
        valid = 0;
        name_validation.innerHTML = "Ju lutem shenoni emrin tuaj";
        name_validation.style.display = "block";
        name_validation.parentNode.style.backgroundColor = "#FFDFDF";
    } else {
        name_validation.style.display = "none";
        name_validation.parentNode.style.backgroundColor = "transparent";
    }

    if (message.value === "") {
        valid = 0;
        message_validation.innerHTML = "Ju lutem plotesoni fushen e mesazhit";
        message_validation.style.display = "block";
        message_validation.parentNode.style.backgroundColor = "#FFDFDF";
    } else {
        message_validation.style.display = "none";
        message_validation.parentNode.style.backgroundColor = "transparent";
    }

    if (email.value === "") {
        valid = 0;
        email_validation.innerHTML = "Ju lutem shenoni email tuaj";
        email_validation.style.display = "block";
        email_validation.parentNode.style.backgroundColor = "#FFDFDF";
    } else {
        email_validation.style.display = "none";
        email_validation.parentNode.style.backgroundColor = "transparent";
    }

    if (!filter.test(email.value)) {
        valid = 0;
        email_validation.innerHTML = "Email juaj nuk eshte valid";
        email_validation.style.display = "block";
        email_validation.parentNode.style.backgroundColor = "#FFDFDF";
    } else {
        email_validation.style.display = "none";
        email_validation.parentNode.style.backgroundColor = "transparent";
    }
    if (!valid)
        alert("KENI ERROR : Fushat duhen te plotesohen ");

}

PHP 文件︰

<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'herdesigns';
$con = mysqli_connect($host, $user, $pass,$db) or die(mysqli_error());
/* mysqli_select_db($con , $db); */
?>

<?php

if (isset($_POST['submit']))
{
$name = mysqli_real_escape_string($con, $_POST['name']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$message = mysqli_real_escape_string($con, $_POST['message']);
$rate = mysqli_real_escape_string($con, $_POST['rate']);
$orders = mysqli_real_escape_string($con, $_POST['orders']);
$state = mysqli_real_escape_string($con, $_POST['state']);
/*$con = mysqli_connect($host, $user, $pass,$db) or die(mysqli_error());*/
/*mysqli_select_db($con , $db);*/
$sql = "INSERT INTO contacts (
orders,
name,
state,
email,
message,
rate
)
VALUES (
'$orders',
'$name',
'$state',
'$email',
'$message',
'$rate'
)";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "Mesazhi juaj eshte postuar me sukses";
header('Location:contact.php');

mysqli_query($con, $sql);

mysqli_close($con);
}
?>

解决方法 1:

编辑︰ 哪些领域 now () 也要?

没有实际的字段发送该日期时间会给那若。或为此添加字段。尝试提交 Mysql 没有它会看起来像的 now ()

$sql = "INSERT INTO contacts (
            name,
            email,
            message,
            rate,
            orders,
            state
        )
        VALUES (
            '$name',
            '$email',
            '$message',
            '$rate',
            '$orders', 
            '$state'
        )";

我注意到的第一件事是 PHP 代码被显示在浏览器中。如果您正在发送到 file:// 是不好,你应该使用

http//localhost/ 

(如果您本地测试) 或当然使用的服务器的 url,如果是活。

在这里找到
浏览器显示 PHP 代码而不是处理它

第二,你应该净化输入你 mysql 数据。如果任何这些值提交内容与单/双报价,mysql 查询将失败。

由于您使用的旧的 mysql 函数,下面是应该工作的转义函数

 mysql_real_escape_string()

这样,

 $sql = "INSERT INTO contacts (
            name,
            email,
            message,
            rate,
            orders,
            state
        )
        VALUES (
            '".mysql_real_escape_string($name)."',
            '".mysql_real_escape_string($email)."',
            '".mysql_real_escape_string($message)."',
            '".mysql_real_escape_string($rate)."',
            '".mysql_real_escape_string($orders)."', 
            '".mysql_real_escape_string($state)."',
            NOW()
        )";

我不确定如果其中的任何人都是原因,但他们都是从您已发布的红旗。你应该总是消毒 (escape) 从崩溃 mysql 查询的任何投入。

官方微信
官方QQ群
31647020