在MySQL数据库中添加行

I am pretty new with databases and SQL, and I am learning to program. I have a table like this. I use phpMyAdmin.

I have to fill a form and when I click the button submit, I must add some datas in this database called "europe".

<form action="addtime.php" method="POST">
 //html table
 <p align="center"><input value="Send record" type="submit"></p>
</form>

And here you can see addtime.php:

<?php

$con=mysqli_connect("localhost","username","password","my_mk7vrlist");

mysql_query(INSERT INTO europe (Num,playername,Friendcode,Country,Skype,Twitter,Youtube) VALUES (3, "kjghskj", "t4glofshgk", "es", "jgd", "49hfis", "vvvv44444"));

mysqli_close($con);
?>

The database name is my_mk7vrlist and the table name is europe. I have an error that says "unexpected T_STRING on line 22". On that line there is the function mysql_query();.

What am I missing?

It's been a bit since I've dealt with PHP but this should work (make appropriate changes to the username, password and database fields; I removed the name of the database to force you to re-write it, in case it's actually spelled wrong):

<?php

$con = mysqli_connect('localhost', 'user_name', 'password', 'database');

mysqli_query($con, "INSERT INTO europe (Num, playername, Friendcode, Country, Skype, Twitter, Youtube) VALUES (3, 'kjghskj', 't4glofshgk', 'es', 'jgd', '49hfis', 'vvvv4444'");

mysqli_close($con);

?>

I'd suggest the following though:

<?php

$db_con = mysqli_connect('localhost', 'user_name', 'password', 'database');

$db_query = "INSERT INTO europe"
    . " (Num, playername, Friendcode, Country, Skype, Twitter, Youtube)"
    . " VALUES (3, 'kjghskj', 't4glofshgk', 'es', 'jgd', '49hfis', 'vvvv4444'";

mysqli_query($db_con, $db_query);

mysqli_close($db_con);

?>

In short, as others have mentioned, when you're passing your SQL to the mysql_query and mysqli_query functions, it needs to be a String data type (Either double or single quoted).

In this example you don't need double quotes, but I'd suggest you make it a habit, because typically when working with databases you're not going to be passing raw values like the above; you're going to be passing variables into the string, which a double quote allows (a single quoted string does not make that check in PHP). Take this into account, because your original code had your connection variables encapsulated in double quotes; this means that PHP's interpreter is going to make pass to check for variables first, and then interpret the actual string.

However, what others left out (when I began writing this) is that you're writing procedurally and not utilizing the mysqli object; this requires you to pass a connection variable to the mysqli_query as well, otherwise how is it going to query the data?

So, this is why $db_con is the first argument, and $db_query is the second. For reference, you could also do this (as an object):

<?php

$db = new mysqli('localhost', 'user_name', 'password', 'database');

$db_query = "INSERT INTO europe"
    . " (Num, playername, Friendcode, Country, Skype, Twitter, Youtube)"
    . " VALUES (3, 'kjghskj', 't4glofshgk', 'es', 'jgd', '49hfis', 'vvvv4444'";

$db->query($db_query);

$db->close();

?>

I also suggest instead of using the variable name "con", use something less ambiguous like "db_con" because someone who sees the variable name, and only the variable name, will not typically know what "con" means; with a "db" prefix, they could more easily infer this is a database connection variable.

"Unexpected T_STRING" from what I can recall is sort of an asinine error (it can mean a number of things), but typically it suggests a syntax error; for example, missing brackets, missing semicolon, missing quotes, et cetera. Typically you look at the beginning of the line it mentions or before it; I mention this because I can't assume that line 22 is the only error, unless PHP has come very far forward in it's error protocols.

Additionally, I broke your declaration into two parts; a query variable, and the query function call. This is, again, semantics and unnecessary, but you'll find it easier to maintain and read this way. I broke the query variable into separate lines to highlight important logical shifts in the SQL query. Again this is unnecessary now, but important to take into account for future cases when your queries get more complex (especially when working with joins).

Furthermore, I'd suggest your field names stay consistent; I noticed "playername" is not capitalized, and the others are capitalized. I'd suggest keeping them all lowercase and to separate inner words with "_". Consistency is more important than your code as most time goes into debugging than actual programming.

I apologize if anything is off point or if they're any errors (I'll make edits if necessary), it has been a while since I've worked with PHP.

References:

http://www.php.net/manual/en/mysqli.query.php

"usernale" are you it`s right? Maybe "username" and add a quotes '' in your query

Your sql query is a string, so enclose it properly:

"INSERT INTO europe (Num,playername,Friendcode,Country,Skype,Twitter,Youtube) 
VALUES (3, 'kjghskj', 't4glofshgk', 'es', 'jgd', '49hfis', 'vvvv44444')"

Rewrite your query like this

mysql_query("INSERT INTO `europe` (`Num`,`playername`,`Friendcode`,`Country`,`Skype`,`Twitter`,`Youtube`) VALUES (3, 'kjghskj', 't4glofshgk', 'es', 'jgd', '49hfis', 'vvvv44444')");

Also, stop using mysql_* functions as they are deprecated. Switch to MySQLi or PDO instead.

Put your query inside quotes. Becareful with single / double quotes.

<?php
$con=mysqli_connect("localhost","usernale","password","my_mk7vrlist");

mysqli_query($con, 'INSERT INTO europe (Num,playername,Friendcode,Country,Skype,Twitter,Youtube) VALUES (3, "kjghskj", "t4glofshgk", "es", "jgd", "49hfis", "vvvv44444")');

mysqli_close($con);

And change the mysql_query to mysqli_query (Procedural style)

mysqli_query("INSERT INTO...

might require the db $link:

mysqli_query($con, "INSERT INTO...