简体   繁体   中英

mySQL database not updating from form

Not to sure my error is. In my html I have a form, and when I click submit it gives me an error. My connection is working, but for some reason it won't actually add to the database

 <?php

    if( isset( $_POST['submit'] ) ){
        $username = $_POST['username'];
        $password = $_POST['password'];
        $connection = mysqli_connect( 'localhost', 'root', '', 'loginapp' );

        if($connection){
            echo "we are connected";
        }
        else{
            die( "connection failed" );
        }

        $query = "INSERT INTO users(username,password) ";
        $query.= "VALUES('$username','$password')";
        $result = mysqli_query( $connection, $query );

        if( !$result ){ // if not true, put query failed
            die('Query Failed' .mysqli_error($connection));
        }
    }

    ?>

I am wondering if there is something wrong with this here as this code isnt working as planned

  $query = "INSERT INTO users(username,password) ";
  $query.= "VALUES('$username','$password')";
  $result = mysqli_query( $connection, $query );

  if( !$result ){ // if not true, put query failed
      die( 'Query Failed' . mysqli_error( $connection ) );
  }
  }

You are trying to insert a duplicate entry into your table. likely the username column. You can get around this but either providing a value for username that is not in your table yet, or delete the record from your table so that you can re-insert it. I can get more specific if you include the definition for your users table in your question, but that is the basic problem you are having.

If your table is defined with a numeric typed id column as your primary key, and you do not specify a new value in your insert, like this:

insert into users (id, username, password) values (1, 'joeuser', 'password');

Instead, doing what you have in your question:

insert into users (username, password) values ('joeuser', 'password');

The record inserted will either have null or 0 as the default insert value. Each time the insert is performed the same value will be inserted into the id column, which will cause a duplicate value error as you are getting.

To get around that, defining the id column as auto-increment tells it that anytime you perform an insert, and do not specify a value for id it will use the next available integer as the default value.

The other approach would be to specify a new value for the id on each execute of the insert statement, like so:

insert into users (id, username, password) values (1, 'joeuser', 'password');

insert into users (id, username, password) values (2, 'daveuser', 'password');

insert into users (id, username, password) values (3, 'janeuser', 'password');

For a table like this, using auto-increment is the best way to go.

Alternatively; why not nest your Query within a Conditional statement and take it from there: like so:

    <?php
        if($username && $password){
            $query  = "INSERT INTO users(username, password) ";
            $query .= "VALUES('" . $username . "', '" . $password . "')";
            $result = mysqli_query($connection,$query);

            if(!$result) {
                die('Query Failed' .mysqli_error($connection));
            }               
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM