-1

i want to display the food name and foodRate from my database but i cannot find out why i cannot do it with the below script. input type radio and name all of them button with different value data is inserted no problem and problem is in the first block of php error Notice: Undefined index: foodID in C:\wamp\www\web\polling\includes\resul and Warning: mysqli_query() expects parameter 1 to be mysqli, integer given i

    $pollid = $_POST['foodID'];
    $connection = include('connection.php');

    $query = "SELECT * FROM polling WHERE foodID='$pollid'";
    $q = mysqli_query($connection, $query);
    while($row = mysqli_fetch_array($q)) {
        $id = $row[0];
        $food = $row[1];
        $foodRate = $row[2];
        $userEmail = $row[3];
        echo "<h1>$food</h1>";
        echo "<h1>$userEmail</h1>";
    }
?>

Below script is fine working good

if(isset($_POST['submit'])){
    if(isset($_POST['button'])&&isset($_POST['email'])){


        $query=$DBH->prepare('SELECT COUNT(*) FROM polling WHERE userEmail=?');
        $query->bind_param('s',$_POST['email']);
        $query->execute();
        $query->bind_result($count);
        $query->close();
        if($count>0)echo"You've already used this email address to vote!";
        else{
            $update=$DBH->prepare('INSERT INTO polling (userEmail,foodRating) VALUES(?,?)');
            $update->bind_param('si',$_POST['email'],$_POST['button']);
            $update->execute();
            $update->close();
            echo"you have successfully voted! Thank you!";
        }
    }elseif(!isset($_POST['email']))echo"No email address enter";
    elseif(!isset($_POST['button']))echo"You've not selected a vote";
}


?>
Ding
  • 3,027
  • 1
  • 15
  • 27
Hamza
  • 49
  • 7

2 Answers2

0

You are missing a symbol in the statement

$query = "SELECT * FROM polling WHERE foodID='$pollid'";

s/b

$query = "SELECT * FROM `polling` WHERE foodID='$pollid'";

Kirk Powell
  • 895
  • 9
  • 14
  • will make absolutely no difference,. http://stackoverflow.com/questions/261455/using-backticks-around-field-names –  Jan 27 '15 at 22:55
0

Try mysqli_affected_rows() and see if $q is getting any data, if not it will never enter the while loop

Besides that it appears there is an issue in your connection, can you display how your connecting in connection.php?

I'm not sure but the two different types of mysql interactions on the same page raises a red flag. Do you have other pages that work with two types of mysql interactions?

EDIT 1: Try this

$connection = mysqli_connect("localhost", "root", "", "test");

if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

This should work for your first script

Is there a reason your using two different types of mysql interaction?

Ian Thompson
  • 172
  • 1
  • 10
  • connect_errno) { echo "Failed to connect to MySQL: (" . $DBH->connect_errno . ") " . $DBH->connect_error; } ?> – Hamza Jan 27 '15 at 23:27
  • i an new to php so any recommendation will be welcomed. – Hamza Jan 27 '15 at 23:33
  • ^^^ edited,,,you can keep both connections if you close the first before opening the second one if it's not possible to run both of your queries off off one connection – Ian Thompson Jan 28 '15 at 01:02