使用php自定义函数将值插入mysql

时间:2012-09-05 12:32:58

标签: php

我想将值插入MySQL数据库,但值不插入,我不知道什么是错的。

这是我的代码:

<?php


include("inc/connection.php");

function RegisterStep1() {

        $txtuser = $_POST['txtName'];
        $txtsurname = $_POST['txtSurname'];
        $txtemail = $_POST['txtEmail'];
        $txtpass = $_POST['txtPassword'];
        $txtconfirmpass = $_POST['txtConfirmPass'];
        $txtcontactperson = $_POST['txtcompanycontactname'];
        $txtCompAddress = $_POST['txtCompAddress'];
        $txtRegNo = $_POST['txtRegNo'];
        $txtuserpos = $_POST['txtuserpos'];
        $txtdepartment = $_POST['txtdepartment'];
        $txtcontacts = $_POST['txtcontacts'];
        /* check if values are posted */



        $txtuser = mysql_real_escape_string($_POST['txtName']);
        /* combo company type */
        $txtCompAddress = mysql_real_escape_string($_POST['txtCompAddress']);
        /* combo locations
         */
        $txtRegNo = mysql_real_escape_string($_POST['txtRegNo']);
        /* combo companysize */
        $txtcontactperson = mysql_real_escape_string($_POST['txtcompanycontactname']);
        $txtsurname = mysql_real_escape_string($_POST['txtSurname']);
        $txtuserpos = mysql_real_escape_string($_POST['txtuserpos']);

        $txtdepartment = mysql_real_escape_string($_POST['txtdepartment']);
        $txtcontacts = mysql_real_escape_string($_POST['txtcontacts']);
        $txtemail = mysql_real_escape_string($_POST['txtEmail']);
        $txtpass = mysql_real_escape_string($_POST['txtPassword']);
        $txtconfirmpass = mysql_real_escape_string($_POST['txtConfirmPass']);



        $q = "INSERT INTO company(Name,Type,Address,Location,RegisteredNumber,CompanySize,ContactPerson,Surname,Position,Department,Contacts,DateResgistered,AccountStatus,Email,Password)
      Values('" . $txtuser . "','" . $txtCompAddress . "','" . $txtRegNo . "','" . $txtcontactperson . "','" . $txtsurname . "','" . $txtuserpos . "','" . $txtRegNo . "','" . $txtdepartment . "','" . $txtcontacts . "','" . $txtemail . "','" . $txtpass . "')";

        $submitquery = mysql_query($q);
        if ($submitquery) {
            echo"<div id='results'>Error occured while creating account,please try again in few minutes</div>";
        } else {
            echo "<div id='results'>Thanks for signing up</div>";
        }

}

?>

1 个答案:

答案 0 :(得分:1)

有趣的是,请检查一下你的情况

$submitquery = mysql_query($q);
        if ($submitquery) {
            echo"<div id='results'>Error occured while creating account,please try again in few minutes</div>";
        } else {
            echo "<div id='results'>Thanks for signing up</div>";
        }

应该是

$submitquery = mysql_query($q);
            if (!$submitquery) {
                echo"<div id='results'>Error occured while creating account,please try again in few minutes</div>";
            } else {
                echo "<div id='results'>Thanks for signing up</div>";
            }

我认为您的代码运行正常。 :)