silverglade Posted February 14, 2010 Share Posted February 14, 2010 hi, i have user input for voting for a favorite actor, and the problem is, when people vote for an actor, they could either type Dustin Hoffman, or dustin hoffman, which will make 2 votes for 2 different people. is there a way to make it 2 votes for Dustin Hoffman, or 2 votes for dustin hoffman? i dont know how to get it to vote for 1 person regardless of capitalization. or for a color, like blue-green or Blue-Green. any help greatly appreciated. thanks. derek here is the code for the page <?php // connect to database include file //include("connect2.php"); include("connect2.php"); $checkIp = TRUE; // Set to tru to enable ip checking ini_set('display_errors', 1); error_reporting(E_ALL); // escape username and password for use in SQL//person said on board "looks fine" like this //to prevent sql injections function post($fieldname = '') { $data = ''; if($fieldname!='' AND isset($_POST[$fieldname])) $data = $_POST[$fieldname]; return $data; } if($_POST) { $actor = post('actor'); $actor = strip_tags(mysql_real_escape_string($actor)); } if(isset($_POST['Submit']) AND isset($_POST['vote'])) { $Check = mysql_query("SELECT * FROM actor_voters WHERE IP='".$_SERVER['REMOTE_ADDR']."'"); $num = mysql_num_rows($Check); if(!$checkIp) $num = 0; if($num < 1) // counts if the ip is less than 1, if it is more than 1 it means that someone already voted. { echo (" You have voted for ".$actor. " <br /><br />"); $check1 = mysql_query("SELECT vote FROM actors WHERE `actor`='{$actor}' "); $check1_rows = mysql_num_rows($check1); if($check1_rows > 0) { $row = mysql_fetch_array($check1); $vote = $row['vote'] + 1; mysql_query("UPDATE actors SET vote='$vote' WHERE `actor`='{$actor}' "); } else { $sql = "INSERT INTO actors (`actor`,`vote`) VALUES('{$actor}' ,'1')"; mysql_query($sql); } if($checkIp) mysql_query("INSERT INTO actor_voters (ip) VALUES('{$_SERVER['REMOTE_ADDR']}')"); } else { echo "sorry, you can just vote once."; } } if($_POST) { $query="SELECT * FROM actors ORDER BY vote DESC LIMIT 10"; $result=mysql_query($query); if(mysql_num_rows($result) > 0) { echo "Top ten actors :<br />"; echo '<table border="1"> <tr> <td>Actor</td> <td>Vote</td> </tr> '; while($row = mysql_fetch_array($result)) { echo '<tr> <td>'.$row['actor'].' </td> <td>'.$row['vote'].' </td> </tr> '; } echo '</table>'; } else { echo "No actors in the database"; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Actors </title> </head> <body> </p> <div align="center">welcome to <strong>vote for your favorite actor</strong> </div> <p>Please enter your vote your favorite actor.<span class="style4"></span><br /> </p> <form id="form1" name="form1" method="post" action=""> <table border="0"> <tr> <td>Actor</td> <td> <input name="actor" type="text" id="actor" /></td> </tr> <tr> <td> </td> <td> <input type="submit" name="Submit" value="Submit" /> <input type="hidden" name="vote" value="TRUE" /> </td> </tr> </table> </form> <form id="form2" name="form2" method="post" action=""> <table width="666" border="1"> <tr> <td width="42"> </td> <td width="608"><div align="center">VIEW THE UPDATED TOP TEN ACTORS</div></td> </tr> <tr> <td> </td> <td><input type="submit" name="submit" id="submit" value="submit" /></td> </tr> </table> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/192025-database-user-input-capitalization-problem/ Share on other sites More sharing options...
jl5501 Posted February 14, 2010 Share Posted February 14, 2010 change this $check1 = mysql_query("SELECT vote FROM actors WHERE `actor`='{$actor}' "); to this $check1 = mysql_query("SELECT vote FROM actors WHERE `actor`='strtolower($actor)' "); Link to comment https://forums.phpfreaks.com/topic/192025-database-user-input-capitalization-problem/#findComment-1012065 Share on other sites More sharing options...
silverglade Posted February 14, 2010 Author Share Posted February 14, 2010 awesome thanks for that fast reply jl5501! thats great thank you. derek Link to comment https://forums.phpfreaks.com/topic/192025-database-user-input-capitalization-problem/#findComment-1012066 Share on other sites More sharing options...
silverglade Posted February 14, 2010 Author Share Posted February 14, 2010 it doesnt work. it still separates Dustin Hoffman, from dustin hoffman. now when i type in Dustin Hoffman more than once, it creates a separate entry instead of increasing the vote number. with the following code <?php // connect to database include file //include("connect2.php"); include("connect2.php"); $checkIp = FALSE; // Set to tru to enable ip checking ini_set('display_errors', 1); error_reporting(E_ALL); // escape username and password for use in SQL//person said on board "looks fine" like this //to prevent sql injections function post($fieldname = '') { $data = ''; if($fieldname!='' AND isset($_POST[$fieldname])) $data = $_POST[$fieldname]; return $data; } if($_POST) { $actor = post('actor'); $actor = strip_tags(mysql_real_escape_string($actor)); } if(isset($_POST['Submit']) AND isset($_POST['vote'])) { $Check = mysql_query("SELECT * FROM actor_voters WHERE IP='".$_SERVER['REMOTE_ADDR']."'"); $num = mysql_num_rows($Check); if(!$checkIp) $num = 0; if($num < 1) // counts if the ip is less than 1, if it is more than 1 it means that someone already voted. { echo (" You have voted for ".$actor. " <br /><br />"); $check1 = mysql_query("SELECT vote FROM actors WHERE `actor`='strtolower($actor)'"); //='{$actor}' $check1_rows = mysql_num_rows($check1); if($check1_rows > 0) { $row = mysql_fetch_array($check1); $vote = $row['vote'] + 1; mysql_query("UPDATE actors SET vote='$vote' WHERE `actor`='{$actor}' "); } else { $sql = "INSERT INTO actors (`actor`,`vote`) VALUES('{$actor}' ,'1')"; mysql_query($sql); } if($checkIp) mysql_query("INSERT INTO actor_voters (ip) VALUES('{$_SERVER['REMOTE_ADDR']}')"); } else { echo "sorry, you can just vote once."; } } if($_POST) { $query="SELECT * FROM actors ORDER BY vote DESC LIMIT 10"; $result=mysql_query($query); if(mysql_num_rows($result) > 0) { echo "Top ten actors :<br />"; echo '<table border="1"> <tr> <td>Actor</td> <td>Vote</td> </tr> '; while($row = mysql_fetch_array($result)) { echo '<tr> <td>'.$row['actor'].' </td> <td>'.$row['vote'].' </td> </tr> '; } echo '</table>'; } else { echo "No actors in the database"; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Actors </title> </head> <body> </p> <div align="center">welcome to <strong>vote for your favorite actor</strong> </div> <p>Please enter your vote your favorite actor.<span class="style4"></span><br /> </p> <form id="form1" name="form1" method="post" action=""> <table border="0"> <tr> <td>Actor</td> <td> <input name="actor" type="text" id="actor" /></td> </tr> <tr> <td> </td> <td> <input type="submit" name="Submit" value="Submit" /> <input type="hidden" name="vote" value="TRUE" /> </td> </tr> </table> </form> <form id="form2" name="form2" method="post" action=""> <table width="666" border="1"> <tr> <td width="42"> </td> <td width="608"><div align="center">VIEW THE UPDATED TOP TEN ACTORS</div></td> </tr> <tr> <td> </td> <td><input type="submit" name="submit" id="submit" value="submit" /></td> </tr> </table> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/192025-database-user-input-capitalization-problem/#findComment-1012068 Share on other sites More sharing options...
jl5501 Posted February 14, 2010 Share Posted February 14, 2010 you may have to do $actor=strtolower($actor) outside the query. Also, it is worth echoing the actual query after you have set it up to see exactly what it is checking. echo $check1; Link to comment https://forums.phpfreaks.com/topic/192025-database-user-input-capitalization-problem/#findComment-1012086 Share on other sites More sharing options...
silverglade Posted February 14, 2010 Author Share Posted February 14, 2010 thanks for looking and helping. i managed to fix it for lowercase and uppercase both. by using this code if($num < 1) // counts if the ip is less than 1, if it is more than 1 it means that someone already voted. { ucwords($wine); echo (" You have voted for ".$wine. " <br /><br />"); Link to comment https://forums.phpfreaks.com/topic/192025-database-user-input-capitalization-problem/#findComment-1012100 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.