代码仅向DB发送名称并将电话号码发送到电子邮件字段

时间:2014-08-09 13:19:37

标签: php

我根据建议更新了代码,但现在它只向DB发送名称并将电话号码发送到电子邮件字段。我在下面复制了新代码。

形式:

<form action="processor.php" method="post">
    <div class="field-box">
        <label>Name:</label>
        <input  type="text" name="name" />
    </div>
    <div class="field-box">
        <label>Age:</label>
        <input  type="text" name="age" />
    </div>
    <div class="field-box">
        <label>Phone Number:</label>
        <input type="text" name="phone" />
    </div>
    <div class="field-box">
        <label>Email:</label>
        <input type="text" name="email"/>
        <input type="submit">
</form>

用于在processor.php上发送数据的SQL:

function sendData() {  
    //Connecting to sql db.
    $connect = mysqli_connect("XXXXXX","XXXXXXXX","XXXXXXX","XXXXXXX");

    //setting values from HTML form
    $name  = mysqli_real_escape_string($connect, $_POST['name']);
    $age   = mysqli_real_escape_string($connect, $_POST['age']);
    $phone = mysqli_real_escape_string($connect, $_POST['phone']);
    $email = mysqli_real_escape_string($connect, $_POST['email']);

    //Sending form data to sql db
    mysqli_query($connect,"INSERT INTO users (name, age, phone, email) VALUES ('$name', '$age', '$phone', '$email')");
}

sendData();

3 个答案:

答案 0 :(得分:0)

尝试其他插入类型

mysqli_query($connect,"INSERT INTO users SET name='$name', age='$age',phone='$phone', email='$email')");

答案 1 :(得分:0)

首先尝试使用静态参数插入数据库。我的意思是这样的

mysqli_query($connect,"INSERT INTO users (name, age, phone, email) VALUES ('name', 'age', 'phone', 'email')");

并检查您的查询是否有效!但你可以试试这个

mysqli_query($connect,"INSERT INTO users (name, age, phone, email) VALUES ('" . $name . "', '" . $age . "', '" . $phone . "', '" . $email . "')");

我建议您使用预备语句,如果可能的话,请使用PDO

祝你好运

答案 2 :(得分:0)

只需用这个查询切换现有查询,它就可以正常工作。欢呼声。

mysqli_query($connect,"INSERT INTO users (name, age, phone, email) VALUES ('" . $name . "', '" . $age . "', '" . $phone . "', '" . $email . "')");