插入查询以插入MySQL数据库不起作用。 (PHP)

时间:2011-01-17 09:35:44

标签: php mysql sql mysqli

我正在尝试从表单插入值并将其插入到MySQL数据库中。 但它不起作用。 我是初学者,请帮忙。

这是我的代码:

<?php

function register(){

    $name = $_POST['name'];
    $address = $_POST['address'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
//echo $name;

$con = mysqli_connect("localhost", "root", "", "LinuxCreditSociety");
$rs = $con->query("insert into cust_mst (customer_id,customer_name,customer_address,customer_mobile,email_id)values(1003,'$name','$address','$phone','$email')");
$rs->free();
$con->close();
}

?>


<html>
    <head>
        <title>Registration</title>
    <link rel="stylesheet" type="text/css" href="phpcss.css"></link>
    </head>
    <body>
        <div style="position:absolute;left:300px;top:5px">
            <h1 align="center"><font face="Purisa" size="20" color="purple">Linux Credit Society</font></h1>
        </div>
        <div style="position:absolute;right:160px;top:5px"><img src="linux.jpg" height="150" /></div>
        <div style="position:absolute;left:140px;top:5px"><img src="linux.jpg" height="150" /></div>


    <form method="post">
        <div class="st1">
         Name:</br></br>
         Address:</br></br>
         Email-id:</br></br>
         Phone#:</br></br>
        </div>
        <div class="st2">
            <div style="position:absolute;top:5px">
                <input type="text" name="name">
            </div>
            <div style="position:absolute;top:78px">
                <input type="text" name="address"> 
            </div>
            <div style="position:absolute;top:148px">
              <input type="text" name="email">
            </div>
            <div style="position:absolute;top:218px">
                <input type="text" name="phone">
            </div>
            <div style="position:absolute;top:290px;">
            <input type="submit" value="Register">  
            </div>
        </div>
    </form>
            <div style="position:absolute;top:320px;">
                    <?php
                            if(isset($_POST['submit']) && $_POST['submit'] == "Register")
                                register();
                  ?>
            </div>
    </body>
</html>

这就是我创建数据库的方式:

create database LinuxCreditSociety;

use LinuxCreditSociety;

create table cust_mst(
customer_id int,
customer_name varchar(50),
customer_address varchar(70),
customer_mobile double,
email_id varchar(50));

insert into cust_mst values(1001, 'Jack Mathew', 'Bandra', '9998887770', 'jackm@yahoo.com');

insert into cust_mst values(1002, 'Jill Roberts', 'Dadar', '999665550', 'jillr@rediff.com');

编辑:

伙计我刚刚做了一个改变而且它起作用了。现在我也会在注射工作!!

以下是我的所作所为:

我刚刚从 - &gt;

更改了来电
    <?php
                        if(isset($_POST['submit']) && $_POST['submit'] == "Register")
                            register();
              ?>

到此 - &gt;

<?php
                                if(isset($_POST['name']) && $_POST['name'] != "")
                                    register();
                      ?>

4 个答案:

答案 0 :(得分:5)

当然你反过来想要这个:

$_POST[name] = $name;
$_POST[address] = $address;
$_POST[email] = $email;
$_POST[phone] = $phone;

例如:

$name = $_POST['name'];
$address = $_POST['address'];
$email = $_POST['email'];
$phone = $_POST['phone'];

答案 1 :(得分:5)

我假设您的数据来自$_POST。变化

$_POST[name] = $name;
$_POST[address] = $address;
$_POST[email] = $email;
$_POST[phone] = $phone;

$name = $con->real_escape_string($_POST['name']);
$address = $con->real_escape_string($_POST['address']);
$email = $con->real_escape_string($_POST['email']);
$phone = $con->real_escape_string($_POST['phone']);

请注意使用mysqli_real_escape_string()来阻止SQL injections。此外,由于您已使用mysqli扩展程序,请考虑构建prepared statements,而不是将变量插入查询字符串。

最后一点注意:为了防止错误通知,例如“在/ your / script中使用未定义的常量名称 - 假定'名称',请使用字符串访问数组键(即$_POST['name']而不是$_POST[name]

答案 2 :(得分:0)

试试这个,但不要忘记清理你的变量!!!

function register(){

    $name       = $_POST['name'];
    $address    = $_POST['address'];
    $phone      = $_POST['email'];
    $email      = $_POST['phone'];

    $con = new mysqli("localhost", "root", "", "LinuxCreditSociety");

    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

    if (!$con->query("insert into cust_mst (customer_id,customer_name,customer_address,customer_mobile,email_id)values(NULL,'$name','$address','$phone','$email')")) {
        printf("Errormessage: %s\n", $con->error);
    }

    $con->close();
}

答案 3 :(得分:-1)

function register(){

    $name       = $_POST['name'];
    $address    = $_POST['address'];
    $phone      = $_POST['email'];
    $email      = $_POST['phone'];

    $con = mysqli_connect("localhost", "root", "", "LinuxCreditSociety");
    $rs = $con->query("insert into cust_mst (customer_id,customer_name,customer_address,customer_mobile,email_id)values(NULL,'{$name}','{$address}','{$phone}','{$email}')");
    $rs->free();
    $con->close();
}