数据未使用php函数进入wampserver sql数据库

时间:2019-01-29 17:34:25

标签: php sql mysqli wampserver

这是php代码。我也是php和查询的新手,我的查询错误是因为或由于数据未反映在名为user的表中而导致的任何逻辑错误。 registration.php也包括在内。 php初学者很难理解,您可以查明错误。

<?php
//connect to the database 
$db=mysqli_connect('localhost','root', '','register') or die("not connected");
if($db){
    echo "connection s";
}
global $errors;
global $sql;
//if next bustton is clicked
    if(isset($_POST['Next'])){
        $first =mysql_real_escape_string($_POST['first name']);
        $last =mysql_real_escape_string($_POST['last name']);
        $regno =mysql_real_escape_string($_POST['reg no']);
        //all fields filled
        if(empty($first)){
            array_push($errors,"First name is required");
        }
        if(empty($last)){
            array_push($errors,"Last name is required");
        }
        if(empty($regno)){
            array_push($errors,"Registration number is required");
        }

        //error free then there are no errors,save it in database
        if(count($errors)==0){

            $sql="insert into users (first name, last name, regno) values ('$first', '$last', '$regno')";
            $query=mysqli_query($db,$sql);
            if($query){
                echo "query inserted";
            }

        }
    }

?>   这里是主要的registration.php开始

<?php include('server.php'); ?>

<!DOCTYPE html>
<html>
<head>
<title>Registration</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
    <div class="wrap">          
        <h2>Book Bank Registration</h2>
            <form action="registration.php" method="POST">
                <?php include('errors.php'); ?>
                <div class="form-group">
                    <label>First Name</label>

                <input type="text" class="form-control" name="firstname">
                </div>
                <div class="form-group">
                    <label>Last Name</label>
                    <input type="text" class="form-control" name="lastname">
                </div>
                <div class="form-group">
                    <label>Registration No</label>
                    <input type="text" class="form-control" name="regno">
                </div>
                <div class="form-group">
                    <!--label for="next">Next</label-->
        <input type="submit" value="Next" class="form-control" name="next">
                </div>
            </form>
    </div>      
  </body>
</html>

这里的方法是POST

4 个答案:

答案 0 :(得分:0)

使用mysql_real_escape_string会在mysqli_connect数据库中导致错误... 而是使用mysqli_real_escape_string($ connection,$ field);

现在尝试 mysqli_query($db,$sql) or die(mysqli_error($db)).

<?php
    //connect to the database 
    $db=mysqli_connect('localhost','root', '','register') or die("not connected");
    if($db){
        echo "connection s";
    }
    global $errors;
    global $sql;
   if ($_SERVER["REQUEST_METHOD"] == "POST") {
    //if next bustton is clicked
        if(isset($_POST['next'])){
            $first =mysqli_real_escape_string($db,$_POST['firstname']);
            $last =mysqli_real_escape_string($db,$_POST['lastname']);
            $regno =mysqli_real_escape_string($db,$_POST['regno']);
            //all fields filled
            if(empty($first)){
                array_push($errors,"First name is required");
            }
            if(empty($last)){
                array_push($errors,"Last name is required");
            }
            if(empty($regno)){
                array_push($errors,"Registration number is required");
            }
            var_dump($errors);
            echo "<br/>Count of errors ".count($errors);
    //error free then there are no errors,save it in database
            if(count($errors)==0){
                $sql="insert into users (firstname, lastname, regno) values ('$first', '$last', '$regno')";
                $query=mysqli_query($db,$sql);
                if($query){
                    echo "query inserted";
                }
                else{
                    echo mysqli_error($db);
                }
            }
            else{
                echo "Errors Variable.";
            }
        }
        else{
            echo "No Post Done";
        }
}

答案 1 :(得分:0)

尝试使用mysqli_error()输出错误消息:

<?php

//...

$query = mysqli_query($db, $sql);
if (!$query) {
  echo mysqli_error($db);
}

//...

答案 2 :(得分:0)

使用

$db->real_escape_string

mysql_real_escape_string($_POST['first name']);


    <?php
//connect to the database 
$db=mysqli_connect('localhost','root', '','register') or die("not connected");
if($db){
    echo "connection s";
}
global $errors;
global $sql;
//if next bustton is clicked
    if(isset($_POST['Next'])){
        $first =$db->real_escape_string($_POST['first name']);
        $last =$db->real_escape_string($_POST['last name']);
        $regno =$db->real_escape_string($_POST['reg no']);
        //all fields filled
        if(empty($first)){
            array_push($errors,"First name is required");
        }
        if(empty($last)){
            array_push($errors,"Last name is required");
        }
        if(empty($regno)){
            array_push($errors,"Registration number is required");
        }

        //error free then there are no errors,save it in database
        if(count($errors)==0){

            $sql="insert into users (first name, last name, regno) values ('$first', '$last', '$regno')";
            $query=mysqli_query($db,$sql);
            if($query){
                echo "query inserted";
            }

        }
    }

答案 3 :(得分:0)

尝试编辑代码:

$_POST['first name'] => $_POST['first_name']
$_POST['last name'] => $_POST['last_name']
$_POST['reg no'] => $_POST['reg_no']

$sql="insert into users (first name, last name, regno) values ('$first', '$last', '$regno')"; 

$sql="insert into users (first_name, last_name, regno) 
        values ('$first', '$last', '$regno')";

first name不是MySQL用户表中的字段,它应该是first_namefirstname,与last namereg no相同