Hi guys, Happy New Year!

I'm trying to setup a voting site where the button under the contest entry just says "Vote For Me" and my form will collect the data from that button and store it into MySQL. I'm having trouble naming that image/vote button so the form will pick it up and put it into the DB as a collected vote.

I'm using this to no result:

<form action="db_update.php" method="post">
//image to vote on
<input type="image" name="<?php $_POST["person1"]; ?>" value="submit" id="person1" src="<?php echo $img; ?>" alt="<?php echo $alt; ?>" />

(I have variable images whether the person is registered or not)

Then my bd_update.php file reads partially like this:

$conn = //DB Connection

$name = ($_POST['name']);
$Username = ($_POST['Username']);

$conn = "UPDATE images SET vote = '$name'  where 'Username' = '$Username')";

As always, your help is invaluable!

Recommended Answers

All 8 Replies

From what I can see, "name" isn't recognised (I might be wrong)..

Your input needs to have a name, and the value needs to be what you process, for example:

<input type="image" name="myImage" value="Phorce">

And then in PHP:

 $name = $_POST['myImage'];
 var_dump($name); // Should display "Phorce"

Hope this helps, might be wrong.. Haven't ran the code :)

Phorce, you are here to save me again (this is the same project I was working on the other day).

So I did the var_dump and it printed NULL. Could my SQL command line be incorrect? I'm thinking it's displaying the data in the 'vote' field, which is still nothing.

$conn = "UPDATE images SET vote = '$name'  where 'Username' = '$Username')";


Your SQL statement should read:

$conn = "UPDATE images SET vote = '$name'  where 'Username' = '$Username'";

And no, you're var_dumping the variable and not the query.. The variable is null because "name" isn't declared

<input type="image" name="vote" value="person1" src="<?php echo $img; ?>" alt="<?php echo $alt; ?>" />
$conn = //DB Connection

$name = $_POST['vote'];
$Username = ($_POST['Username']);

$conn = "UPDATE images SET vote = '$name'  WHERE Username = '$Username'";

Sorry.. My server is down and can't run it but try that :)

This is weird. It's dumping the correct output in Safari, but not FF. Thoughts?

I don't think it's supported in FireFox.. ?

Will your voting be dynamic, in that, the images will change? Why not just have radio buttons or have two images that have their own ID and then increment it that way?

It's basically going to be 6 images on one page that will never change; the voter can register, click on the vote button for the one he/she likes, and that's it. Thank you for voting. Have a nice day.

The design I was given has the "Vote" button and it does look nice, so I don't think they will like the radio button. I don't even need to tally everything until I do the data dump then just count how many people voted for "person1" or "person2" etc.

Right can you try this then:

<button name="vote" type="submit" value="Person1"><img src="<?php echo $img; ?>"></button>

Different to <input type="image"> and should process..

=) -- updated: wrong input xD

Okay.. So full code:

	// This is the form have an array of all the people
	$people = array (
				'Phillip' => 'picture.png', 
				'James' => 'picture1.png',
				'Harry' => 'picture2.png',
				'Justin' => 'picture3.png',
				'Mark' => 'picture4.png',
				'Sally' => 'picture5.png');
	echo '<form method="post" action="theForm.php">';
	foreach($people as $person => $key)
		echo '<button name="vote" type="submit" value="' .$person. '"><img src="' .$key. '"></button><br />';
	echo '</form>';


and the php:


	$name = (string) $_POST['vote'];
	$vote = "UPDATE images SET vote=vote+1 WHERE Username='$name'";
	$voteRes = mysql_query($vote);
	if(mysql_affected_rows() == 1) { // only 1 row should be affected
		echo 'Vote has been submitted =)';
	  echo 'There is something wrong :(';


Obviously, include your sql connection - I haven't tested cos I don't have the table but looks right :) Let me know how you get on!

commented: Most helpful person over here! +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.