php - mySQL database not updating from form -
not sure error is. in html have form, , when click submit gives me error. connection working, reason won't add database
<?php if( isset( $_post['submit'] ) ){ $username = $_post['username']; $password = $_post['password']; $connection = mysqli_connect( 'localhost', 'root', '', 'loginapp' ); if($connection){ echo "we connected"; } else{ die( "connection failed" ); } $query = "insert 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 wondering if there wrong here code isnt working planned
$query = "insert 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 trying insert duplicate entry table. username
column. can around either providing value username
not in table yet, or delete record table can re-insert it. can more specific if include definition users
table in question, basic problem having.
if table defined numeric typed id
column primary key, , not specify new value in insert, this:
insert users (id, username, password) values (1, 'joeuser', 'password');
instead, doing have in question:
insert users (username, password) values ('joeuser', 'password');
the record inserted either have null
or 0
default insert value. each time insert performed same value inserted id
column, cause duplicate value error getting.
to around that, defining id
column auto-increment
tells anytime perform insert, , not specify value id
use next available integer default value.
the other approach specify new value id
on each execute of insert statement, so:
insert users (id, username, password) values (1, 'joeuser', 'password'); insert users (id, username, password) values (2, 'daveuser', 'password'); insert users (id, username, password) values (3, 'janeuser', 'password');
for table this, using auto-increment best way go.
Comments
Post a Comment