开发者

Php Undefined index, when storing a variable

Problem: Need to store field value when doing this query on my database. Have a couple pages that's using this same syntax but for some odd reason this isn't cooperating..

HTML

    &l开发者_StackOverflow社区t;form method="post" action="listPage.php">  
    <fieldset>  
    <legend>Pull some data</legend>  
    <label for="name">Name</label>  
    <input type="text" name="name" id="name" maxlength="255" /> 
    <hr />  
    <br />
    <input type="submit" value="find event" /> 
    </fieldset>  
    </form>

listPage.php

<?php

//CONNECT TO DATABASE
$user="root";
$password="";
$database="db";
$connection=mysql_connect('localhost',$user,$password);
@mysql_select_db($database) or die( "Unable to select database");

echo "NOT running...";

//STORE ALL DATA FROM PREVIOUS FORM
if (isset($_POST['name'])) {
  $name = mysql_real_escape_string($_POST['name']);

  $query="SELECT event FROM events WHERE event='$name'";  
  $result=mysql_query($query) or die(mysql_error());  
  $num=mysql_numrows($result);

    echo "running...";

  while ($row = mysql_fetch_array($result)) 
  {  
    echo $row['event'] . " // ";
    echo "<br />";
  }  
}

mysql_close($connection);  
?> 


Check to see if the post value is set before doing anything with it. (Also, don't forget to call mysql_real_escape_string() on it).

It may not be set if this code occurs on the page when it initially loads or is refreshed after having data posted to it.

// Don't attempt any of this unless you actually have a $_POST value
if (isset($_POST['name'])) {
  $name = mysql_real_escape_string($_POST['name']);

  $query="SELECT event FROM events WHERE event='$name'";  
  $result=mysql_query($query) or die(mysql_error());  
  $num=mysql_numrows($result);

  while ($row = mysql_fetch_array($result)) 
  {  
    echo $row['event'] . " // ";
    echo "<br />";
  }  
}


I couldn't quickly find any documentation regarding possible reserved words for POST or html form variables, so I went to

http://www.w3schools.com/TAGS/tryit.asp?filename=tryhtml_form_method_post

and changed fname field to name, (clicked edit and click me button) and the POST variable was not passed or recognized.

So you might consider changing the name of your field to something other than name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜