php - Cannot insert rows into mysql database from an html form using phpmyadmin
Get the solution ↓↓↓Solution:
Since about the DB connection being
mysqli_*
based, and .
Assuming your DB connection variable is$con
change:
mysql_query($query);
tomysqli_query($con,$query);
mysqli_*
andmysql_*
functions do not mix together.
Or replace thexxx
with your credentials using the code below:
<?php
DEFINE ('DB_USER', 'xxx');
DEFINE ('DB_PASSWORD', 'xxx');
DEFINE ('DB_HOST', 'xxx');
DEFINE ('DB_NAME', 'xxx');
$con = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die("could not connect");
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$username=$_POST['newusername'];
$password=$_POST['newpassword'];
$email=$_POST['email'];
$query="INSERT into user (username,password,firstname,lastname,email) VALUES ('$username','$password','$firstname','$lastname','$email')";
mysqli_query($con,$query);
?>
To catch errors, place the following line of code before your DB connection code:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
Adding the radio button value to your DB
In order to get the value of the radio button inserted in your DB, you will need to add a column in your table, something to the effect of "gender", then add this to your code:
$gender=$_POST['sex'];
and change your query to this: (I usedgender
as the column name)
$query="INSERT into user (username,password,firstname,lastname,email,gender) VALUES ('$username','$password','$firstname','$lastname','$email','$gender')";
Sidenote: Your present code is open to .
I recommend you use , or
- An example of preparing and binding for
mysqli_
can be found .
To add a bit of protection to your present code, change this line:
$firstname=$_POST['firstname'];
to:
$firstname = mysqli_real_escape_string($con,$_POST["firstname"]);
and do the same for the others. Again, assuming your DB connection variable is$con
.
Since you are just beginning to get into coding:
Here are a few tutorials on prepared statements that you can study and try:
Here are a few tutorials on PDO:
Passwords
I also noticed that you are storing passwords in plain text. This is not recommended.
Use one of the following:
- PHP 5.5's
function.
Other links:
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: php_network_getaddresses: getaddrinfo failed: temporary failure in name resolution
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.