Solved

voting results to mysql database

Posted on 2011-02-14
9
320 Views
Last Modified: 2012-05-11
i have this code based on a player:

$testFile = "test.txt";


if($_REQUEST['action']=="vote"){


	function writeTextFile2($theFileName, $theContent, $openCondition='w+'){
		if($theContent=="" || is_null($theContent)){
			$enterContent = " ";
		} else {
			$enterContent = $theContent;
		}
		$retval = TRUE;
		if (!$fp = fopen($theFileName, $openCondition)) {
			$retval = FALSE;
		}
		if($retval){
			if (!$filewrite = fwrite($fp, stripslashes($enterContent))) {
				$retval = FALSE;
				exit;
			} else {
				$retval = TRUE;
			}
		}
		@fclose($fp);
		return $retval;
		@chmod ($theFileName, 0777);
	}

	

	// NOTE: Some PHP installations may use a different nomenclature for the "$_REQUEST" such as "$_HTTP_SERVER"
	$theContent = "";
	
	// Here we are extracting all of the info contained in the request.

	foreach($_REQUEST as $key => $value){
		$theContent .= $key." : \t".urldecode($value)."\r\n";
	}

	strstr( PHP_OS, "WIN") ? $slash = "\\" : $slash = "/";
	
	//print "$testFile";

	if(writeTextFile2(getcwd ().$slash.$testFile, $theContent, 'w+')){
		print "&retval=ok";
	} else {
		print "&retval=error";
	}

	$dbc=mysql_connect('173.201.136.40','jschin','Js2759067') or die(mysql_error());
	mysql_query("jschin",$dbc);
	
}

the code returns the result of a user voting on a song and returns all of the variables of the song such as title, artist, filename to the text file.  What i am looking to do is take this information and scan a database and add a vote to a song based upon the information that is posted.  How would I do this using a php function?
0
Comment
Question by:jvsmooth
  • 3
  • 3
9 Comments
 
LVL 3

Expert Comment

by:wwwdeveloper2
ID: 34893704
1) does this code function as is?
2) What is the layout of your database tables?
0
 

Author Comment

by:jvsmooth
ID: 34894235
Yes the function works the layout is id , song_title, song_artist, genre, filepath    


In that order
0
 
LVL 3

Expert Comment

by:wwwdeveloper2
ID: 34896011
If you are wanting to record a vote, where are you going to store that?  Are you wanting to keep a count in table with a foreign key constraint to the table you mentioned above so it will grow vertically and you have an audit trail of the votes or do you want to just have a vote column in the table you mentioned and update it with a running total of votes?

What is your table name, so I can reference it by name?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:jvsmooth
ID: 34898179
I would rather have a vote column that is numerically increased by one when the user votes and the name of the table is music
0
 
LVL 3

Accepted Solution

by:
wwwdeveloper2 earned 500 total points
ID: 34903052
Okay, not sure if you have the song id or not.  Let's assume you do for now and it is 53.  Also let's say you have a column in your table that holds the vote counts called votes.



 
$songId=53;

<?php
$con = mysql_connect("host","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);
//we need to do a select to grab the current votes the song has
$result = mysql_query("SELECT votes FROM music WHERE id = '$songId'");

while($row = mysql_fetch_array($result))
  {
  $voteCnt =  $row['votes']; //grab the current vote count
  
  }

$voteCnt++; add 1 vote to the count

//now we need to insert the new vote total back into the db

mysql_query("INSERT INTO musc
(votes) VALUES('$voteCnt' ) where id='$songId' ") 
or die(mysql_error());  

//cloe your db connection
mysql_close($con);
?>

Open in new window



Is this making sense?  If you have follow-up questions or trouble, just let me know and I'll help in any way I can.

Good luck!
0
 

Author Comment

by:jvsmooth
ID: 34903167
so as it is the function creates a function called test.txt which has all of the information in it.  How do I catch this information?  Do I jus make a file called votecapture.php, which has the above function in it, and set that where the test.txt currently is? Or do i need to do something else.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34992351
@jvsmooth, here is how I would handle the voting.  Make a junction table that contains (at a minimum) two columns - the key of the song and the key of the user.  You could augment this table to include other interesting things like the DATETIME of the vote, or the IP address (can be used to determine the geographic location of the voter).  This table will be quite long, but if you have an index on the key columns you will have pretty good performance even if it goes into the hundreds of thousands of rows.

I would not have a vote column in the music table.  To understand why, you would need to understand all about data base normalization and that is a little too much for you to learn over a question at EE.

If you're still fairly new to PHP and data base driven web sites, this book will help you get a strong foundation.
http://www.sitepoint.com/books/phpmysql4/

Best regards, ~Ray
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now