doutandusegang2961
doutandusegang2961
采纳率0%
2015-08-30 18:51

信息未插入表格

My problem is that it does not insert info to the table. Please read the code and tell me what is the problem. I also don't get any errors from the database because this thing won't stop bugging me about telling more details.

This is the HTML:

<div id="middle" class="col">
    <br><br>
    <p class="text-center"><strong> فرم ثبت نام</strong></p>
    <br><p class="text-center"> اجباری<span><strong style="color:red">&nbsp *</strong></p><br>

  <form method="post" style="height:70vh; width:47vw; text-align:center;" action="">
    <table width="75%">

        <tr>
            <td align="right"><span class="error"><?php echo $usernameErr?></span></td>
            <td align="right"><input type="text" name="username" value="<?php echo $username?>"></td>
            <td style="font-size:medium" align="right" height="1.5em">:شناسه<font color="red">&nbsp;*</font>&emsp;</td>
        </tr>

        <tr>
            <td align="right"><span class="error"><?php echo $passwordErr?></span></td>
            <td align="right"><input type="password" name="password" value="<?php echo $password?>">
            <td style="font-size:medium" align="right" height="1.5em">:رمزعبور<font color="red">&nbsp*</font>&emsp;</td>
        </tr>

        <tr>
            <td align="right"><span class="error"><?php echo $password_checkErr?></span></td>
            <td align="right"><input type="password" name="password_check" value="<?php echo $password_check ?>">
            <td style="font-size:medium" align="right" height="1.5em">:تکرار رمز عبور<font color="red">&nbsp*</font>&emsp;</td>
        </tr>

        <tr>
            <td align="right"><span class="error"><?php echo $fnameErr?></span></td>
            <td align="right"><input type="text" name="fname" value="<?php echo $fname?>"></td>
            <td style="font-size:medium" align="right">: نام<font color="red">&nbsp*</font>&emsp;</td>
        </tr>

        <tr>
            <td align="right"><span class="error"><?php echo $lastnameErr?></span></td>
            <td align="right"><input type="text" name="lname" value="<?php echo $lname?>"></td>
            <td style="font-size:medium" align="right">: نام خانوادگی<font color="red">&nbsp*</font>&emsp;</td>
        </tr>

        <tr>
            <td align="right"><span class="error"><?php echo $emailErr?></span></td>
            <td align="right"><input type="email" name="email" value="<?php echo $email?>"></td>
            <td style="font-size:medium" align="right">:پست الکترونیک<font color="red">&nbsp*</font>&emsp;</td>
        </tr>

        <tr>
            <td align="right"><span class="error"><?php echo $phone_numberErr?></span></td>
            <td align="right"><input type="text" name="phonenumber" value="<?php echo $phonenumber?>" ></td>
            <td style="font-size:medium" align="right">:تلفن ثابت<font color="red">&nbsp*</font>&emsp;</td>
        </tr>

        <tr>
            <td align="right"><span class="error"><?php echo $cellphone_numberErr?></span></td>
            <td align="right"><input type="text" name="cellphonenumber" value="<?php echo $cellphonenumber?>" ></td>
            <td style="font-size:medium" align="right">:موبایل<font color="red">&nbsp*</font>&emsp;</td>
        </tr>

        <tr>
            <td colspan=3 align=right>
            <table border=0>
                <tr><br>
                <td colspan="2">
                    <input type="text" name="state" value="<?php echo $state?>" >
                </td>
                <td colspan=2 align=right>
                    &emsp;&emsp;:استان<font style="font-size:medium" color="red">&nbsp*&emsp;&emsp;&emsp;&ensp;</font>
                </td>
            </tr>

            </table>
            <table>
            <tr>
                <td><span class="error"><?php echo $addressErr?></span></td>
                <td> <textarea name="address" row+4><?php echo $address?></textarea></td>
                <td style="font-size:medium" colspan=2 align="right">&ensp;:نشانی<font color="red">&nbsp*&emsp;&emsp;&emsp;&ensp;</font></td>
            </tr>

            <tr>
                <td align="right"><span class="error"><?php echo $zipcodeErr?></span></td>
                <td align="right"><input type="text" name="zipcode" value="<?php echo $zipcode?>"></td>
                <td id="address" style="font-size:medium" colspan=2 align="right">&ensp;:کد پستی<font color="red">&nbsp*&emsp;&emsp;&emsp;&ensp;</font></td>
            </tr>

        </table>
        <br><span class="text-center"><input type="submit" name="submit" value="ثبت" style="height:3vh"></span>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
  </form>  

this is the php,mysql:

    <?php
    // define variables and set to empty values
    $zipcode = $username = $password = $password_check = $lname = $state
     = $fname = $email = $cellphonenumber = $address = $phonenumber ="";


    $zipcodeErr = $usernameErr = $passwordErr = $password_checkErr = $lastnameErr = $stateErr
     = $fnameErr = $emailErr = $cellphone_numberErr = $addressErr = $phone_numberErr = "";


    $anyerror=0;
    // Create connection
    $conn = new mysqli('localhost', 'root', '', 'mysql');

// Check connection
    if ($conn->connect_error)
        die("Connection failed: " . $conn->connect_error);

    $sql = "CREATE TABLE IF NOT EXISTS MyUsers6 (
            id INT(6) UNSIGNED AUTO_INCREMENT,
            username VARCHAR(30) NOT NULL,
            password VARCHAR(30) NOT NULL,
            fname VARCHAR(30) NOT NULL,
            lname VARCHAR(30) NOT NULL,
            phonenumber VARCHAR(30) NOT NULL,
            cellphonenumber VARCHAR(30) NOT NULL,
            email VARCHAR(50) NOT NULL,
            state VARCHAR(50) NOT NULL,
            address VARCHAR(50) NOT NULL,
            zipcode VARCHAR(50) NOT NULL,
            reg_date TIMESTAMP,
            primary key (id)
    )";

    if ($conn->query($sql) === FALSE)
        echo "Error creating table: " . $conn->error;



    if ($_SERVER["REQUEST_METHOD"] == "POST") {


    if (empty($_POST["username"])){
        $usernameErr ="لطفا شناسه خود را وارد نمایید";
        $anyerror=1;}
    else
        $username = test_input($_POST["username"]);

    if (empty($_POST["state"])){
        $stateErr ="لطفا شناسه خود را وارد نمایید";
        $anyerror=1;}
    else
        $state = test_input($_POST["state"]);


    if (empty($_POST["password"])){
        $passwordErr = "لطفا رمزعبور خود را وارد نمایید";
        $anyerror=1;}
    else
        $password = test_input($_POST["password"]);


    if (empty($_POST["password_check"])){
        $password_checkErr = "لطفا رمزعبور را دوباره وارد کنید";
        $anyerror=1;}
    if(strcmp($password,$password_check) ==0 && isset($_POST["password_check"])){
        $password_checkErr = " رمز عبور ها یکسان نیستند";
        $anyerror=1;}
    else
        $password_check = test_input($_POST["password_check"]);


    if (empty($_POST["fname"])) {
        $fnameErr = "لطفا نام خود را وارد نمایید";
        $anyerror=1;}
    else 
        $fname = test_input($_POST["fname"]);


    if (empty($_POST["lname"])) {
        $lastnameErr = "لطفا نام خانوادگی خود را وارد نمایید";
        $anyerror=1;}
        else 
        $lname = test_input($_POST["lname"]);


    if (empty($_POST["email"])) {
        $emailErr = "لطفا ایمیل خود را وارد نمایید";
        $anyerror=1;}
    elseif (filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $emailErr = "فرمت ایمیل غیرمجاز است";
        $anyerror=1;
    }
    else 
        $email = test_input($_POST["email"]);


    if (empty($_POST["phonenumber"])) {
        $phone_numberErr = "لطفا شماره تلفن خود را وارد نمایید";
        $anyerror=1;}
    else 
        $phonenumber = test_input($_POST["phonenumber"]);


    if (empty($_POST["address"])) {
        $addressErr = "لطفا نشانی خود را وارد نمایید";
        $anyerror=1;}
    else 
        $address = test_input($_POST["address"]);


    if (empty($_POST["cellphonenumber"])) {
        $cellphone_numberErr = "لطفا شماره موبایل خود را وارد نمایید";
        $anyerror=1;}
    else 
        $cellphonenumber = test_input($_POST["cellphonenumber"]);


    if (empty($_POST["zipcode"])) {
        $zipcodeErr = "لطفا شماره موبایل خود را وارد نمایید";
        $anyerror=1;}
    else 
        $zipcode = test_input($_POST["zipcode"]);


    if ( $anyerror = 0 )
            $sql = "INSERT INTO MyUsers6 VALUES ('$username' ,'$password', '$fname', '$lname', '$phonenumber', '$cellphonenumber', '$email', '$state', '$address', '$zipcode')";


        if ($conn->query($sql) === FALSE) 
            echo "Error: " . $sql . "<br>" . $conn->error;
        else
            echo "ssssssss";

    }

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }


    ?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dousha7645 dousha7645 6年前
    //change your database name put this code and check it
    <?php
        // define variables and set to empty values
        $zipcode = $username = $password = $password_check = $lname = $state
         = $fname = $email = $cellphonenumber = $address = $phonenumber ="";
    
    
        $zipcodeErr = $usernameErr = $passwordErr = $password_checkErr = $lastnameErr = $stateErr
         = $fnameErr = $emailErr = $cellphone_numberErr = $addressErr = $phone_numberErr = "";
    
    
        $anyerror=0;
        // Create connection
        $conn = new mysqli('localhost', 'root', '', 'test');
    
    // Check connection
        if ($conn->connect_error)
            die("Connection failed: " . $conn->connect_error);
    
        $sql = "CREATE TABLE IF NOT EXISTS MyUsers6 (
                id INT(6) UNSIGNED AUTO_INCREMENT,
                username VARCHAR(30) NOT NULL,
                password VARCHAR(30) NOT NULL,
                fname VARCHAR(30) NOT NULL,
                lname VARCHAR(30) NOT NULL,
                phonenumber VARCHAR(30) NOT NULL,
                cellphonenumber VARCHAR(30) NOT NULL,
                email VARCHAR(50) NOT NULL,
                state VARCHAR(50) NOT NULL,
                address VARCHAR(50) NOT NULL,
                zipcode VARCHAR(50) NOT NULL,
                reg_date TIMESTAMP,
                primary key (id)
        )";
    
        if ($conn->query($sql) === FALSE)
            echo "Error creating table: " . $conn->error;
    
    
    
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
    
        if (empty($_POST["username"])){
            $usernameErr ="لطفا شناسه خود را وارد نمایید";
            $anyerror=1;}
        else
            $username = test_input($_POST["username"]);
    
        if (empty($_POST["state"])){
            $stateErr ="لطفا شناسه خود را وارد نمایید";
            $anyerror=1;}
        else
            $state = test_input($_POST["state"]);
    
    
        if (empty($_POST["password"])){
            $passwordErr = "لطفا رمزعبور خود را وارد نمایید";
            $anyerror=1;}
        else
            $password = test_input($_POST["password"]);
    
    
        if (empty($_POST["password_check"])){
            $password_checkErr = "لطفا رمزعبور را دوباره وارد کنید";
            $anyerror=1;}
        if(strcmp($password,$password_check) ==0 && isset($_POST["password_check"])){
            $password_checkErr = " رمز عبور ها یکسان نیستند";
            $anyerror=1;}
        else
            $password_check = test_input($_POST["password_check"]);
    
    
        if (empty($_POST["fname"])) {
            $fnameErr = "لطفا نام خود را وارد نمایید";
            $anyerror=1;}
        else 
            $fname = test_input($_POST["fname"]);
    
    
        if (empty($_POST["lname"])) {
            $lastnameErr = "لطفا نام خانوادگی خود را وارد نمایید";
            $anyerror=1;}
            else 
            $lname = test_input($_POST["lname"]);
    
    
        if (empty($_POST["email"])) {
            $emailErr = "لطفا ایمیل خود را وارد نمایید";
            $anyerror=1;}
        elseif (filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "فرمت ایمیل غیرمجاز است";
            $anyerror=1;
        }
        else 
            $email = test_input($_POST["email"]);
    
    
        if (empty($_POST["phonenumber"])) {
            $phone_numberErr = "لطفا شماره تلفن خود را وارد نمایید";
            $anyerror=1;}
        else 
            $phonenumber = test_input($_POST["phonenumber"]);
    
    
        if (empty($_POST["address"])) {
            $addressErr = "لطفا نشانی خود را وارد نمایید";
            $anyerror=1;}
        else 
            $address = test_input($_POST["address"]);
    
    
        if (empty($_POST["cellphonenumber"])) {
            $cellphone_numberErr = "لطفا شماره موبایل خود را وارد نمایید";
            $anyerror=1;}
        else 
            $cellphonenumber = test_input($_POST["cellphonenumber"]);
    
    
        if (empty($_POST["zipcode"])) {
            $zipcodeErr = "لطفا شماره موبایل خود را وارد نمایید";
            $anyerror=1;}
        else 
            $zipcode = test_input($_POST["zipcode"]);
    
        if ( $anyerror == 0 ) {
                 $sql = "INSERT INTO MyUsers6 (username,password,fname,lname,phonenumber,cellphonenumber,email,state,address,zipcode)
    VALUES ('$username' ,'$password', '$fname', '$lname', '$phonenumber', '$cellphonenumber', '$email', '$state', '$address', '$zipcode')";
               }
    
            if ($conn->query($sql) === FALSE) 
                echo "Error: " . $sql . "<br>" . $conn->error;
            else
                echo "ssssssss";
    
        }
    
        function test_input($data) {
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
        }
    
    
        ?>
    
    点赞 评论 复制链接分享
  • dswwuo1223 dswwuo1223 6年前

    You have a syntax problem in your SQL query. you have to specify the column you want to insert data to,but you haven't done it.

    INSERT INTO(column1,column2) VALUES('value1','value2')
    

    So you have to update you code:

            $sql = "INSERT INTO MyUsers6**(your columns)** VALUES ('', '$username' ,'$password', '$fname', '$lname', '$phonenumber', '$cellphonenumber', '$email', '$state', '$address', '$address', '')";
    
    点赞 评论 复制链接分享

为你推荐