无法将数据输入sql数据库

时间:2018-06-16 09:03:39

标签: php mysql sql database

我正在使用此代码将一些数据添加到我已经存在的sql数据库中,但似乎无法这样做,它也没有给出任何错误。我已经尝试了所有我能想到的东西。这是一个允许用户输入数据的表单,然后当用户点击提交时,它会在url中给出成功消息,但是我得到了成功消息但数据库中没有数据。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Signup Form</title>
</head>
<body>
    <form action="signup.php" method="POST">
        <input type="text" name="firstname" placeholder="First Name">
        <br>
        <input type="text" name="lastname" placeholder="Last Name">
        <br>
        <input type="text" name="email" placeholder="E-mail">
        <br>
        <input type="text" name="uid" placeholder="User name">
        <br>
        <input type="password" name="pwd" placeholder="Password">
        <br>
        <button type="submit" name="submit">Sign up</button>
        &nbsp;
    </form>

<?php
    $sql = "SELECT * FROM users;" ;
    $result = mysqli_query($conn,$sql);          //connects the database to the query we just generated
    $resultcheck = mysqli_num_rows($result);        // it returns the number of rows in the query

    if($resultcheck > 0){
        //the if condition checks if there is any data inside $resultcheck
        //The mysqli_fetch_assoc() function fetches a result row as an associative array.
        while($row = mysqli_fetch_assoc($result)){
            echo $row['user_uid'].'<br>';
        }
    }
?>

</body>
</html>
<?php
    include_once 'dbh.php';

    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $email = $_POST['email'];
    $uid = $_POST['uid'];
    $pwd = $_POST['pwd'];

    $sql = "INSERT INTO users (`user_firstname`, `user_firstname`, `user_email`, `user_uid`, `user_pwd` ) VALUES (\'$firstname\',\'$lastname\',\'$email\',\'$uid\', \'$pwd\');";

    //require 'dbh.php';

    mysqli_query('$conn','$sql');

    /* if($result=$mysqli->query($sql)){
    echo "<p>User successfully added to database</p>".'<br>';
    }
    else{
        echo "Error enterting user into database!".mysql_error().'<br>';
    } */

    header("Location: index.php?signup=success");
?>
<?php
    $dbServername = "localhost";
    $dbUsername = "root";
    $dbPassword = "";
    $dbName = "login_system";               // selecting the database

    $conn = mysqli_connect($dbServername,$dbUsername,$dbPassword,$dbName );
    //$mysqli = new mysqli('localhost','root',"",$dbName );

    if(mysqli_connect_errno()){
        printf("connection failed %s\n",mysqli_connect_error());
        exit();
    }

    $mysqli->select_db("login_system");
?>

4 个答案:

答案 0 :(得分:1)

请删除$ conn和$ sql中的单引号

mysqli_query($conn, $sql);
插入PHP文件中的

答案 1 :(得分:0)

$sql = "INSERT INTO `users` (`user_firstname`, `user_lastname`, `user_email`, `user_uid`, `user_pwd` ) VALUES ('".$firstname."', '".$lastname ."', '".$email."', '".$uid."', '".$pwd."');";
$result=mysqli_query('$conn','$sql');
if($result)
{
    echo "succsessfuly...";
}
else
{
    echo "Not succsessfuly...";
}

答案 2 :(得分:0)

试试这个:

extensions [gis]
globals
[
  land-use-map
   Senario1N               ;; the count of patches senario 1 describes
   Senario1L
   Senario1A
   Senario1B
   Senario1I
   Senario1R
   Senario1W
  %landusetypeN           ;; the amount patches
  %landusetypeL
  %landusetypeA
  %landusetypeB
  %landusetypeI
  Atractiveneighbor

]

patches-own
  [   Land-use                ;; Wat kind og landusetype a patch has
      Willingstochange        ;; If true a patch would like to change (if true the count of patches has a surplus comparing to the sneario, if false they have a shortage)
      atractiveness           ;; if a patch type is attractive to change in <1 = yess
    ]



to setup
  clear-all
  load-gis                       ;;load the maps
  setup-constants
  update-global-variables
  update-display
  reset-ticks
end



to load-gis  ;;load the maps
  set land-use-map gis:load-dataset "a_LANDUSE_cellsize5.asc"     ;;loads the land use map
  gis:set-world-envelope-ds gis:envelope-of land-use-map          ;;sets the envelope of the world to match that of the GIS dataset
  gis:apply-raster land-use-map Land-use                          ;;patches in the land-use-map have a specific land-use now

  ask patches [
    if Land-use = 1 [ set pcolor Green ] ; Green = Nature         ;; patches have a certain color now
    if Land-use = 2 [ set pcolor red ] ; Dark red = Leisure
    if Land-use = 3 [ set pcolor Yellow ] ; Yellow = Agriculture
    if Land-use = 4 [ set pcolor brown ] ; brouwn = Buildup
    if Land-use = 5 [ set pcolor grey ] ; grey = roads
    if Land-use = 6 [ set pcolor pink ] ; pink = industry
    if Land-use = 7 [ set pcolor blue ] ; Blue = water
  ]
    resize-world 0 1633 0 780
    set-patch-size 1
end

to setup-constants
  set Senario1N 49174        ;; the count of patches senario 1 describes
  set Senario1L 17871
  set Senario1A 569970
  set Senario1B 34202
  set Senario1I 5540
  set Senario1R 34968
  set Senario1W 65594

end


to go ;; this asks the model to caculate certain variables defined below
  askforchange
  caculateWILLingtochange
  caculateAtrac
  tick
end


to update-display
  ask patches
  [ if Land-use = 1 [ set pcolor Green ]   ;; Green = Nature           
    if Land-use = 2 [ set pcolor red ]     ;; Dark red = Leisure
    if Land-use = 3 [ set pcolor yellow ]  ;; Yellow = Agriculture
    if Land-use = 4 [ set pcolor brown ]   ;; brouwn = Buildup
    if Land-use = 5 [ set pcolor grey ]    ;; grey = roads
    if Land-use = 6 [ set pcolor pink ]    ;; pink = industry
    if Land-use = 7 [ set pcolor blue ]    ;; Blue = water
    ] ;; patches have a certain color now
end

to update-global-variables
  if count patches > 0
    [ set %landusetypeN (count patches with [ Land-use = 1 ] / count patches) * 100
      set %landusetypeL (count patches with [ Land-use = 2 ] / count patches) * 100
      set %landusetypeA (count patches with [ Land-use = 3 ] / count patches) * 100
      set %landusetypeB (count patches with [ Land-use = 4 ] / count patches) * 100
      set %landusetypeI (count patches with [ Land-use = 6 ] / count patches) * 100
    ]
end



to caculateWILLingtochange
  Ask  patches [
    if count patches with [Land-use = 1] > Senario1N [ ask patches with [ Land-use = 1 ][
           set  Willingstochange True
         ] ]

    if count patches with [Land-use = 2] > Senario1L [ ask patches with [ Land-use = 2 ][
           set  Willingstochange True
         ] ]
    if count patches with [Land-use = 3] > Senario1A [ ask patches with [ Land-use = 3 ][
           set  Willingstochange True
         ] ]
    if count patches with [Land-use = 4] > Senario1B [ ask patches with [ Land-use = 4 ][
           set  Willingstochange True
         ] ]
    if count patches with [Land-use = 5] > Senario1R [ ask patches with [ Land-use = 5 ][
           set  Willingstochange True
         ]]
    if count patches with [Land-use = 6] > Senario1I [ ask patches with [ Land-use = 6 ][
           set  Willingstochange True
         ]]
    if count patches with [Land-use = 7] > Senario1W [ ask patches with [ Land-use = 7 ][
           set  Willingstochange True
         ] ]  ]
end

to caculateAtrac
  Ask  patches [
    if count patches with [Land-use = 1] > Senario1N [ ask patches with [ Land-use = 1 ][
           set  atractiveness (count patches with [Land-use = 1]/ Senario1N )
         ] ]

    if count patches with [Land-use = 2] > Senario1L [ ask patches with [ Land-use = 2 ][
           set  atractiveness (count patches with [Land-use = 2]/ Senario1L )
         ] ]
    if count patches with [Land-use = 3] > Senario1A [ ask patches with [ Land-use = 3 ][
           set  atractiveness (count patches with [Land-use = 3]/ Senario1A )
         ] ]
    if count patches with [Land-use = 4] > Senario1B [ ask patches with [ Land-use = 4 ][
            set  atractiveness (count patches with [Land-use = 4]/ Senario1B )
         ] ]
    if count patches with [Land-use = 5] > Senario1R [ ask patches with [ Land-use = 5 ][
            set  atractiveness (count patches with [Land-use = 5]/ Senario1R )
         ]]
    if count patches with [Land-use = 6] > Senario1I [ ask patches with [ Land-use = 6 ][
             set  atractiveness (count patches with [Land-use = 6]/ Senario1I )
         ]]
    if count patches with [Land-use = 7] > Senario1W [ ask patches with [ Land-use = 7 ][
             set  atractiveness (count patches with [Land-use = 7]/ Senario1W )
         ] ]  ]
end


to askforchange
  ask patches [
    if Willingstochange = true  [change]   ;; this ask the patches that are willing to change (have a surpuls) to go and change
  ]
end


to change
  ask neighbors with [Willingstochange = false ]  [         ;; this asks if the patch had neigbors with a shortage
  set Atractiveneighbor min-one-of patches [atractiveness]  ;; this asks to give the neigbor with the lowest patchcount/senario ratio
  ]

  ask patches with [Willingstochange = true]  [
    set Land-use ([Land-use] of Atractiveneighbor)   ;; this asks the patches to change their land-use to the land-use of neigbor with the lowest patchcount/senario ratio
  ]     

end

<强> signup.php

<?php
include_once 'dbh.php';
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title> Signup Form </title>
    </head>
    <body>
        <form action="signup.php" method="POST">
            <input type="text" name="firstname" placeholder="First Name">
            <br>
            <input type="text" name="lastname" placeholder="Last Name">
            <br>
            <input type="text" name="email" placeholder="E-mail">
            <br>
            <input type="text" name="uid" placeholder="User name">
            <br>
            <input type="password" name="pwd" placeholder="Password">
            <br>
            <button type="submit" name="submit">Sign up</button>
            &nbsp;
        </form>
        <?php
        $sql = "SELECT * FROM users; " ;
        $result = $mysqli->query($sql);             //connects the database to the query we just generated
        $resultcheck = $result->num_rows;       // it returns the number of rows in the query

        if($resultcheck > 0){
            while($row = $result->fetch_assoc()){
                echo $row['user_uid'].'<br>';
            }
        }
        ?>
    </body>
</html>

<强> dbh.php

<?php
include_once 'dbh.php';

$firstname = $mysqli->real_escape_string($_POST['firstname']);
$lastname = $mysqli->real_escape_string($_POST['lastname']);
$email = $mysqli->real_escape_string($_POST['email']);
$uid = $mysqli->real_escape_string($_POST['uid']);
$pwd = $mysqli->real_escape_string($_POST['pwd']);

$sql = "INSERT INTO users (`user_firstname`, `user_lastname`, `user_email`, `user_uid`, `user_pwd` ) VALUES ('$firstname','$lastname','$email','$uid', '$pwd');";


if($result=$mysqli->query($sql)){
    echo "<p>User successfully added to database</p>".'<br>';
}
else{
    echo "Error enterting user into database!".$mysqli->error.'<br>';
}

header("Location: index.php?signup=success");

请阅读此参考资料http://php.net/manual/en/book.mysqli.php

答案 3 :(得分:-1)

应该是这样的

$sql = "INSERT INTO users (firstname, lastname, email, uid, pwd ) VALUES ('$firstname','$lastname','$email','$uid', '$pwd')";

mysqli_query($conn,$sql);