timmah1 Posted June 3, 2008 Share Posted June 3, 2008 Can anybody see what I'm doing wrong? If the database finds an answer, it shows the "You already answered", but if they haven't answered, it won't show the form. $answers = "SELECT * FROM answers"; $query = mysql_query($answers) or die(mysql_error()); $total = mysql_num_rows($query); while ($row1 = mysql_fetch_assoc($query)) { if($row1['username'] == $_SESSION['SESS_USERNAME'] && $row1['day'] == $day) { echo "You have already voted this week<br>Your answer $row1[answer]"; } //if they haven't voted, show them the form elseif($row1['username'] == 'null' && $row1['day'] == 'null') { ?> <form action="" method="POST"> Your guess: <input name="textfield" type="answer" id="answer" value="<?php echo $_POST['answer']; ?>" size="45" maxlength="45"><br /> <input type="hidden" name="answer" id="answer" value="yes" > <input type="hidden" name="week" id="week" value="<?php echo $row['day']; ?>" > <input type="submit" name="button" id="button" value="Submit"> </form> <?php } } Any help would be greatly appreciated. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/108581-solved-elseif/ Share on other sites More sharing options...
jonsjava Posted June 3, 2008 Share Posted June 3, 2008 first question. Table structure: is the username and day rows default null, or not null? if it says "NOT NULL", then you need to run it like this: <?php $answers = "SELECT * FROM answers"; $query = mysql_query($answers) or die(mysql_error()); $total = mysql_num_rows($query); while ($row1 = mysql_fetch_assoc($query)) { if($row1['username'] == $_SESSION['SESS_USERNAME'] && $row1['day'] == $day) { echo "You have already voted this week<br>Your answer $row1[answer]"; } //if they haven't voted, show them the form elseif($row1['username'] == "" && $row1['day'] == "") { ?> <form action="" method="POST"> Your guess: <input name="textfield" type="answer" id="answer" value="<?php echo $_POST['answer']; ?>" size="45" maxlength="45"><br /> <input type="hidden" name="answer" id="answer" value="yes" > <input type="hidden" name="week" id="week" value="<?php echo $row['day']; ?>" > <input type="submit" name="button" id="button" value="Submit"> </form> <?php } } Quote Link to comment https://forums.phpfreaks.com/topic/108581-solved-elseif/#findComment-556802 Share on other sites More sharing options...
timmah1 Posted June 3, 2008 Author Share Posted June 3, 2008 It don't work, the form won't show if they haven't answered If the user hasn't submitted an answer for that particular picture, the database is empty to their username and day, then it should show the form, but if there is an entry for their username and day, then it should display, you already answered, which it does, but if they haven't answered, the form don't show Quote Link to comment https://forums.phpfreaks.com/topic/108581-solved-elseif/#findComment-556813 Share on other sites More sharing options...
jonsjava Posted June 3, 2008 Share Posted June 3, 2008 dumb question: why won't this work: <?php $username = $_SESSION['SESS_USERNAME']; $answers = "SELECT * FROM answers WHERE `username` = '$username' AND `day` = '$day';"; $query = mysql_query($answers) or die(mysql_error()); $total = mysql_num_rows($query); while ($row1 = mysql_fetch_assoc($query)) { if($total > 0) { echo "You have already voted this week<br>Your answer $row1[answer]"; } //if they haven't voted, show them the form else{ ?> <form action="" method="POST"> Your guess: <input name="textfield" type="answer" id="answer" value="<?php echo $_POST['answer']; ?>" size="45" maxlength="45"><br /> <input type="hidden" name="answer" id="answer" value="yes" > <input type="hidden" name="week" id="week" value="<?php echo $row['day']; ?>" > <input type="submit" name="button" id="button" value="Submit"> </form> <?php } } ? Quote Link to comment https://forums.phpfreaks.com/topic/108581-solved-elseif/#findComment-556816 Share on other sites More sharing options...
timmah1 Posted June 3, 2008 Author Share Posted June 3, 2008 Good question, wish I had an answer. But it don't, that was how I originally wrote it Here is the entire code for that page, maybe there's an issue elsewhere <?php session_start(); require("config.php"); //check to make sure the user is logged in if(isset($_SESSION['SESS_LOGGEDIN']) == FALSE){ header("Location: " . $config_basedir . "/login.php?error=redirect"); } //This is what will show if the user is logged in //grab the photo that the user clicked $search = "SELECT * FROM photos WHERE id = '$id'"; $query = mysql_query($search) or die(mysql_error()); $total = mysql_num_rows($query); while ($row = mysql_fetch_assoc($query)) { //if the user hits the submit button, let's put his answer in the database if($_POST['answer'] == "yes") { $sql = "INSERT INTO answers(day, answer, username) VALUES( '" . $_POST['day'] . "', '" . $_POST['answer'] . "', '" . $_SESSION['SESS_USERNAME'] . "');"; mysql_query($sql); require("header.php"); echo "answer submitted"; } else { require("header.php"); ?> <table width="100%" border="0" cellspacing="0" cellpadding="10"> <tr> <td><img src="<?php echo $config_basedir; ?>/photos/<?php echo $row['photo']; ?>" width="400" border="0"></td> </tr> <tr> <td><?php //make sure the user hasn't voted for today's photo $username = $_SESSION['SESS_USERNAME']; $answers = "SELECT * FROM answers WHERE `username` = '$username' AND `day` = '$day';"; $query = mysql_query($answers) or die(mysql_error()); $total = mysql_num_rows($query); while ($row1 = mysql_fetch_assoc($query)) { if($total > 0) { echo "You have already voted this week<br>Your answer $row1[answer]"; } //if they haven't voted, show them the form else{ ?> <form action="" method="POST"> Your guess: <input name="textfield" type="answer" id="answer" value="<?php echo $_POST['answer']; ?>" size="45" maxlength="45"><br /> <input type="hidden" name="answer" id="answer" value="yes" > <input type="hidden" name="week" id="week" value="<?php echo $row['day']; ?>" > <input type="submit" name="button" id="button" value="Submit"> </form> <?php } } ?> </td> </tr> </table> <?php } } require("footer.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/108581-solved-elseif/#findComment-556822 Share on other sites More sharing options...
timmah1 Posted June 3, 2008 Author Share Posted June 3, 2008 Figured it out. Thanks for the help //make sure the user hasn't voted for today's photo $username = $_SESSION['SESS_USERNAME']; $answers = "SELECT * FROM answers WHERE username = '$username' AND day = '$day'"; $query = mysql_query($answers) or die(mysql_error()); $total = mysql_num_rows($query); //if they have voted, tell them they have already voted if($total == 1){ echo "You have already voted for this day's photo<br>Go back and view past weeks <a href='index.php'>Here</a>"; } //if they haven't voted, show them the form else { $row1 = mysql_fetch_assoc($query); if($row1['username'] == '' && $row1['day'] == '') { ?> <form action="" method="POST"> Your guess: <input name="textfield" type="answer" id="answer" value="<?php echo $_POST['answer']; ?>" size="45" maxlength="45"><br /> <input type="hidden" name="answer" id="answer" value="yes" > <input type="hidden" name="day" id="day" value="<?php echo $day; ?>" > <input type="submit" name="button" id="button" value="Submit"> </form> <?php } } In case anybody was curious Quote Link to comment https://forums.phpfreaks.com/topic/108581-solved-elseif/#findComment-556882 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.