?
Solved

voting results to mysql database

Posted on 2011-02-14
9
Medium Priority
?
342 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
Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

 

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

650 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