the data is not inserting. i think there is something wrong in
$sql = "INSERT INTO `users` (`Username`, `Password`, `FirstName`, `LastName`, `Email`, `ContactNumber`)
VALUES ('".$_POST["Username"]."','".$_POST["Password"]."','".$_POST["FirstName"]."','".$_POST["LastName"]."','".$_POST["Email"]."','".$_POST["ContactNumber"]."')";
When i try to change the statement in "else" with echo "successs"; its working.
please someone can tell me what is wrong.
<?php
error_reporting(E_ALL & ~E_NOTICE);
if(isset($_POST["Register"]))
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbuseraccounts";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$user = $_POST['Username'];
$pass = $_POST['Password'];
$query = mysqli_query($conn, "SELECT * FROM users WHERE Username= '".$user."'");
if(mysqli_num_rows($query) > 0)
{
echo "email already exists";
}
else
{
$sql = "INSERT INTO users (Username, Password, FirstName, LastName, Email, ContactNumber)
VALUES ('".$_POST["Username"]."','".$_POST["Password"]."','".$_POST["FirstName"]."','".$_POST["LastName"]."','".$_POST["Email"]."','".$_POST["ContactNumber"]."')";
}
$conn->close();
}
?>
Your data is not inserting because you haven't even executed your query.
if (mysqli_num_rows($query) > 0) {
echo "Username already exists";
} else {
$sql = "INSERT INTO users (Username, Password, FirstName, LastName, Email, ContactNumber) VALUES ('".$_POST["Username"]."','".$_POST["Password"]."','".$_POST["FirstName"]."','".$_POST["LastName"]."','".$_POST["Email"]."','".$_POST["ContactNumber"]."')";
/* Run your query and check for errors */
$query = mysqli_query($conn, $sql) or die(mysqli_error($conn));
}
Please Execute query using
mysqli_query($conn, your query);
Related
My website fully functions on localhost but now that I put it on web hosting the account doesn't work, I cannot register or login. I know that it works because it previously did so there is a problem with the connection to the user table in my database. The other table with products is successfully connected. How do I fix this problem in my code?
<?php
include ('connect.php');
$conn = connect();
$email = $_POST['Email'];
$pass = $_POST['Password'];
$type = $_POST['type'];
if($type == "signup"){
$sql = $conn->prepare("SELECT * FROM user WHERE Email like ?");
$sql->bind_param('s', $email);
$sql->execute();
$result = $sql->get_result();
if ($result->num_rows > 0) {
echo "This email is already in use";
}
else{
/*Part 1*/
$sqlInsert = $conn->prepare("INSERT INTO `user` (`UID`, `Email`, `Password`, `FName`, `LName`, `Country`, `City`, `Address`) VALUES (NULL, ?, ?, '', '', '', '', '');");
$sqlInsert->bind_param('ss', $email, $pass);
$sqlInsert->execute();
echo "Success!";
}
}
else if($type == "login"){
$sql = $conn->prepare("SELECT * FROM user WHERE Email like ?");
$sql->bind_param('s', $email);
$sql->execute();
$result = $sql->get_result();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()){
if($row["Password"] == $pass){
// Log in
session_start();
$_SESSION['login_user'] = $email;
$_SESSION['account_id'] = $row["UID"];
echo "Success!";
}
else{
echo "Wrong credentials!";
}
}
}
else{
echo "Wrong credentials!";
}
}
mysqli_close($conn);
?>
I think the problem may be here
<?php
include 'config.php';
function connect() {
$conn = new mysqli(SERVER_NAME, USER, PASSWORD, DB_NAME);
if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
return $conn;
}
?>
you can use this code please fill below fields
my_user: default 'root'
my_password: default ''
my_db: your database name
<?php
include 'config.php';
function connect()
{
$con = mysqli_connect("localhost", "root", "", "my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
return $con;
}
?>
I am unable to pass proper data to MySQL I have no idea what is wrong done by me please help me out my code is as follow:
$con = mysqli_connect('localhost', 'root', '', 'database');
if($con) {
echo "we are connected";
} else {
die("connection failed");
}
if(isset($_POST['submit'])) {
// echo "Yeah it works";
$user = $_POST['username'];
$pass = $_POST['password'];
$query = "INSERT INTO users(username, password) VALUES ('$user', '$pass')";
$result = mysqli_query($con, $query);
if(!$result) {
die("Query failed");
}
}
When I run this code I'm getting 0 for username and 0 for password. whatever I type for username and password I get 0 in database.
Try this code, which also removes SQL injections.
$con = mysqli_connect('localhost', 'root', '', 'database');
if(! $con )
die('Unable to connect');
foreach(array('submit', 'username', 'password') as $arg)
if(! isset($_POST[$arg]) )
die('Missing argument(s)');
unset($arg);
http_response_code(200);
$username = $con->real_escape_string($_POST['username']);
$password = $con->real_escape_string($_POST['password']);
$query = "INSERT INTO users (username, password) VALUES ('{$username}', '{$password}')";
if(! mysqli_query($con, $query))
http_response_code(400);
If this code doesn't work, make sure that username and password from your table are varchar's not int's. (Because your script inserts a 0 value)
I have one table called users (id, username, password), second table called profiles (id, user_id, username, name, lastname, age, gender, country, company_name), third table called companies (id, user_id, name, resources_id) and fourth table called resources (id, user_id, company_id, money). When users register their account, they need to create their profile. Now when they fill profile data, profiles and companies tables are filled just fine.
My problem is in resources table:
Error: Incorrect integer value: '' for column 'company_id' at row 1
When I refresh that page with error, resources table is filled fine, but now I got double rows for profiles and companies tables.
My Code :
<?php
session_start();
$dbserver = "localhost";
$dbusername = "root";
$dbpassword = "1234512345";
$db = "game";
$conn = new mysqli($dbserver, $dbusername, $dbpassword, $db);
if ($conn->connect_error)
{
die("Connection failed: ".$conn->connect_error);
}
if(isset($_SESSION['loggedin']))
$username = $_SESSION['loggedin'];
$query = "SELECT id FROM users WHERE username = '$username'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
$userId = $row['id'];
$_POST['id'] = $userId;
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$country = $_POST['country'];
$company_name = $_POST['company_name'];
$_POST['loggedin'] = $username;
$query = "SELECT id FROM companies WHERE name = '$company_name'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
$companyId = $row['id'];
$_POST['id'] = $companyId;
//INSERT DATA INTO PROFILES
$sql = "INSERT INTO profiles (user_id, username, name, lastname, age, gender, country, company_name)
VALUES ('$userId', '$username', '$name', '$lastname', '$age', '$gender', '$country', '$company_name')";
//INSERT DATA INTO COMPANIES
$sql2 = "INSERT INTO companies (user_id, name)
VALUES ('$userId', '$company_name')";
//INSERT DATA INTO RESOURCES
$sql3 = "INSERT INTO resources (user_id, company_id)
VALUES ('$userId', '$companyId')";
if($conn->query($sql) && $conn->query($sql2) && $conn->query($sql3) === TRUE)
{
header("Location: ../../index.php?page=profile");
die();
}
else
{
echo "Error: ".$conn->error;
}
?>
What should I do differently? I'm still beginner into this, sorry for long post.
EDIT:
One guy helped me out with this code, it's more error proof than last version. Deleted companies table, merged it with profiles, now everything works correctly!
<?php
session_start();
$dbserver = "localhost";
$dbusername = "root";
$dbpassword = "1234512345";
$db = "game";
$conn = new mysqli($dbserver, $dbusername, $dbpassword, $db);
if ($conn->connect_error)
{
die("Connection failed: ".$conn->connect_error);
}
if(isset($_SESSION['loggedin']))
$username = $_SESSION['loggedin'];
$query = "SELECT id FROM users WHERE username = '$username'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
$userId = $row['id'];
$_POST['id'] = $userId;
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$country = $_POST['country'];
$company_name = $_POST['company_name'];
$_POST['loggedin'] = $username;
$query = "SELECT id FROM companies WHERE name = '$company_name'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
$sql1 = "INSERT INTO profiles (user_id, username, name, lastname, age, gender, country, company_name) VALUES (?,?,?,?,?,?,?,?)";
$sql2 = "INSERT INTO resources (user_id) VALUES (?)";
try {
$conn->autocommit(false);
$statement = $conn->prepare($sql1);
$statement->bind_param("dsssdsss", $userId, $username, $name, $lastname, $age, $gender, $country, $company_name);
$statement->execute();
$statement = $conn->prepare($sql2);
$statement->bind_param("d", $userId);
$statement->execute();
// COMMIT THE CHANGES
$conn->commit();
header("Location: ../../index.php?page=profile");
}
catch(Exception $e){
// undo everything that was done in the try block in the case of a failure.
$conn->rollback();
echo "Error: ".$conn->error;
}?>
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 7 years ago.
i've Apach2, Mysql and php (also php-mysql); i'm trying to insert from a form (varchar, varchar, password, date, varchar)into the table utenti in my db music:
Describe Utenti
I'm using procedural style mysqli: It says me "Insert success" but it doesn't write for real on the Db;
<?php
$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
$password = $_POST['password'];
$datanascita = $_POST['datanascita'];
$email = $_POST['email'];
$host = "localhost";
$user = "root";
$password = "popolo";
$dbname = "music";
//Connessione
$con = mysqli_connect($host, $user, $password, $dbname);
//verifica eventuali errori
if (mysqli_connect_errno()) {
echo "Connesione fallita" . mysqli_connect_error();
exit();
} else {
echo "Connected \n";
//Inserting record in table using INSERT query
$mysqli = "INSERT INTO Utenti (`nome`, `cognome`, `password`, `datanascita`, `email`)
VALUES ($nome, $cognome, $password, $datanascita, $email)";
mysqli_query($conn, $mysqli);
echo "Insert success";
}
mysqli_close($conn);
?>
whatever your query succeeded or not this message echo "Insert success"; will output, you should check if your query succeeded first , you must your varchar in 2 '
$mysqli = "INSERT INTO Utenti (`nome`, `cognome`, `password`, `datanascita`, `email`)
VALUES ('$nome', '$cognome','$password', '$datanascita', '$email')";
you can check like this:
$result = mysqli_query($conn,$mysqli);
if($result){
echo "Insert success";
} else {
echo "Insert failed, Error: ".$mysqli->error;
}
hi im creating a login page where it would insert data into a php table but it does not seem to insert data into the table i have 2 tables in 1 database
hrms
<?php
$servername = "localhost";
$user = "root";
$dbpassword="";
$dbname = "hrms";
$username=$_POST['username'];
$passphrase=$_POST['passphrase'];
$conn = new mysqli($servername, $user, $dbpassword, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM user WHERE username='$username' AND passphrase='$passphrase'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$sql = "INSERT INTO access (user, status)
VALUES ('John', 'locked')";
header("location: main.php");
} else {
header("location: index.php");
}
$conn->close();
?>
You forgot to execute the query, do like below:
$sql = "INSERT INTO access (user, status) VALUES ('John', 'locked')";
$conn->query($sql);