voting results to mysql database

i have this code based on a player:

$testFile = "test.txt";


	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 (!$filewrite = fwrite($fp, stripslashes($enterContent))) {
				$retval = FALSE;
			} else {
				$retval = TRUE;
		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('','jschin','Js2759067') or die(mysql_error());

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?
Who is Participating?
wwwdeveloper2Connect With a Mentor Commented:
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.


$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

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!
1) does this code function as is?
2) What is the layout of your database tables?
jvsmoothAuthor Commented:
Yes the function works the layout is id , song_title, song_artist, genre, filepath    

In that order
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to and use offer code ‘EXPERTS’ to get 10% off your first purchase.

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?
jvsmoothAuthor Commented:
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
jvsmoothAuthor Commented:
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.
Ray PaseurCommented:
@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.

Best regards, ~Ray
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.