电话号码文本框不保存到数据库中

时间:2016-10-05 16:40:13

标签: php html database

这是一个学生数据库系统,我可以在其中添加学生数据,编辑学生数据,删除学生数据,以及从注册号码中搜索学生数据。

这是我遇到问题的地方。一切正常但电话号码不会保存在mysql数据库中。所有其他选项都是编辑并插入数据库中。

编辑任何信息或添加新数据时,我没有收到任何错误。

当我输入注册号码时,直接从phpmyadmin提交电话号码,然后从我的索引页面提交电话号码,然后获取没有电话号码的学生的所有信息。

以下是index.php代码:

<?php

$host = "localhost";
$user = "root";
$password ="root";
$database = "college";

$student_reg = "";
$student_name = "";
$father_name = "";
$phone_number = "";
$student_address = "";
$student_course = "";
$student_certificatenumber = "";
$student_email = "";
$student_city = "";

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// connect to mysql database
try{
    $connect = mysqli_connect($host, $user, $password, $database);
} catch (mysqli_sql_exception $ex) {
    echo 'Error';
}

// get values from the form
function getPosts()
{
    $posts = array();
    $posts[7] = $_POST['student_reg'];
    $posts[1] = $_POST['student_name'];
    $posts[2] = $_POST['father_name'];
    $posts[3] = $_POST['phone_number'];
    $posts[4] = $_POST['student_address'];
    $posts[5] = $_POST['student_course'];
    $posts[6] = $_POST['student_certificatenumber'];
    $posts[8] = $_POST['student_email'];
    $posts[9] = $_POST['student_city'];
    return $posts;
}

// Search

if(isset($_POST['search']))
{
    $data = getPosts();

    $search_Query = "SELECT * FROM students WHERE student_reg = $data[7]";

    $search_Result = mysqli_query($connect, $search_Query);

    if($search_Result)
    {
        if(mysqli_num_rows($search_Result))
        {
            while($row = mysqli_fetch_array($search_Result))
            {
                $student_reg = $row['student_reg'];
                $student_name = $row['student_name'];
                $father_name = $row['father_name'];
                $phone_number = $row['phone_number'];
                $student_address = $row['student_address'];
                $student_course = $row['student_course'];
                $student_certificatenumber = $row['student_certificatenumber'];
                $student_email = $row['student_email'];
                $student_city = $row['student_city'];
            }
        }else{
            echo 'No Data For This Id';
        }
    }else{
        echo 'Result Error';
    }
}


// Insert
if(isset($_POST['insert']))
{
    $data = getPosts();
    $insert_Query = "INSERT INTO `students`(`student_reg`, `student_name`, `father_name`, `phone_number`, 

`student_address`, `student_course`, `student_certificatenumber`, `student_email`, `student_city`) VALUES ('$data

[7]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[8]','$data[9]')";
    try{
        $insert_Result = mysqli_query($connect, $insert_Query);

        if($insert_Result)
        {
            if(mysqli_affected_rows($connect) > 0)
            {
                echo 'Data Inserted';
            }else{
                echo 'Data Not Inserted';
            }
        }
    } catch (Exception $ex) {
        echo 'Error Insert '.$ex->getMessage();
    }
}

// Delete
if(isset($_POST['delete']))
{
    $data = getPosts();
    $delete_Query = "DELETE FROM `students` WHERE `student_reg` = $data[7]";
    try{
        $delete_Result = mysqli_query($connect, $delete_Query);

        if($delete_Result)
        {
            if(mysqli_affected_rows($connect) > 0)
            {
                echo 'Data Deleted';
            }else{
                echo 'Data Not Deleted';
            }
        }
    } catch (Exception $ex) {
        echo 'Error Delete '.$ex->getMessage();
    }
}

// Edit
if(isset($_POST['update']))
{
    $data = getPosts();
    $update_Query = "UPDATE `students` SET `student_reg`='$data[7]',`student_name`='$data[1]',`father_name`='$data

[2]',`phone_number`='$data[3]',`student_address`='$data[4]',`student_course`='$data

[5]',`student_certificatenumber`='$data[6]',`student_email`='$data[8]',`student_city`='$data[9]' WHERE 

`student_reg` = $data[7]";
    try{
        $update_Result = mysqli_query($connect, $update_Query);

        if($update_Result)
        {
            if(mysqli_affected_rows($connect) > 0)
            {
                echo 'Data Updated';
            }else{
                echo 'Data Not Updated';
            }
        }
    } catch (Exception $ex) {
        echo 'Error Update '.$ex->getMessage();
    }
}



?>


<!DOCTYPE Html>
<html>
    <head>
        <title>PHP INSERT UPDATE DELETE SEARCH</title>
    </head>
    <body>
        <form action="index.php" method="post">
            <input type="text" name="student_reg" placeholder="Student Registration Code" value="<?php echo 

$student_reg;?>"><br><br>
            <input type="text" name="student_name" placeholder="Name" value="<?php echo $student_name;?>"><br><br>
            <input type="text" name="father_name" placeholder="Student Father Name" value="<?php echo 

$father_name;?>"><br><br>
            <input type="text" name="Phone_number" placeholder="Phone Mobile Number" value="<?php echo 

$Phone_number;?>"><br><br>
            <input type="text" name="student_address" placeholder="Address" value="<?php echo $student_address;?

>"><br><br>
            <input type="text" name="student_course" placeholder="Course" value="<?php echo $student_course;?

>"><br><br>
            <input type="text" name="student_certificatenumber" placeholder="Certificate Number" value="<?php echo 

$student_certificatenumber;?>"><br><br>
            <input type="text" name="student_email" placeholder="EMail" value="<?php echo $student_email;?

>"><br><br>
            <input type="text" name="student_city" placeholder="City" value="<?php echo $student_city;?>"><br><br>
            <div>
                <!-- Input For Add Values To Database-->
                <input type="submit" name="insert" value="Add">

                <!-- Input For Edit Values -->
                <input type="submit" name="update" value="Update">

                <!-- Input For Clear Values -->
                <input type="submit" name="delete" value="Delete">

                <!-- Input For Find Values With The given ID -->
                <input type="submit" name="search" value="Find">
            </div>
        </form>
    </body>
</html>

2 个答案:

答案 0 :(得分:-1)

检查mysql数据库中你的phonenumber列的数据类型使其成为字符串类型...它可以解决你的问题

答案 1 :(得分:-2)

$posts[3] = $_POST['phone_number']

不同
<input type="text" name="Phone_number" 

Phone_number不是= to phone_number。 PHP区分大小写。