BadGoat Posted December 5, 2006 Share Posted December 5, 2006 Hello! I am trying to get a small, simple script to work, starting easy, with a yes or no option vote. I much prefer to use MySQL to store the votes. The script as it currently stands, does not insert the vote into the db. There are no errors, not sure what I am doing wrong. code:[code]<html><title>Vote!</title><head></head><body><form method="post" action="">Please Vote:<br />Yes.<input type="radio" name="vote" value="yes"><br />No.<input type="radio" name="vote" value="no"><br /><input type="submit" value="Vote" name="submit"></form></body></html><?phperror_reporting(E_ALL);$connect= mysql_connect("*****", "*****") or die ("Could not connect to database!");$result= mysql_select_db("*****") or die("Could not select that database !");if (isset($_POST['vote'])) { $vote = $_POST['vote']; switch ($vote) { case 'yes': $query = 'UPDATE vote SET yes = yes + 1'; break; case 'no': $query = 'UPDATE vote SET no = no + 1'; break; default: print 'You didn\'t Vote'; exit(); } $result = mysql_query($query); if (!$result) { die('Query failed.'.$query.mysql_error()); } else{ printf('Thank you for Voting'); } $query = 'SELECT * FROM vote'; $result = mysql_query($query); if (!$result) { die('Query failed.'.$query.mysql_error()); } else{ $row = mysql_fetch_object($result); $total = $row->yes+$row->no; print 'Total votes recieved '.$total.;[/code] Link to comment https://forums.phpfreaks.com/topic/29523-problem-with-simple-vote-script/ Share on other sites More sharing options...
craygo Posted December 5, 2006 Share Posted December 5, 2006 You cannot update something that is not there. You need to have a row already created to update it. Just insert one row first with values of 0 and your script will work fine. If not you would have to create a check to see if there are any rows then insert one. alot of wasted code if you only need one row and not have to do it again.Ray Link to comment https://forums.phpfreaks.com/topic/29523-problem-with-simple-vote-script/#findComment-135469 Share on other sites More sharing options...
BadGoat Posted December 5, 2006 Author Share Posted December 5, 2006 Hey, I'm not so obtuse after all! I inserted one vote per, and it works. Ray, when I created the table, I set the default value to 0. Does it not consider that valid, and increment it from there? Link to comment https://forums.phpfreaks.com/topic/29523-problem-with-simple-vote-script/#findComment-135476 Share on other sites More sharing options...
craygo Posted December 5, 2006 Share Posted December 5, 2006 if you set the default to zero, the field gets set to zero upon an insert only. it does not make a row for you. The default is so that is you insert a row and forget to put something in that row, it will default to zero.Ray Link to comment https://forums.phpfreaks.com/topic/29523-problem-with-simple-vote-script/#findComment-135506 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.