I'm new to MySQL. Whats wrong with this code? It doesn't update data.
"INSERT INTO highscores (name, score, maila, ip)" . "VALUES ('$name', '$score', '$maila', '$ip')" .
"ON DUPLICATE KEY UPDATE score;" . "UPDATE highscores SET (if score>'$score') {score=$score} WHERE name=$name"
This works:
"INSERT INTO highscores (name, score, maila, ip) ".
"VALUES ('$name', '$score', '$maila', '$ip') " . "
on duplicate key update score = greatest(score, $score)"
Thanks to binaryLV:
MSQL: How to overwrite entry only if new one is higher? else create new entry
When you use ON DUPLICATE KEY UPDATE, you must also specify what to update, that is, in PHP, the right query would be:
$q = "INSERT INTO highscores (name, score, maila, ip) ".
"VALUES ('$name', '$score', '$maila', '$ip') ".
"ON DUPLICATE KEY UPDATE score='$score'";
Related
I am really new to php and I am trying to use simple insert to my mysql database from the form.
I know that this mysql connection/insertion is dangerous and not used anymore. so can anyone please help me with this simple thing? I tried to google, but nothing is working so far :/
<?
$text=$_POST['name'];
$text=$_POST['surename'];
mysql_connect("localhost", "db_name", "pass") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());
$result = mysql_query("INSERT INTO `table` (name, surename)
VALUES (NOW(), '".mysql_real_escape_string($name)."', '".mysql_real_escape_string($surename)."')");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
?>
Maybe change
$text=$_POST['name'];
$text=$_POST['surename'];
to
$name = $_POST['name'];
$surename = $_POST['surename'];
PS: And also your column names don't match your values. Your query, after inserting params
"INSERT INTO `table` (name, surename) VALUES (NOW(), '".mysql_real_escape_string($name)."', '".mysql_real_escape_string($surename)."')"
will probably look like this
INSERT INTO `table` (name, surename) VALUES (NOW(), 'Jhon', 'Wick')
As you can see there's name, surename (which probably should be surname) and (NOW(), 'Jhon', 'Wick'). So either add a column (if you have that column in your database):
INSERT INTO `table` (created_at, name, surename) VALUES (NOW(), 'Jhon', 'Wick')
or remove NOW() from your values
INSERT INTO `table` (name, surename) VALUES ('Jhon', 'Wick')
This question already has answers here:
Why can't I run two mysqli queries? The second one fails [duplicate]
(2 answers)
Closed 5 years ago.
I have a very specific problem and nothing I could find online was able to tell me where my error was.
I want to pass two mysql queries at once. Separately, they work perfectly but together they fail. I've tries JOIN, adding ; and the multi_queries method. Everything fails.
Now I am stuck with this code:
// data insertion
$sql = "INSERT INTO comments (id, name, email, comment, article_id, date) VALUES ('$id', '$name', '$email', '$comment', '$article_id', '$date')";
$sql.= "DELETE FROM comments_validation WHERE id = $id";
if ($conn->multi_query($sql) === TRUE) {
header('Location: http://url.com/index.php?success');
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
And the error:
Error: INSERT INTO comments (id, name, email, comment, article_id, date) VALUES ('some values')DELETE FROM comments_validation WHERE id = 'some other value'
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 'DELETE FROM comments_validation WHERE id = 'some other value' at line 1
Thanks in advance!
You have to add a ; at the end of this sql statement
$sql = "INSERT INTO comments (id, name, email, comment, article_id, date) VALUES ('$id', '$name', '$email', '$comment', '$article_id', '$date');";
^here
Please add semi-colon as string at the end of every query in multi query.
// data insertion
$sql = "INSERT INTO comments (id, name, email, comment, article_id, date) VALUES ('$id', '$name', '$email', '$comment', '$article_id', '$date');";
$sql.= "DELETE FROM comments_validation WHERE id = $id";
if ($conn->multi_query($sql) === TRUE) {
header('Location: http://url.com/index.php?success');
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
I getting error "Query error!" with this code:
$result = $connection->query("INSERT INTO EMP_TBLE (NAME, AGE, CATEGORY, UPDATE_COUNT) VALUES('$NAME', '$AGE', $CAT, 1
ON DUPLICATE KEY UPDATE UPDATE_COUNT = UPDATE_COUNT + 1"));
echo "<br>".$result;die;
I also check different example like this example was running success but I found error in above code for my demo project.
The closing brace for VALUES is missing. There is also a non-needed brace at the end of your code line.
Please change
$result = $connection->query("INSERT INTO EMP_TBLE (NAME, AGE, CATEGORY, UPDATE_COUNT) VALUES('$NAME', '$AGE', $CAT, 1 ON DUPLICATE KEY UPDATE UPDATE_COUNT = UPDATE_COUNT + 1"));
to
$result = $connection->query("INSERT INTO EMP_TBLE (NAME, AGE, CATEGORY, UPDATE_COUNT) VALUES('$NAME', '$AGE', $CAT, 1) ON DUPLICATE KEY UPDATE UPDATE_COUNT = UPDATE_COUNT + 1");
Apparently you did not close the parentesis in the right place:
$result = $connection->query("INSERT INTO EMP_TBLE (NAME, AGE, CATEGORY,
UPDATE_COUNT) VALUES('$NAME', '$AGE', $CAT, 1)
ON DUPLICATE KEY UPDATE UPDATE_COUNT = UPDATE_COUNT + 1");
Also... you should at least escape all the variables before concatenating in queries like this to avoid SQL injection
I am using a simple php script to insert data into database but it's failing. The query just doesn't become successful without showing a single error which is why I am unable to figure out the problem. Some expert here help me please.
echo $name." ".$email." ".$pass." ".$phone." ".$area." ".$specialization." ".$city." ".$latitude." ".$longitude;
The result of echo is normal - without any null elements.
$query = mysqli_query($conn, "INSERT INTO users (name, email, pass, phone, area, specialization, hospital, city, latitude, longitude)
VALUES ('$name', '$email', '$pass', '$phone', '$area', '$specialization', '$hospital', '$city', '$latitude', '$longitude') ");
if ($query) {
echo "Status: Registeration Successful!";
// creating directory for user and storing dummy profile picture
//mkdir('../profiles/'.$email_trim, 0777);
//$result_copy = copy("img/dp.jpg.jpg", "../profiles/".$email_trim."/dp.jpg.jpg");
} else {
echo "Status: Err";
}
This "Status: Err" is always printed. I don't know why.
P.S I have double checked the database the field labels are fine.
UPDATE 1:
I added the
die(mysqli_error($conn));
statement and it says "DUPLICATE ENTRY '0' FOR KEY PRIMARY'.
PROBLEM AND SOLUTION:
The issue was that I had an 'id' field which was primary key of the table but it was not set to AUTO_INCREMENT. So, whenever I tried to insert a new record, I was actually inserting entries with duplicate PKs which was the issue. I change it to AUTO_INCREMENT and it solved the problem.
It seems you try to insert a new element with a PK = 0, but there is already a record with this key !
What is the primary key of your table ? Do you use an "id" field which is not shown in your insert statement ? Is this field AUTO_INCREMENT ?
It would be helpful to see the structure of your 'users' table.
Wild guess: looks like you may have defined an "id" column (or with whatever other name) which is primary key with default value "0", but it's not auto increment. That way you can insert 1 row and it will get "0" as "id" column's value, but you cannot insert another row because it will also try to use default value "0", which cannot happen as primary key has to be unique.
If that is the case, then please alter users table and make sure that the primary key column is also 'auto increment'.
Please check the proper error by adding below mentioned code inside else:
echo mysqli_errno($conn) . '----' . mysqli_error($conn);
<?php
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
$query = mysqli_query($conn, "INSERT INTO `users` (`name`, `email`, `pass`, `phone`, `area`, `specialization`, `hospital`, `city`, `latitude`, `longitude`)
VALUES ('$name', '$email', '$pass', '$phone', '$area', '$specialization', '$hospital', '$city', '$latitude', '$longitude') ") or die(mysqli_error());
$query = mysqli_query($conn, "INSERT INTO `users` (`name`, `email`, `pass`, `phone`, `area`, `specialization`, `hospital`, `city`, `latitude`, `longitude`)
VALUES ('$name', '$email', '$pass', '$phone', '$area', '$specialization', '$hospital', '$city', '$latitude', '$longitude') ");
use ` Tick maybe because there's some reserved word in your fields.
I think you doubled your close parenthesis and do not put $conn inside the query..
$query = "INSERT INTO users (name, email, pass, phone, area, specialization, hospital, city, latitude, longitude)
VALUES ('$name', '$email', '$pass', '$phone', '$area', '$specialization', '$hospital', '$city', '$latitude', '$longitude')";
mysqli_query($query, $conn);
Something like this. I hope this helps
I have the following sql statement:
$sql = "INSERT INTO jos_vm_product_details (variant_id, global_variant_id,
code, size, details, color, top, price)
VALUES ('$variant_id', '$global_variant_id', '$code', '$size',
'$details', '$color', '$top', '$price')";
This is just a basic INSERT sql. Is there a way for it to Update a record if the 'variant_id' is already present. If the 'variant_id' is not already there it should Insert a new one.
Any help would be appreciated
You can check INSERT ... ON DUPLICATE KEY UPDATE
$sql = "INSERT INTO jos_vm_product_details (variant_id, global_variant_id,
code, size, details, color, top, price)
VALUES ('$variant_id', '$global_variant_id', '$code', '$size',
'$details', '$color', '$top', '$price')
ON DUPLICATE KEY UPDATE
global_variant_id = VALUES(global_variant_id),
code = VALUES(code),
size = VALUES(size),
details = VALUES(details),
color = VALUES(color),
top = VALUES(top),
price = VALUES(price)";
In MySQL you can use ON DUPLICATE KEY
INSERT INTO jos_vm_product_details (variant_id, global_variant_id,
code, size, details, color, top, price)
VALUES ('$variant_id', '$global_variant_id', '$code', '$size',
'$details', '$color', '$top', '$price')";
ON DUPLICATE KEY UPDATE `code`=VALUES(`code`);
In T-SQL (2008 an higher) you could use MERGE