Form data is not getting inserted in MYSQL . Form successfully posts data (I have checked with var dump). Please help me with this.
This is my action PHP.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname= "test";
$conn = new mysqli($servername,$username,$password,$dbname);
if($conn->connect_error){
die("connection failed:" .$conn->connect_error);
}
else{
echo "Connected successfully";
}
$selected = mysqli_select_db($conn,$dbname);
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$address = $_POST['address'];
//$gender = $_POST['male'];
//$gender = $_POST['female'];
$sql = "INSERT INTO test1 (first_name,last_name,email,mob,home_address) VALUES ('$first_name','$last_name','$email','$mobile','$address')";
var_dump($sql);
?>
You are missing the last part which executes the query to save the data:
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
Related
I am trying to POST my data in my database but unable to do... Which mistake I did here? I have simply try to print Query but it prints without any value....
If I did any mistake then let me know..
And suggest me what can I do now...
<?php
if(isset($_POST['firstname'])){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sms";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(!$conn){
die("Could not connect to the database due to the following error --> ".mysqli_connect_error());
}
//echo "success";
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$mobno = $_POST['mobno'];
$dob = $_POST['dob'];
$sql1="INSERT INTO `register`(`firstname`, `lastname`, `email`, `mobno`, `dob`) VALUES ('$firstname','$lastname','$email','$mobno','$dob')";
echo $sql1;
if($conn->query($sql1) == true){
// echo "Successfully inserted";
// Flag for successful insertion
$insert = true;
}
else{
echo "ERROR: $sql1 <br> $conn->error";
}
// Close the database connection
$conn->close();
}
?>
every thing is getting inputted correctly but date is being submitted into database like 0000-00-00. please help!! i tried to echo my date before posting it into dob and it coming out to be correct as inserted into the form. but when i check my database the dob columnsgets updates with a 0000-00-00.
here is the code:
<?php
if (isset($_POST['submit'])) {
echo "<pre>";
var_dump($_POST);
echo "</pre>";}
$username = $_POST['username'];
$password = $_POST['password'];
//$salary = $_POST['salary'];
$birthdate = $_POST['year'] . '-' . $_POST['month']. '-' .$_POST['day'];
echo $birthdate."<br>";
if(!empty($username)){
if(!empty($password)){
if(!empty($birthdate)){
$servername = "localhost";
$usernamei = "root";
$passwordi = "password";
$databasei = "regis";
// Create connection
$conn = new mysqli($servername, $usernamei, $passwordi, $databasei);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else {
echo $birthdate;
$sql = "INSERT INTO iocl (user_name, password,dob)
values ('$username','$password','$birthdate')";
if($conn->query($sql)){
echo "new record is inserted sucess";
}
else{
echo "error: ".$sql."<br>".$conn->error;
}
$conn->close();
}}}}
?>
m making this user signup form and linking the user email name and password tot he table in mysql DB, but it's not showing any row in mySQl DB.
Here is the code:
if(isset($_POST['submit']))
{
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$query_input = mysql_query($connect,"INSERT INTO user_basic_info(username,email,password)VALUES('$username','$email','$password')");
if($query_input){
echo "done and dope";
}
else{
echo "no";
}
}
You have a error in your mysql_query function
$query_input = mysql_query("INSERT INTO user_basic_info(username,email,password) VALUES('$username','$email','$password')",$connect);
see http://php.net/manual/en/function.mysql-query.php
BTW please use mysqli
mysqli_query("INSERT INTO user_basic_info(username,email,password)VALUES('$username','$email','$password')",$connect);
Firstly you should try to use MySQLi or PDO (tutorial here)
$connect = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['submit']))
{
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$query_input = mysqli_query($connect,"INSERT INTO user_basic_info(username,email,password)VALUES('$username','$email','$password')");
if($query_input){
echo "done and dope";
}
else{
echo "no";
}
//close db connection
mysqli_close($connect);
}
well i just made a form in HTML witch accepts user inputs and a mysql database to store them, now in the php file everything goes well no errors but the problem is the data never displays in the database, here is the php file:
<?php
if(isset($_POST["submitacc"])){
$servernm = "localhost";
$serverusrnm = "root";
$serverpass = "2003";
$db = "blue";
$conn = new mysqli($servernm, $serverusrnm, $serverpass, $db);
if($conn ->connect_error){
die("connection failed".$conn->connect_error);
}
$fnm = $_POST["fnm"];
$lnm = $_POST["lnm"];
$mail = $_POST["mail"];
$pass = $_POST["pass"];
$age = $_POST["age"];
$gender = $_POST["gender"];
if(isset($_POST["gender"])&&$_POST["gender"]=="male"){
$gender = "male";
}else {
$gender = "female";
}
$mysql="update createacc set fnm = '$fnm', lnm = '$lnm', mail = '$mail', passwod = '$pass', age = '$age', gender = '$gender' ";
if($conn->query($mysql)== true){
echo "record updated";
}else {
echo "error updating record".$conn->error;
}
$conn->close();
}
?>
Use mysqli_query() instead of query(). Also use WHERE clause in your $mysql variable. The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!
Example:
if(mysqli_query($conn , $mysql)){
echo "Records were updated successfully.";
} else {
echo "ERROR: Could not able to execute $mysql. " . mysqli_error($conn);
}
I have faced another problem please help.
when i query to my db 1 time, 2 rows entered into the db!
Here is the code:
<?php
$servername = "localhost";
$username = "***";
$password = "***";
$dbname = "***";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$name = $_POST['name'];
$famil = $_POST['famil'];
$email = $_POST['email'];
$amount = $_POST['amount'];
$name = mysqli_real_escape_string($conn, $name);
$famil = mysqli_real_escape_string($conn, $famil);
$email = mysqli_real_escape_string($conn, $email);
$amount = mysqli_real_escape_string($conn, $amount);
$sql = "INSERT INTO users".
"(name, famil, email, amount)".
"VALUES ('$name','$famil','$email','$amount')";
mysqli_query($conn,$sql);
if (mysqli_query($conn, $sql)) {
echo "Data entered successfully.";
} else {
echo "Error entering data: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
and the result after 2 times run this code:
3 mohsen gholi ***#yahoo.com 235354346
4 mohsen gholi ***#yahoo.com 235354346
5 mohsen gholi ***#yahoo.com 235354346
6 mohsen gholi ***#yahoo.com 235354346
Remove this line as said by Fred
mysqli_query($conn,$sql);
and just use:
if (mysqli_query($conn, $sql)) {
echo "Data entered successfully.";
} else {
echo "Error entering data: " . mysqli_error($conn);
}