I've setup a sign up page to register users, its passed from HTML and into PHP
however the PHP function is not passing it over to the MySQL database
<?php
try{
$db = new PDO ("mysql:host=localhost;dbname=car_rental;port=3306","root","");
}
catch (Exception $e){
echo "SQL is Off";
exit;
}
echo "success";
try{
$trial = "INSERT INTO users (firstName) VALUES ('trial')";
}
catch (Exception $e){
echo "doesnt work..";
}
echo "works?";
try{
function NewUser()
{
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$age = $_POST['age'];
$email = $_POST['email'];
$password = $_POST['pass'];
$query = "INSERT INTO users (firstName,lastName,age,email,pass) VALUES ('$firstName','$lastName','$age','$email','$password')";
echo"user created";
}
}
catch (PDOException $e)
{
echo "ERROR -_-";
}
?>
is this the correct implementation to execute a sql query in PHP?
function NewUser()
{
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$age = $_POST['age'];
$email = $_POST['email'];
$password = $_POST['pass'];
$query = "INSERT INTO users (firstName,lastName,age,email,pass) VALUES ('$firstName','$lastName','$age','$email','$password')";
$db->exec($query);
echo"user created";
}
Thanks
You just write queries, but forgot to execute
$db = new PDO ("mysql:host=localhost;dbname=car_rental;port=3306","root","");
$query = "INSERT INTO users (firstName,lastName,age,email,pass) VALUES ('$firstName','$lastName','$age','$email','$password')";
$db->query($db); // executes it
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();
}
?>
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;
}
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 6 years ago.
I'm creating a website and i want to insert data into a phpmyadmin table from a form (method="post") it didn't work i'm connected to the data base but when i type stuff in my form it's not inserted in the table, here's my php part:
<?php
$hostname = 'localhost';
$username = 'root';
$password = '';
try {
$conn = new PDO("mysql:host=$hostname;dbname=Database", $username, $password);
echo 'Connected to database';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$nom = $_POST['nom'];
$prenom =$_POST['prenom'];
$email = $_POST['email'];
$password = $_POST['password'];
$type = $_POST['type'];
$sql = "INSERT INTO client (nom, prenom, email,password,type)
VALUES ($nom, $prenom, $email, $password , $type)";
}
$conn->connection = null;
?>
I'm not gonna comment much, there's still a lot of learning and
practice that you need to do. Please take your time and go through
this blog, read and practice from it, do not rush take your time
https://phpdelusions.net/pdo
Your code should be looking similar to the one below :
<?php
$hostname = 'localhost';
$username = 'root';
$password = '';
try {
$conn = new PDO("mysql:host=$hostname;dbname=Database", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo $e->getMessage();
}
$nom = $_POST['nom'];
$prenom = $_POST['prenom'];
$email = $_POST['email'];
$password = $_POST['password'];
$type = $_POST['type'];
try {
$sql = "INSERT INTO client (nom, prenom, email,password,type) VALUES (?,?,?,?,?)";
$stmt = $conn->prepare($sql);
if ($stmt->execute(array(
$nom,
$prenom,
$email,
$password,
$type
))) {
echo "Data inserted";
} else {
echo "could not insert";
}
}
catch (Exception $ex) {
error_log($ex->getMessage());
}
?>
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);
}