-5

mysqli_query function not working

$query=mysqli_query($config,"INSERT into users (username,first_name,last_name,email,password) VALUES('$un','$fn','$ln','$em','$pass','$date','0')");"); 

When I echo the SQL query it worked fine but when I tried to insert into database its not doing anything and goes to next line .

I used a submit button like this $signup = @$_POST['signup'];

signup is the name of the submit button.

smottt
  • 3,272
  • 11
  • 37
  • 44
David
  • 23
  • 5

3 Answers3

1

Well you need to fix your query cause you've stated 5 fields and trying to insert 7 values...

So once you know the field names for "no_clue_what_this_guy_is_called and date" then you might progress further. Also, please watch your ") as you have too many...

And on another note, please don't use short variable names. It's just painful. It costs nothing to type out "big words". I.E. $un should be $user_name or the like.

So based on your question and bad variable names, you'd want something like...

$query = mysqli_query($config, "INSERT into users(
                    username,
                    first_name,
                    last_name,
                    email,
                    password,
                    date,
                    no_clue_what_this_guy_is_called
                    ) VALUES (
                    '$un',
                    '$fn',
                    '$ln',
                    '$em',
                    '$pass',
                    '$date',
                    '0'
                    )"); 

And you only need to quote columns that are varchars/text and the like. Do not use single quotes around integers.

There's more that can be said, but this should get you working on this part of your issue. I'm not sure what type '0' is defined as in your table.

TimBrownlaw
  • 5,457
  • 3
  • 24
  • 28
0

Informations

There are two basic syntaxes of INSERT INTO statement as follows:

INSERT INTO TABLE_NAME (column1, column2, column3,...columnN) VALUES (value1, value2, value3,...valueN);

You may not need to specify the column(s) name in the SQL query if you are adding values for all the columns of the table.

INSERT INTO TABLE_NAME VALUES (value1,value2,value3);

How to check errors

You can also check your connection if is estabilished or are there any errors with following code:

if (!$config) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

Or at the end you can check your query errors with :

echo mysqli_error($config);

More informations about debugging you can read here

Problems in your code

1. In your code you have too many ) it should be like follows:

$query=mysqli_query($config,"INSERT into users (username,first_name,last_name,email,password) VALUES('$un','$fn','$ln','$em','$pass','$date','0');");

2. Also you are trying to insert 7 values to 5 tables, there is too much in VALUES, or you have wrote too less tables.

3. Delete @ before your $_POST because maybe there is some error, and it cause that it will not show up. More informations can be found here

Karol Gasienica
  • 2,825
  • 24
  • 36
0

i suggest you to use like this

$con = mysqli_connect("host_name","db_username","db_password","db_name");

$query = "insert into tbl_name(col1,col2,col3) values('$val1','$val2','$val3')";

$insert = mysqli_query($con,$query);
Karthick
  • 281
  • 2
  • 7