Jump to content

Recommended Posts

Hi All,

 

Can anyone tell me why i might be getting this error:

 

an error occured in the script '/home/footba33/public_html/index.php' on 94:

Undefined index: team_name

 

Code applying to line 94 is:

 


   <td>'.$_SESSION['search_result']['team_name'].'</td>
   <td><a href="'.$_SESSION['search_result']['url'].'">'.$_SESSION['search_result']['url'].'</a></td>
</tr>
</tbody>'); 

 

I have provided the script below and thanks for any help!

 


<?php

// This is the main page for the site

// Include our error config file

require_once ('./includes/config.inc.php');

// Set the page title

$page_title = 'Football Trip';

include ('./includes/header.html');

// Welcome the user when they are logged in

echo '<h1> Welcome to Football Trip';

if (isset($_SESSION['first_name'])) {
echo ", {$_SESSION['first_name']}!";
}

echo '</h1>';

?>

<form action="index.php?go" method="post" />

   <p class="team_supported"><label> Team Name: </label>
<input type="text" name="team_name" maxlength="40" />

   <p class="submit">
<input type="submit" name="submit" value="Search" />
           
</form>

<?php

$error = null;

if(isset($_POST['submit'])){ // Search form

require_once ('../mysql_connect.php');

if (

preg_match ('/^[[:alnum:] ]{4,30}$/i', $_POST['team_name'])

) {

$query = "SELECT * FROM teams WHERE team_name LIKE '%" . $_POST['team_name'] .  "%'";

$result = mysql_query($query) or trigger_error("Query: $query\n<br />MySQL Error: ".mysql_error());

$_SESSION['search_result'] = array();

while($row = mysql_fetch_array($result)){
$_SESSION['search_result'][] = $row;
}

} else {

$error = '<center><p><font color ="red">Team Name contains either; one or more invalid characters, or too many/too little characters.</font></p></center>';
}
}

if(!isset($_SESSION['search_result']) || $error != null){ // No Search or errors
echo(($error != null)? $error : "Please submit a Search query");

}else{

echo('

<table width="600" height="100" border="0" />
<thead>    
  <tr>
<th scope="col">Team Name</th>
<th scope="col">Fixture List URL</th> 
  </tr>
</thead>');

echo('

<tbody>
<tr>
<td>'.$_SESSION['search_result']['team_name'].'</td>
<td><a href="'.$_SESSION['search_result']['url'].'">'.$_SESSION['search_result']['url'].'</a></td>
</tr>
</tbody>'); 

}




// Include HTML Footer

include ('./includes/footer.html');
?>

Link to comment
https://forums.phpfreaks.com/topic/200389-search-database-error/
Share on other sites

do a print_r and you will be able to see the format of $_SESSION['search_result']. It is probably different that what you think.

 

since you do this here

while($row = mysql_fetch_array($result)){
$_SESSION['search_result'][] = $row;
}

 

$_SESSION['search_result'] becomes a numeric array, it would look something like

$_SESSION['search_result'][0] = first row
$_SESSION['search_result'][1] = second row

 

If you wanted to access a certain column of one of the rows, you have to specify it

$_SESSION['search_result'][0]['team_name'];

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.