doutandusegang2961 2015-08-30 10:51 采纳率: 0%
浏览 20
已采纳

信息未插入表格

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 2015-08-30 11:24
    关注
    //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;
        }
    
    
        ?>
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部