Update SQL database PHP - php

I have a form that posts variables back from a drop down list in php. I then need to take those values and update my database values for each "TeamName". I am using the following code but nothing is updating.
$g1 = mysql_real_escape_string($_POST['g1']);
$conn = mysql_connect("****.com","****","*****") or die("Connection to MYSQL");
mysql_select_db("****_teamlist", $conn) or die("Connection to MYSQL database failed");
$sSQL = "UPDATE Sheet1 Set g1='$g1' WHERE TeamName = 'TeamName' ";
$result = mysql_query($sSQL, $conn) or die(mysql_error());
Where is my error? I am stuck.

Related

php Update sql and Query

I want to do a query in php, output the data on the page and then modify it in the database.
How do I do that?
Currently I do it like this but it dose not work:
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM pics WHERE id = '$id'";
$result = $conn->query($sql);
// output data of each row
while($row = $result->fetch_assoc()) {
$dir = $row["dir"];
$likes = $row["likes"];
}
$sqlq = "UPDATE pics SET likes='$likes+1' WHERE id='$id'";
$conn->query($sqlq);
$conn->close();
But the like dose not add to the database.
If you echo your $sqlq out using
echo $sqlq;
you'll see that the '$likes+1' isn't doing what you expect.
You could really simplify it by doing
$sqlq = "UPDATE pics SET likes=likes+1 WHERE id='$id'";
which removes any risk of two users updating the database at teh same time overwriting each other.
But you should really check out using "parameterized queries" as that would solve all your problems (and may your queries safer). Check the examples in the manual http://php.net/manual/en/mysqli-stmt.bind-param.php

how to align the record in mysql using php?

I really want the data in my database to be in the same row.
My problem is that when I submit the start time the end time automatically becomes like this: 00:00:00
Here is my code:
<?php
//Connect and select a database
mysql_connect("localhost", "root" , "" )or die("cannot connect to database server");
mysql_select_db("dbdorm")or die("cannot select the database");
//if form is submitted
if(isset($_POST['submit']))
{
date_default_timezone_set('Asia/Manila');
$query = "INSERT INTO datetime (starttime) VALUES(NOW())";
$submit = mysql_query($query) or die("insertion error");
header("location:loggedin.php?");
}
else{
date_default_timezone_set('Asia/Manila');
$query = "INSERT INTO datetime (endtime) VALUES(NOW())";
$submit2 = mysql_query($query) or die("insertion error");
header("location:loggedin.php?");
}
?>

Mysqli Update not working properly

$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "UPDATE Cube SET xValue=15 WHERE Index=1";
mysqli_query($conn, $sql);
mysqli_close($conn);
This seems like it should be pretty straight forward, but for some reason, the xValue field won't change, and I get no errors whatsoever. Been trying this for too long.
as u_mulder said, index is a reserved word in mySQL so you got 2 options:
$sql = "UPDATE Cube SET xValue=15 WHERE Cube.Index=1";
or
$sql = "UPDATE Cube SET xValue=15 WHERE `Index`=1";

mysql insert error check

I have a PHP script that collects form data and inserts some of that data into a MySQL database. I just noticed that some inserts/records were NOT, or never created in the database. I would like to write a retry routine that if the insert fails to retry 3 times and then error out to the user.
Just so you can see my code for the DB and the insert so you can see that I am NOT nuts...
mysql_connect($hostname,$username, $password) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
Here is my insert code:
$query = "INSERT INTO contacts VALUES ('','$name','$address','$city','$state','$zip','$phone','$email_address','$arrive','$depart','$room','$found','$promocode','$message','$datetimestamp','$ip')";
mysql_query($query);
mysql_close();
I started out with an IF statement then into a loop but got lost.
#Jay:
So something like this:
$conn = new mysqli($hostname, $username, $password, $dbname);
// check connection
if (mysqli_connect_errno())
{
exit('Connect failed: '. mysqli_connect_error());
}
$query = "INSERT INTO contacts VALUES ('','$name','$address','$city','$state','$zip','$phone','$email_address','$arrive','$depart','$room','$found','$promocode','$message','$datetimestamp','$ip')";
// Performs the $query on the server to insert the values
if ($conn->query($query) === TRUE) {
//echo 'users entry saved successfully';
}
else {
echo 'Error: '. $conn->error;
}
$conn->close();
I am already checking for injection before the insert query
Your query is perfect, make sure that number of parameter you are passing in sql query is same as number of column in database table & parameter value in sql is same order of database table column order

PHP: mysqli_query is not working [duplicate]

This question already has answers here:
php/mysql with multiple queries
(3 answers)
Closed 3 years ago.
I've a doubt with mysqli_query..
this is a part of my code:
$con = db_connect();
$sql= "SET foreign_key_checks = 0; DELETE FROM users WHERE username = 'Hola';";
$result = mysqli_query($con, $sql);
return $result;
I can't do the query...
If I try to do a query like this:
$sql= "INSERT INTO categorias(id_categoria,name) VALUES ('15','ssss');";
It works.
What's the problem?? I can't use SET with mysqli_query?
Thanks
You can not execute multiple queries at once using mysqli_query but you might want to use mysqli_multi_query as you can find out in the official documentation:
http://www.php.net/manual/en/mysqli.multi-query.php
Lets start with creating a working php script.
<?php
// replace for you own.
$host ="";
$user = "";
$password = "";
$database = "";
$con= mysqli_connect($host, $user, $password, $database);
if (!$con)
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else{
// Begin SQL query
$sql = "SELECT * FROM users";
$result = mysqli_query($con,$sql) OR Die('SQL Query not possible!');
var_dump($result);
return $result;
var_dump($result);
// End SQL query
mysqli_close($con);
};
?>
INSERT query:
$sql= "INSERT INTO categorias(name) VALUES ('ssss')";
mysqli_query ($con,$sql) OR Die('SQL Query not possible!');
UPDATE and DELETE query:
$sql= "DELETE FROM users WHERE username = 'Hola';";
$sql.= "UPDATE users SET foreign_key_checks = 0 WHERE username = 'Hola'"; /* I made a guess here*/
mysqli_multi_query ($con,$sql) OR Die('SQL Query not possible!');
Check the SET query. I think something is missing. I have changed it to what I think was your aim.
The connection should be established like this:
$Hostname = "Your host name mostly it is ("localhost")";
$User = "Your Database user name default is (root)"//check this in configuration files
$Password = "Your database password default is ("")"//if you change it put the same other again check in config file
$DBName = "this your dataabse name"//that you use while making database
$con = new mysqli($Hostname, $User , $PasswordP , $DBName);
$sql= "INSERT INTO categorias(id_categoria,name) VALUES ('15','ssss');";
In this query:
put categorias in magic quotes(`) and column names also
For your next query do this:
$sql= "SET foreign_key_checks = 0; DELETE FROM users WHERE username = 'Hola';";
Change to:
$sql= "SET foreign_key_checks = 0; DELETE FROM `users` WHERE `username` = 'Hola'";

Categories