-4
this is the html form to display the the poll question

here the variables poll and question are giving me error plz help with it , im new to php

<html>
    <head></head>
    <body>
    <span style="font-size: 12px;">
    <span style="font-weight: bold; font-size: 14px;">
    Poll #<?php echo $poll; ?>
    </span><br />
    <span style="font-weight: bold"><?php echo $question; ?></span>
    <form action="vote_process.php" method="post">
    <ul style="list-style-type: none;">
    <?php echo $question_list; ?>
    </ul>
    <input name="poll" type="hidden" value="<?php echo $poll; ?>">
    <input name="" type="submit" value="Vote!">
    </form>
    </span>
    </body></html>

//php code-this is where im trying to access database ,i wanna know how to retrieve the value from database and pass to html where the variables are present

<?php
/* Display a vote form. */
require_once("vote_config.php");
$poll= $_POST['poll']; 
if (!is_numeric($poll)) 
die("Invalid poll");

/* Look up the poll in the database. */
$sql = "SELECT P.question, A.answer, A.answer_ID FROM poll P, answer A  WHERE P.ID =".$poll." and A.ID = P.ID";
$result = mysql_query($sql, $db) or die ("mysql error: " . mysql_error());
if (mysql_num_rows($result) == 0) {
die('Invalid poll.');
}

/* If the user has already voted, show the results. */
if ($_COOKIE["poll_voted_$poll"]) {
header("Location: vote_tally.php?poll=$poll");
exit;
}

/* Vote form */
while($row = mysql_fetch_array($result)) {
$question = $row['question'];
$question_list = '<li><input name="answer" type="radio" value='.row['answer_ID'].'> '.$row['answer'] .'</li>';
}
?>
  • Is this all in the same file? – Scimonster May 20 '14 at 10:55
  • 1
    `$poll` isn't yet defined the first time the form is loaded. Use `` instead of `` – Michael Berkowski May 20 '14 at 10:56
  • yeah i have put it in one file – user3656217 May 20 '14 at 10:56
  • ...and the same is probably true for `$question` and `$question_list` – Michael Berkowski May 20 '14 at 10:57
  • Please read http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php -- the code posted above is a "how to open yourself up to SQL injection" – zajd May 20 '14 at 10:58
  • actually when i open the file for first time i want the display as Poll #1 – user3656217 May 20 '14 at 10:58
  • @zajd to be fair, the code above does at least validate that `$poll` is numeric and bail out if not. – Michael Berkowski May 20 '14 at 10:59
  • @user3656217 Then you'll want to start with `` to begin with poll 1. Or better, initialize the variable at the start of the script. `if (!isset($poll)) $poll = 1;` – Michael Berkowski May 20 '14 at 11:01
  • there is nothing to input here in the form, in my db i have data whose id is 1 . i want that particular id to be displayed with $poll and the question from the database which is connected to that id – user3656217 May 20 '14 at 11:01
  • If you want to display question id 1 on first load, you'll need to query that out of your database when it first loads, using `if (!isset($poll)) { //retrieve poll 1 }` – Michael Berkowski May 20 '14 at 11:12
  • @MichaelBerkowski how to do that and should i retrieve in html form or php code part – user3656217 May 20 '14 at 11:26
  • @user3656217 all variables you intend to use in the HTML have to be defined before the form is displayed, so you must initialize them before (above) the HTML part. Generally if PHP and HTML are combined in one script, you should take care of all the PHP logic first, before displaying the form. When the form has been submitted, verify that with something like `if (isset($_POST['Vote!'])) { //process the form, insert data }` so the database logic is only executed when it is supposed to be (rather than every time the form loads) – Michael Berkowski May 20 '14 at 11:40

2 Answers2

0

Change line

Poll #<?php echo $poll; ?>

to

Poll #<?php echo isset($poll) ? $poll : ''; ?>

Make sure to initialize variables before being used to avoid warning messages

rsakhale
  • 1,018
  • 13
  • 26
0

If $poll is not set php will throw an error. So you have to check it first:

<span style="font-weight: bold; font-size: 14px;">
Poll #<?php 
if(isset($poll)) {
    echo $poll; 
} else {
    echo "Poll is not set!"; 
} 
?>
</span><br />
Fabrizio Mazzoni
  • 1,831
  • 2
  • 24
  • 46