Solved

voting results to mysql database

Posted on 2011-02-14
9
327 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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 110

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

695 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