PHP INSERT INTO localhost not working - php

After running a SELECT * FROM users, there is no difference to the table.
$firstName = $_POST["firstName"];
$lastName = $_POST["lastName"];
$dateOfBirth = $_POST["dateOfBirth"];
$gender = $_POST["gender"];
$fitnessLevel = $_POST["fitnessLevel"];
$number = $_POST["number"];
$address = $_POST["address"];
$password = $_POST["password"];
$user = 'root';
$pass = 'root';
$db = 'gymmembers';
$db = new mysqli('localhost',$user,$pass,$db) or die("Error, try again");
mysqli_query($db, "INSERT INTO users(`firstName`,`lastName`,`dateOfBirth`,`gender`,`fitnessLevel`,`number`,`address`,`password`)
VALUES('$firstName','$lastName','$dateOfBirth','$gender','$fitnessLevel','$number','$address','$password')" or die(mysqli_error()));
I can echo any of the variables and they show, so the data from the form is being passed to here.
Thanks :-)

Actually you putted die(mysqli_error()) inside mysqli_query() which is not correct, do like below:-
mysqli_query($db, "INSERT INTO users(`firstName`,`lastName`,`dateOfBirth`,`gender`,`fitnessLevel`,`number`,`address`,`password`)
VALUES('$firstName','$lastName','$dateOfBirth','$gender','$fitnessLevel','$number','$address','$password')") or die(mysqli_error($db));
Note:- add $db in mysqli_error() so that if any error occur you will come to know.

Related

PHP for MSSQL insert runs but no data inserted

I have a PHP script that is attempting to INSERT form data into a MSSQL database. When I run the script, the script runs successfully, but the INSERT doesn't seem to post the data. Here is the relevant code:
<?php
//collect data from form
$username = $_POST['username'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
$password = $_POST['password'];
$serverName = "192.168.1.1"; //serverName\instanceName
$connectionInfo = array( "Database"=>"mydatabase", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
$sql = "INSERT INTO Member_Details (FirstName,LastName,Address,City,State,Zip_Code,Telephone,Email,User_Name,Password,Is_Validated) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
$params = array($firstname,$lastname,$address,$city,$state,$zip,$telephone,$email,username,$password,0);
$stmt = sqlsrv_query( $conn, $sql, $params);
echo "You were successfully registered for user name " . $username;
}else {
echo "Something went wrong";
}
?>
I have confirmed that form data is being passed successfully via the message passed upon the running of the script.
Any suggestions would be appreciated!
Mike
As far as i remember, you have to prepend the dbo. (or whatever the owner of the table is) to the table name.
like
insert into dbo.tablename (cols) values (vals)

0 concat displays first and last name but only first name is saved to mysql database

<?php
$servername = "localhost";
$username = "root";
$password = "";
$databasename = "test";
$conn = new mysqli($servername , $username , $password, $databasename);
$name = $_POST["firstname"];
$last = $_POST["lastname"];
$statement = mysqli_prepare($conn, "INSERT INTO user(firstname ,lastname) VALUES(?,?)");
mysqli_stmt_bind_param($statement ,"si", $name,$last);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
where is the problem in above php script that it save the first name in database while cannot save the lastname.
The problem is because of this line,
mysqli_stmt_bind_param($statement ,"si", $name,$last);
^ see here
It should be,
mysqli_stmt_bind_param($statement ,"ss", $name,$last);

Is there any error in my sql query?

I want to update my user_info table but it's not working. In my user_info I have already taken some values from one activity and from another activity I want to take some more values but it's not working. I'm pretty sure that there is no error in my Java code.. is there any error in my query?
$db_name = "db";
$mysql_user = "root";
$mysql_pass = "";
$server_name = "localhost";
$con = mysqli_connect($server_name, $mysql_user, $mysql_pass, $db_name);
$name = $_POST["user"];
$user_email = $_POST["user_email"];
$Gender = $_POST["Gender"];
$contact = $_POST["contact"];
$address = $_POST["address"];
$sql_query = "UPDATE user_info SET name='$name', Gender='$Gender', contact='$contact', address='$address' WHERE user_email='$user_email'";
mysqli_query($con,$sql_query)
mysqli_close($con);
Im not sure without seeing you database table but "Gender" is capitalized while the other field names are lowercase. Just a shot in the dark here, otherwise your query looks fine.

Database connect undefined

I'm getting an error saying Undefined variable: con,
the connection of the database is on the other php file, (include() is already on top of the code). I just don't know how to call the $con
if (isset($_POST['update_profile']))
{
if (isset($_POST['first_name']))
{
$first_name = mysqli_real_escape_string($con, $_POST['first_name']);
$sql = mysqli_query($con, "UPDATE tbl_fbusers SET fname = '$first_name' WHERE email = '$email_to_connect'");
}
if (isset($_POST['last_name']))
{
$last_name = mysqli_real_escape_string($con, $_POST['last_name']);
$sql = mysqli_query($con, "UPDATE tbl_fbusers SET lname = '$last_name' WHERE email = '$email_to_connect'");
}
if (isset($_POST['contact']))
{
$contact = mysqli_real_escape_string($con, $_POST['contact']);
$sql = mysqli_query($con, "UPDATE tbl_fbusers SET contact = '$contact' WHERE email = '$email_to_connect'");
}
}
here is the other php file
class Users {
public $table_name = 'tbl_fbusers';
function __construct(){
//database configuration
$dbServer = 'localhost'; //Define database server host
$dbUsername = 'root'; //Define database username
$dbPassword = ''; //Define database password
$dbName = 'db_zalian'; //Define database name
//connect databse
$con = mysqli_connect($dbServer,$dbUsername,$dbPassword,$dbName);
if(mysqli_connect_errno()){
die("Failed to connect with MySQL: ".mysqli_connect_error());
}else{
$this->connect = $con;
}
}
Thanks!
Defining $con as a GLOBAL variable is a terrible idea...
I suggest to make a file (eg. connection.php) that will contain the $con variable that is not in a function, and then include the connection.php to your other php files. It's more secure and easier, and you won't get to any troubles.
Since you have a class you need to initialize user class.
$user = new Users();
and then
$con = $user->connect;
Here you can run your sql like:
$contact = mysqli_real_escape_string($con, $_POST['contact']);
$sql = mysqli_query($con, "UPDATE tbl_fbusers SET contact =......... etc.
you need to define $con as global:
global $con
$con = mysqli_connect($dbServer,$dbUsername,$dbPassword,$dbName);

Database linking using php

I am getting an error in my php code for database linking.I am trying to link my website login page with my database hosted on server.Here is the code
init.php
<?php
define('HOST','##########');
define('USER','##########');
define('PASS','##########');
define('DB','##########');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
?>
<?php
require "init.php";
$username = $_POST["username"];
$password = $_POST["password"];
$fullname = $_POST["fullname"];
$sex = $_POST["sex"];
$country = $_POST["country"];
$address = $_POST["address"];
$contact = $_POST["contact"];
$email = $_POST["email"];
$dob = $_POST["dob"];
$flag = $_POST["flag"];
$sql = "insert into signup('".$username."','".$password."','".$fullname."','".$sex."','".$country."','".$address."','".$contact."','".$email."','".$dob."','".$flag."');";
if(mysqli_query($con,$sql))
{ echo"<br><h3>One row inserted....</h3>"; }
else
{ echo "Error in insertion...." . mysqli_error($con);
}
?>
ERROR
Error in insertion....You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near ''','','','','','','','','','')' at
line 1
Your insert query should be like:
$sql = "insert into signup values('".$username."','".$password."','".$fullname."','".$sex."','".$country."','".$address."','".$contact."','".$email."','".$dob."','".$flag."');";
You missed VALUES in insert query. And use bind_param() to bind the values in query instead of directly including.
$sql = "insert into signup VALUES('".$username."','".$password."','".$fullname."','".$sex."','".$country."','".$address."','".$contact."','".$email."','".$dob."','".$flag."');";

Categories