Is there any error in my sql query? - php

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.

Related

PHP INSERT INTO localhost not working

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.

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);

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);

I can not update database using php

For some reason I am can not update my database. Can anyone spot what I am doing wrong ?
Here is the code.
.......
session_start();
$user = mysql_real_escape_string($_POST['user']);
$email = mysql_real_escape_string($_POST['email']);
.......
if ($errorMessage == "") {
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ----- CHECKING SERVER
include('connect.php');
if (isset($user)) {
$sql = "UPDATE hookers ".
"SET user= ´$user´".
"WHERE email= ´$email´" ;
mysqli_query($con, $sql) or die("Can´t find user". mysql_error());
print "user updated";
mysqli_close($con);
}
}
connect.php file
$host = 'localhost';
$username = 'root';
$password = '';
$db = 'putas';
$con = mysqli_connect($host, $username, $password) or die("Can´t connect to server");
mysqli_select_db($con, $db) or die("Can´t connect to database");
Whenever I run the script it prints "Can´t find user". The variables $user & $email have the right data as I have checked it.
I would appreciate any help you guys can provide me.
Thanks in advance.
Oliver Tangari
You need to change all instances of ´ to '.
Your Sql Query Will Be like this:--
$sql = "UPDATE hookers ".
"SET `user`= '$user'".
"WHERE `email`= '$email'" ;
Hope it helps you...
Use ' single quote not `
$sql = "UPDATE hookers ".
"SET user= '$user'".
"WHERE email= '$email'" ;
As already mentioned the problem is ´
Why don't you use PDO and parameter binding instead? This won't give you this type of errors in the future.
<?php
// configuration
$dbtype = "sqlite";
$dbhost = "localhost";
$dbname = "test";
$dbuser = "root";
$dbpass = "admin";
// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
// new data
$user = 'Belén Esteban';
$email = 'belenesteban#telecinco.es';
// query
$sql = "UPDATE hookers
SET user=?
WHERE email=?";
$q = $conn->prepare($sql);
$q->execute(array($user,$email));
?>

Updating mysql database through php with HTTP POST methods

It executes on application perfectly I double checked it. It sends all parameters properly and the problem is in php script it doesn't execute the query (update,delete) it execute the insert query properly. The php script works perfect with html form but I don't know where the problem is.
Here is my php script:
$mysql_host = "localhost";
$mysql_database = "locator";
$mysql_user = "root";
$mysql_password = "";
mysql_connect($mysql_host,$mysql_user,$mysql_password) or die(mysql_error());
mysql_select_db($mysql_database);
$uMail = $_POST['u_mail'];
$uIMEI = $_POST['u_IMEI'];
$uName = $_POST['u_name'];
$uPass = $_POST['u_pass'];
$tName = $_POST['t_names'];
$tIMEI = $_POST['t_IMEIs'];
$tDesc = $_POST['t_desc'];
mysql_query("UPDATE user_master SET t_names='$tName',t_IMEIs='$tIMEI',t_desc= '$tDesc' WHERE u_mail = '$uMail' AND u_IMEI = '$uIMEI'");
mysql_close();
?>
maybe you should escape your strings with mysql_real_escape_string()
$uMail = mysql_real_escape_string($_POST['u_mail']);
$uIMEI = mysql_real_escape_string($_POST['u_IMEI']);
$uName = mysql_real_escape_string($_POST['u_name']);
$uPass = mysql_real_escape_string($_POST['u_pass']);
$tName = mysql_real_escape_string($_POST['t_names']);
$tIMEI = mysql_real_escape_string($_POST['t_IMEIs']);
$tDesc = mysql_real_escape_string($_POST['t_desc']);
mysql_query("UPDATE user_master SET t_names='$tName',t_IMEIs='$tIMEI',t_desc= '$tDesc' WHERE u_mail = '$uMail' AND u_IMEI = '$uIMEI'");
and make sure $uMail and $uIMEI are set correctly

Categories