?
Solved

voting results to mysql database

Posted on 2011-02-14
9
Medium Priority
?
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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 111

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
In this article, we’ll look at how to deploy ProxySQL.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

800 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