I have been tying to figure out what is wrong for a long time, but I cannot figure it out. I have been trying to connect to a local MySQL database. When I run this, no error is thrown. Also, I know that the database and the table exist, and I know that my username and password are correct.
<?php
// Connects to your Database
$con = mysqli_connect("localhost", "**MY_USERNAME**", "**MY_PASSWORD**", "gamesite_data") or die(mysql_error());
$username = $_POST["username"];
$password = $_POST["password"];
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$age = $_POST["age"];
mysqli_query($con, "INSERT INTO information (`username`, `password`, `first_name`, `last_name`, `age`) VALUES ($username, $password, $firstname, $last_name, $age);"
);
?>
<?php
mysqli_close($con)
?>
If the values of username, password, first_name, last_name are strings, you have to surround them with single quotes here:
mysqli_query($con, "INSERT INTO information (`username`, `password`, `first_name`, `last_name`, `age`) VALUES ('".$username."', '".$password."', '".$firstname."', '".$last_name."', ".$age.")");
And if you use mysqli_connect
, you have to use also mysqli_error
instead of mysql_error
.
try this
<?php
// Connects to your Database
$con = new mysqli("localhost", "**MY_USERNAME**", "**MY_PASSWORD**", "gamesite_data") or die(mysql_error());
$username = $_POST["username"];
$password = $_POST["password"];
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$age = $_POST["age"];
$query="INSERT INTO information (`username`, `password`, `first_name`, `last_name`, `age`) VALUES ('$username', '$password', '$firstname', '$last_name', '$age')";
$con->query($query);
$con->close();
?>
Here:
mysqli_query($con, "INSERT INTO information (`username`, `password`, `first_name`, `last_name`, `age`) VALUES ($username, $password, $firstname, $last_name, $age);"
);
Should be
mysqli_query($con, "INSERT INTO information (`username`, `password`, `first_name`, `last_name`, `age`) VALUES ($username, $password, $first_name, $last_name, $age);"
);
You dont have $firstname
variable, you have $first_name
Also keep in ming that you need to use mysqli_real_escape_string()
for all $_POST
values.
And i think is a good practice to pass variable into query as strings :
"INSERT INTO information (`username`, `password`, `first_name`, `last_name`, `age`) VALUES ('$username', '$password', '$first_name', '$last_name', '$age');"
i noticed your code here you made 2 mistake first one is you take wrong variable name into your sql query .you are declare $first_name variable but you are taking $firstname and second one you are taking extra semicolon (;) just after your value's end.
please use this code
<?php
$con = mysqli_connect("localhost", "root", "", "gamesite_data");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$username = $_POST["username"];
$password = $_POST["password"];
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$age = $_POST["age"];
$query = "INSERT INTO information (`username`, `password`, `first_name`, `last_name`, `age`) VALUES ($username, $password, $first_name, $last_name, $age)";
$result = mysqli_query($con, $query);
?>
hope its worked
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.