Jump to content

On duplicate?


Streakerstyle

Recommended Posts

Hello,

 

I'm quite new to mysql and putting things in the database through php,

but i've managed to get quite far with my script.

It can connect with the database,

get to the right table and write the given username in it and set the "votes" to 1.

 

The only problem i encountered is:

$sql="INSERT INTO `players` (`name`, `votes`) VALUES ('".$voter."', '1')
ON DUPLICATE KEY UPDATE `votes`=votes+1";

 

The ON DUPLICATE part of the code isn't working.

And i can't figure out why.

So if someone here can help me, that would be fantastic!

 

What i want to do is:

if given username doesn't exist in the database put him in with "votes" set to 1

if username already exists in the database add +1 to "votes"

 

The full code:

<?
$voter = $_GET['username'];
$conn = new MySQLi("localhost", "username", "password", "votetest");
if (mysqli_connect_errno()) {
exit('connect failed: '. mysqli_connect_error());
}
"INSERT INTO `players` (`name`, `votes`) VALUES ('".$voter."', '1')
ON DUPLICATE `name` UPDATE `votes`=votes+1";
echo "added";
?>

 

NOTE:

don't mind my grammar, i'm dutch.

Edited by Streakerstyle
Link to comment
Share on other sites

I've set the mysqli_query() to execute the query,

and changed the syntax from name to KEY again.

 

mysqli_query("INSERT INTO `players` (`name`, `votes`) VALUES ('".$voter."', '1') 
 ON DUPLICATE KEY UPDATE `votes`=votes+1");

 

But now it is giving me the warning:

Warning: mysqli_query() expects at least 2 parameters, 1 given in ... on line 16

 

line 16 is empty, and under the newly set on duplicate code.

So what parameter am i missing in this case?

 

and a quick question about that on duplicate,

will it add the +1 to all the votes or just to the vote for the given username?

Link to comment
Share on other sites

i was reading it and spotted the error.

had to put the $conn from the first line before the input.

 

But still it isn't working how it should,

it will now create a new line even if name exists.

And won't add 1 to the "votes" column if the name already exists.

 

mysqli_query($conn, "INSERT INTO `players` (`name`, `votes`) VALUES ('".$voter."', '1')
ON DUPLICATE KEY UPDATE `votes`=votes+1");

 

so there will probably be still a mistake in the ON DUPLICATE line of the code.

can it be: `votes`=votes+1? because in the database there won't be "votes" but 1 in this case.

 

---

The database after executing the edited php file:

qd6dxjvrc.51.22.png

Edited by Streakerstyle
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.