Jump to content

Recommended Posts

Hey everyone,

 

this is my first post on the forum. I found it via google.

 

I've got some code now that a friend of mine shot me and below is the index.php file and then below that is the voted.php file. It's not passing any of the +1 to anything that they voted for.

 

index.php

<?php 
// Connects to your Database 
$link = mysql_connect('localhost', 'user', 'pass'); 
mysql_select_db("database") or die(mysql_error()); 

?>

<?php include('header.php'); ?>

<div class="contentwide">
<div class="contentwrap">
<form method="post" action="voted.php">
<?php $query = "SELECT id, name, url FROM photos ORDER BY RAND() LIMIT 3"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { ?>
<input type="hidden" name="id" value="<?php {echo "{$row['id']}"; } ?>">
     <div class="picturewrap">
	<img src="<?php {echo "{$row['url']}"; } ?>" alt="<?php {echo "{$row['name']}"; } ?>" />
	<div class="formwrapper">
<input type="radio" name="<?php {echo "images[{$row['id']}]";} ?>" value="1">
	</div>
	<div class="formwrapper">
<input type="radio" name="<?php {echo "images[{$row['id']}]";} ?>" value="2">
	</div>
	<div class="formwrapper">
<input type="radio" name="<?php {echo "images[{$row['id']}]";} ?>" value="3">
	</div>
     </div>
<?php } ?>
     <div class="submitform">
	<input type="image" src="images/button_submit.jpg" class="button_submit" value="Submit"><br/ >
     </div>
</form>
</div>
</div>
<?php mysql_close($link); ?>
<?php include('footer.php'); ?>

 

voted.php

<?php

$images = $_POST['images'];

// Loop over each item in the $images array.
foreach ($images as $id => $vote) {
$query = "SELECT {$id} FROM photos";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);

switch ($vote) {
     case '1':
         # Vote for a spoon
         $spoon_votes = $row['spoon_votes'] + 1;
         $update = "UPDATE photos SET spoon_votes={$spoon_votes} WHERE id={$row[id]}";
         mysql_query($query);
         break;
     case '2':
         # Vote for a fork
         $fork_votes = $row['fork_votes'] + 1;
         $update = "UPDATE photos SET fork_votes={$fork_votes} WHERE id={$row[id]}";
         mysql_query($query);
         break;
     case '3':
         # Vote for a knife
         $knife_votes = $row['knife_votes'] + 1;
         $update = "UPDATE photos SET knife_votes={$knife_votes} WHERE id={$row[id]}";
         mysql_query($query);
         break;

     default:
         die("Invalid value passed as a vote.");
     break;
}
}?>

 

I put a dump_var(); option in for $images and got the array and it looks like this:

 

Array ( [2] => fork [9] => spoon [11] => knife ) Array ( [2] => fork [9] => spoon [11] => knife ) Array ( [2] => fork [9] => spoon [11] => knife )

 

The numbers are for the picture ID from the database and the word is what they're voting for. For some reason though, its showing three times and also not passing any of the info properly (like I said above, whatever they vote for, it should be giving a +1 to the column inside the row).

 

Any thoughts/help would be much appreciated.

update you voted.php to

<?php
$images = (int)$_POST['images'];
if($images <1 || $images > 3) die("Invalid value passed as a vote.");
$update = "UPDATE photos SET votes=votes+1 WHERE id=$images";
mysql_query($query);
?>

 

thats the full script

 

EDIT wait a minute you have different field for votes ?

can you give an example of how your table is setup..

Yeah. theres 14 images total right now (there will be hundreds when I get it all set up) and they randomly pull three from the database for each voting cycle.

 

The xhtml/css is done for the design and I have the index.php page setup and also have the voted.php file set up. Not sure if thats what you're asking or not though when you said design stages?

I set up the three columns so each row can have tallies for each item (spoon fork knife) this way I can keep track of how many times overall these pics get each vote.

 

The page will show three random pics every time it's loaded and the same pic might be in a different set. (ie: first page shows id's 1,4,9 while the next page load shows 5,9,13) So I need to tally each of the times the spoon fork or knife is voted for, for that pic.

 

Make sense? sorry if it sounds like rambling. I'm truly new to php like this. I work with wordpress and other blogging platforms and know my way around them, but writing codes is something completely new to me.

i'm sorry i am reading the code and trying to workout what you are trying to do..

 

this is what i have so far

you have a form with 3 images (a radom 3 of 14)  and 9 radio buttons (3 each)

 

you want to count the votes for each image but why the 3 radio button per image ?

No problem. I appreciate all of the help. I truly do.

 

I need the three radio buttons because they need the option to choose from "spoon, fork, knife" for each image. They have the choice from those three, for each of the three images. It's similar to the www.fmarrykill.net site

untested

 

try this

<?php 
// Connects to your Database 
$link = mysql_connect('localhost', 'user', 'pass'); 
mysql_select_db("database") or die(mysql_error()); 
?>

<?php include('header.php'); ?>

<div class="contentwide">
<div class="contentwrap">
<form method="post" action="voted.php">
<?php
$query = "SELECT id, name, url FROM photos ORDER BY RAND() LIMIT 3"; 
$result = mysql_query($query); 
while($row = mysql_fetch_assoc($result))
{
?>
<input type="hidden" name="id[]" value="<?php echo $row['id']; ?>">
<div class="picturewrap">
	<img src="<?php echo $row['url']; ?>" alt="<?php echo $row['name']; ?>" />
	<div class="formwrapper">
		<input type="radio" name="images[<?php echo $row['id']; ?>]" value="spoon">
	</div>
	<div class="formwrapper">
		<input type="radio" name="images[<?php echo $row['id']; ?>]" value="folk">
	</div>
	<div class="formwrapper">
		<input type="radio" name="images[<?php echo $row['id']; ?>]" value="knife>">
	</div>
</div>
<?php
}
?>
<div class="submitform">
	<input type="image" src="images/button_submit.jpg" class="button_submit" value="Submit"><br/ >
</div>
</form>

</div>
</div>
<?php mysql_close($link); ?>
<?php include('footer.php'); ?>

 

 

<?php
$IDS = implode(",",array_keys($_POST['id']));

//security filters
$IDS = preg_replace('/[^,\d]/s', '', $IDS);
$valid = array("spoon","knife","fork");

$query = "SELECT id FROM photos IN ($IDS)";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
$images = mysql_real_escape_string($_POST['images'][$row['id']]);
if(!in_array($images,$valid)) die("ERROR");
$update = sprintf("UPDATE photos SET {$images}_votes={$images}_votes+1 WHERE id=%d LIMIT 1",$row['id']);
mysql_query($query);
}
?>

I added the error item to a different spot in the voted.php file and this is the error it gave me.

 

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 'IN (0,1,2)' at line 1

My php for voted.php is now set up with the following. The page loads, but shows a blank screen. I check the database and it's not passing through properly. Not sure what is going wrong? I changed the IN ($IDS) to WHERE id='($IDS)' ... could that be why?

 

<?php

$IDS = implode(",",array_keys($_POST['id']));

//security filters
$IDS = preg_replace('/[^,\d]/s', '', $IDS);
$valid = array("spoon","knife","fork");

$query = "SELECT id FROM photos WHERE id='($IDS)'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$images = mysql_real_escape_string($_POST['images'][$row['id']]);
if(!in_array($images,$valid)) die("ERROR");
$update = sprintf("UPDATE photos SET {$images}_votes={$images}_votes+1 WHERE id=%d LIMIT 1",$row['id']);
mysql_query($query) or die(mysql_error());
}
?>

 

When I did get the die(mysql_error) to work, it gave me this error (this is before I changed the IN to WHERE.

 

Operand should contain 1 column(s)

 

I added the following code to the bottom of the file so I could get the var_dump for a couple variables.

 

var_dump($IDS);

var_dump($valid);

 

 

 

And this is the code that it spits out.

 

string(5) "0,1,2"
array(3) { [0]=> string(5) "spoon" [1]=> string(5) "knife" [2]=> string(4) "fork" }

Sorry for the multiple posts. It wont let me edit the other ones anymore.

 

When I did a var_dump for $IDS, it says 0,1,2 .... those are not the image id values that are showing when I load the index.php file. Could that be whats causing the problem? something with that?

Sorry you took awhile to responde and i needed sleep

okay.. it looks like you added some extra quote (also your line 15 isn't my line 15

change

$query = "SELECT id FROM photos WHERE id='($IDS)'";

to

$query = "SELECT id FROM photos WHERE id=($IDS)";

Sure thing.

 

--

-- Table structure for table `photos`

--

 

CREATE TABLE IF NOT EXISTS `photos` (

  `id` text COLLATE utf8_unicode_ci NOT NULL,

  `name` text COLLATE utf8_unicode_ci NOT NULL,

  `url` text COLLATE utf8_unicode_ci NOT NULL,

  `category` text COLLATE utf8_unicode_ci NOT NULL,

  `spoon` text COLLATE utf8_unicode_ci NOT NULL,

  `fork` text COLLATE utf8_unicode_ci NOT NULL,

  `knife` text COLLATE utf8_unicode_ci NOT NULL

) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

 

--

-- Dumping data for table `photos`

--

 

INSERT INTO `photos` (`id`, `name`, `url`, `category`, `spoon`, `fork`, `knife`) VALUES

('1', 'Cristal Vang', 'http://www.4amcreative.com/clients/spoonforkknife/phpcodes/images/pictures/cristal-vang.jpg', 'Girls', '', '', '1'),

('2', 'Woman One', 'http://www.4amcreative.com/clients/spoonforkknife/phpcodes/images/pictures/woman1.jpg', 'Girls', '', '', '1'),

('3', 'Woman Two', 'http://www.4amcreative.com/clients/spoonforkknife/phpcodes/images/pictures/woman2.jpg', 'Girls', '', '', '1'),

('4', 'Woman Three', 'http://www.4amcreative.com/clients/spoonforkknife/phpcodes/images/pictures/woman3.jpg', 'Girls', '', '', '1'),

('5', 'Woman Four', 'http://www.4amcreative.com/clients/spoonforkknife/phpcodes/images/pictures/woman4.jpg', 'Girls', '', '', '1'),

('6', 'Woman Five', 'http://www.4amcreative.com/clients/spoonforkknife/phpcodes/images/pictures/woman5.jpg', 'Girls', '', '', '1'),

('7', 'Woman Six', 'http://www.4amcreative.com/clients/spoonforkknife/phpcodes/images/pictures/woman6.jpg', 'Girls', '', '', '1'),

('8', 'Woman Seven', 'http://www.4amcreative.com/clients/spoonforkknife/phpcodes/images/pictures/woman7.jpg', 'Girls', '', '', '1'),

('9', 'Woman Eight', 'http://www.4amcreative.com/clients/spoonforkknife/phpcodes/images/pictures/woman8.jpg', 'Girls', '', '', '1'),

('10', 'Woman Nine', 'http://www.4amcreative.com/clients/spoonforkknife/phpcodes/images/pictures/woman9.jpg', 'Girls', '', '', '1'),

('11', 'Woman Ten', 'http://www.4amcreative.com/clients/spoonforkknife/phpcodes/images/pictures/woman10.jpg', 'Girls', '', '', '1'),

('12', 'Woman Eleven', 'http://www.4amcreative.com/clients/spoonforkknife/phpcodes/images/pictures/woman11.jpg', 'Girls', '', '', '1'),

('13', 'Woman Twelve', 'http://www.4amcreative.com/clients/spoonforkknife/phpcodes/images/pictures/woman12.jpg', 'Girls', '', '', '1'),

('14', 'Woman Thirteen', 'http://www.4amcreative.com/clients/spoonforkknife/phpcodes/images/pictures/woman13.jpg', 'Girls', '1', '1', '3');

 

 

Just thought I'd post to let you know I figured it out. I had another buddy of mine show me some code and I edited it to work with what I needed. THANK YOU for all of your help. I truly appreciate it and hope that this topic helps someone else who's having a similar issue.

 

<?php 

// Connects to your Database 
$link = mysql_connect('localhost', 'myuser_bootstrap', '18766622'); 
mysql_select_db("myuser_sfk") or die(mysql_error()); 

if ( !empty( $_POST['images'] ) ) { 
    $images = $_POST['images']; 
    // Loop over each item in the $images array. 
    foreach ( $images as $id => $vote ) { 
        if ( $vote == 1 ) { 
            $update = sprintf( "UPDATE photos SET spoon=spoon+1 WHERE id=$id", $vote , $id ); 
            mysql_query( $update ) OR DIE( "error in query" ); 
        } elseif ( $vote == 2 ) { 
            $update = sprintf( "UPDATE photos SET fork=fork+1 WHERE id=$id", $vote , $id ); 
            mysql_query( $update ) OR DIE( "error in query" ); 
        } elseif ( $vote == 3 ) { 
            $update = sprintf( "UPDATE photos SET knife=knife+1 WHERE id=$id", $vote , $id ); 
            mysql_query( $update ) OR DIE( "error in query" ); 
        } else 
            die( "Invalid value passed as a vote." ); 
    } 
} 

?>

Well heres mine version,

 

<?php #include('header.php'); ?>

<div class="contentwide">
<div class="contentwrap">
<form method="post" action="voted.php">
<?php
$query = "SELECT id, name, url FROM photos ORDER BY RAND() LIMIT 3"; 
$result = mysql_query($query); 
while($row = mysql_fetch_assoc($result))
{
?>
   <div class="picturewrap">
      <img src="<?php echo $row['url']; ?>" alt="<?php echo $row['name']; ?>" />
      <div class="formwrapper">
         <input type="radio" name="images[<?php echo $row['id']; ?>]" value="spoon">
      </div>
      <div class="formwrapper">
         <input type="radio" name="images[<?php echo $row['id']; ?>]" value="fork">
      </div>
      <div class="formwrapper">
         <input type="radio" name="images[<?php echo $row['id']; ?>]" value="knife">
      </div>
   </div>
<?php
}
?>
   <div class="submitform">
      <input type="image" src="images/button_submit.jpg" class="button_submit" value="Submit"><br/ >
   </div>
</form>

</div>
</div>
<?php mysql_close($link); ?>
<?php #include('footer.php'); ?>

 

<?php 
// Connects to your Database 
$link = mysql_connect('localhost', '', ''); 
mysql_select_db("") or die(mysql_error()); 
?>

<?php
$IDS = implode(",",array_keys($_POST['images']));

//security filters
$IDS = preg_replace('/[^,\d]/s', '', $IDS);
$valid = array("spoon","knife","fork");

$query = "SELECT id FROM photos WHERE ID IN ($IDS)";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
   $images = mysql_real_escape_string($_POST['images'][$row['id']]);
   if(!in_array($images,$valid)) die("ERROR");
   $update = sprintf("UPDATE photos SET {$images}={$images}+1 WHERE id=%d LIMIT 1",$row['id']);
   mysql_query($update) or die(mysql_error());
}
?>

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.