Jump to content

Undefined Index


supanoob

Recommended Posts

I keep getting the following errors with the following bit of code, but i cant find out what the problem is :S

 

Notice: Undefined index: myForm in C:\wamp\www\post_ratings.php on line 13

 

Notice: Undefined index: rate in C:\wamp\www\post_ratings.php on line 17

 

Notice: Undefined index: rate in C:\wamp\www\post_ratings.php on line 25

0

 

 

<?php
$dbuser = "zixel";
$dbpass = "meh";
$dbloc = "localhost"; //db location
$dbname = "hub_beta";
$db_conn = mysql_connect($dbloc,$dbuser,$dbpass);
// test for conn
$db = mysql_select_db( $dbname );
// test for db
// fetch POST params and sanitize
$rate = 1;

$post_rate = $_POST['myForm'];

echo "$post_rate";

if ($_POST['rate'] == '+1')
{
$sql2="UPDATE post_ratings SET rating_up=rating_up+1 where post_id=$rate";
if(mysql_query($sql2));

echo "Post Rated Up.";
}

if ($_POST['rate'] == '-1')
{
$sql2="UPDATE post_ratings SET rating_down=rating_down+1 where post_id=$rate";
if(mysql_query($sql2));

echo "Post Rated Down.";
}
// test for result
// do other stuff
$query="select post_id, rating_up, rating_down from post_ratings where post_id = '$rate'";
$result=mysql_query($query);
if (!$result)
{
die(mysql_error());
}
$num_rows=mysql_num_rows($result);

$row=mysql_fetch_array($result);
$rating_up=($row['rating_up']);
$rating_down=($row['rating_down']);

$rating = ($rating_up-$rating_down);

echo "$rating";  
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/148576-undefined-index/
Share on other sites

It really think that it's not wise to put your mysql password in your post...

 

Second, do some debugging. See if it can select the database....

if (!$db) {
    die ('Can\'t use database : ' . mysql_error());
}

 

yeah good call, but its a wamp5 server. ill check that now thanks.

 

That didnt show any errors :S i do have another bit of code that links to this one, but it has AJAX in it, this is below:

<html>
<head>
<!-- easy ajax - download from http://www.prototypejs.org/-->
<script src="../scripts/prototype.js" type="text/javascript"></script>
<script type="text/javascript">
  function liveStuff()
  {
        var ajax = new Ajax.Updater(
            {
                success:'results',
            },
            '../post_ratings.php',
            {
                parameters: $('myForm').serialize(true)
            }
        );
  }
</script>
</head>

<body>
<form id="myForm" method="post" action="javascript:liveStuff();" onsubmit="liveStuff();">
<select>
  <option value="+1">+1</option>
  <option value="-1">-1</option>
</select>
  <INPUT type="submit" value="Rate">
</form>
<div id=results></div>
</body>
</html>

 

 

Link to comment
https://forums.phpfreaks.com/topic/148576-undefined-index/#findComment-780200
Share on other sites

Undefined index refers to an array.

You are trying to get a key from an array but php is unable to find it and therefore gives you an undefined index warning. Do you use uppercase characters in your mysql field names? If so, you should also use them in your array...

 

To see what the array contains, you could do:

print_r ($arrayname);

Link to comment
https://forums.phpfreaks.com/topic/148576-undefined-index/#findComment-780201
Share on other sites

Undefined index refers to an array.

You are trying to get a key from an array but php is unable to find it and therefore gives you an undefined index warning. Do you use uppercase characters in your mysql field names? If so, you should also use them in your array...

 

To see what the array contains, you could do:

print_r ($arrayname);

 

i have checked all them :S and that doesnt do anything

Link to comment
https://forums.phpfreaks.com/topic/148576-undefined-index/#findComment-780203
Share on other sites

@kickstart....true...how did I miss that one...

 

@supanoob...

You haven't specified a name for your form. You should do so in your HTML code...

 

<html>
<head>
<!-- easy ajax - download from http://www.prototypejs.org/-->
<script src="../scripts/prototype.js" type="text/javascript"></script>
<script type="text/javascript">
  function liveStuff()
  {
        var ajax = new Ajax.Updater(
            {
                success:'results',
            },
            '../post_ratings.php',
            {
                parameters: $('myForm').serialize(true)
            }
        );
  }
</script>
</head>

<body>
<form id="myForm" name="myForm" method="post" action="javascript:liveStuff();" onsubmit="liveStuff();">
<select>
  <option value="+1">+1</option>
  <option value="-1">-1</option>
</select>
  <INPUT type="submit" value="Rate">
</form>
<div id=results></div>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/148576-undefined-index/#findComment-780216
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.