I"m getting an undefined index error message on this script.

jvutechnc
jvutechnc used Ask the Experts™
on
It has to do with the index act but I can't figure it out.
<?php

include("../dbconnect.php");

$info = mysql_query("SELECT * FROM requests"); 

echo '<table border="1" cellpadding="3" cellspacing="1"> 
  <tr valign="top"> 
    <td>Date</td> 
    <td>From</td> 
    <td>Subject</td> 
    <td>Message</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
  </tr>'; 
     
if (mysql_num_rows($info) < 1) { 
echo '<tr valign="top"> 
  <td colspan="4">There are no members that match the query. Please go back and try again</td> 
    </tr>'; 
} 

else { 
   while ($qry = mysql_fetch_array($info)) { 

//create the layout 

?>
  <tr valign="top"> 
    <td><?php echo $qry['ID']; ?></td>     
    <td><?php echo $qry['date']; ?></td> 
    <td><?php echo $qry['from']; ?></td> 
    <td><?php echo $qry['subject']; ?></td> 
    <td><?php echo nl2br($qry['message']); ?></td> 
    <td><a href="?act=edit&id=<?php echo $qry['ID']; ?>">Edit</a></td> 
    <td>Delete</td> 
  </tr> 


<?php

   } 
} 


echo '</table>'; 


if ($_GET['act'] == 'edit') {
    require("edit.php");
} 
?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Please take a look at lines 35 and 49.
Hi,

are you sure that 'act' exists in your get data? It would need to be part of the url. How are you sending data to this page. If it is a situation where you might not have this in url or you may, try using isset

if(isset($_GET['act'])
{
  if($_GET['act'] == 'edit'])
  {
     require("edit.php");
   }
}
sorry there needs to be another ) at the end of the first line

if(isset($_GET['act']))
{
  if($_GET['act'] == 'edit'])
  {
     require("edit.php");
   }
}

Author

Commented:
thanks--I tried your code and get a parse error.  Does my line 35 look correct?..the url?
Expert of the Quarter 2010
Expert of the Year 2010
Commented:
Run this query in your fav mysql query tool:

SELECT * FROM requests limit 1;

Now, match up the column names, CASE-sensitive, against these lines

    <td><?php echo $qry['ID']; ?></td>    
    <td><?php echo $qry['date']; ?></td>
    <td><?php echo $qry['from']; ?></td>
    <td><?php echo $qry['subject']; ?></td>
    <td><?php echo nl2br($qry['message']); ?></td>
    <td><a href="?act=edit&id=<?php echo $qry['ID']; ?>">Edit</a></td>

Specifically, "ID", "date", "from", "subject", "message"
php/mysql_fetch_assoc is case sensitive

Author

Commented:
Yep, the order and column names are correct.   ID is in caps on the database.
Expert of the Quarter 2010
Expert of the Year 2010

Commented:
Try this:

change
    <td><a href="?act=edit&id=<?php echo $qry['ID']; ?>">Edit</a></td>

to
    <td><a href="<?php echo $_SERVER['PHP_SELF']; ?>?act=edit&id=<?php echo $qry['ID']; ?>">Edit</a></td>
What is exactly the error that you get?
AN "undefined index" error is not usually important - it just means that the array does not have the index you are looking for. There are two options.

1. Ignore it completely. Doing an insertion of a new element will generate an array index error, but it does not matter  

$arr = array();
$arr ['newIndex'] = 123;


2. If you are READING the array rather than adding data to it then you may want to know that the value is missing so you can take your own action. By default PHP would return "" and you may not want that. Use ISSET() in this case.

$arr = array();

if ( !isset( $arr['newIndex'] ) )
    $myValue = -1;
else
    $myValue = $arr ['newIndex'];


To suppress the error messages either change the defaults in php.ini or add one or both of these lines to the start of your script;

ini_set('display_errors', 0 );  // Will suppress ALL error messages

error_reporting( E_ALL & ~E_NOTICE );  // Will stop "Notice" messages but will allow all others

Author

Commented:
Sorry to be a pain...let's me shift gear a little.  I'm looping through my mysql table to show all results allow with a column to Edit or Delete the record.   What are other ways you would go about doing this?

Marco GasiFreelancer
Top Expert 2010

Commented:
First, I suggest to popst the exact error message you get (what exactky happens) when you click Edit link.

Second, I think that you have to follow cyberkiwi suggestion and tu write

<td><a href="<?php echo $_SERVER['PHP_SELF']; ?>?act=edit&id=<?php echo $qry['ID']; ?>">Edit</a></td>

If this doesn't work, you should try to verify that parameter isn't empty and write

<td><a href="<?php echo $_SERVER['PHP_SELF']; ?>?act=edit&id=<?php echo $qry['ID']; ?>">Edit <?php echo $qry['ID']; ?></a></td>

If id resulkt is correct, you can try to write the entire url <a href="myeditpage.php?act=edit&id=<?php echo $qry['ID']; ?>">Edit</a>

If this doesn't work you can try to use a form in every row fo your table passing id and action as hidden fields, but this should not be necessary. My opinion is that id parameter is empty. Let me know your output, please.
Bye

Author

Commented:
btw, thank you everyone for helping me with this on a weekend.

marqusG,
The output is correct; next to each EDIT link is the correct row ID.
The error I am getting is:
Notice: Undefined index: act in C:\Program Files\wamp\www\Scripts\testedit.php on line 50

As you suggested, I'm going to try <a href="myeditpage.php?act=edit&id=<?php echo $qry['ID']; ?>">Edit</a>
Marco GasiFreelancer
Top Expert 2010

Commented:
Now I have to go but I'll return later. I suugest to try


if(isset($_GET['act']){
    echo "act is " . $_GET['act']."<br>";
}else{
    echo "act is not set<br>";
}

"See" you later.

Author

Commented:
Come to think of it, we can just remove and not use

if(isset($_GET['act']){
    echo "act is " . $_GET['act']."<br>";
}else{
    echo "act is not set<br>";
}

Now, if I can just figure out how to pass the ID of the row from this script to my edit.php

<a href="edit.php?act=edit&id=<?php echo $qry['ID']; ?>">Edit</a>

Currently, the edit.php is reporting:
Notice: Undefined index: ID in C:\Program Files\wamp\www\Scripts\edit.php on line 6

Notice: Undefined index: edit in C:\Program Files\wamp\www\Scripts\edit.php on line 14
The user with the id does not exist!
Notice: Undefined index: edit in C:\Program Files\wamp\www\Scripts\edit.php on line 53


Attached is the edit.php script
<?php

include("../dbconnect.php");

// Get the user id
$id = $_POST['ID'];

// Get data from user with the specified id
$info = mysql_query("SELECT * FROM requests WHERE id = '$id'") or die ('Error Getting User Data! <br />' .mysql_error());
$chk = mysql_num_rows($info);
$u = mysql_fetch_array($info);

// If edit not hit
if (!$_POST['edit']) {

    // If user id returns no results
    if ($chk < 1) {
        echo 'The user with the id <b>'.$id.'</b> does not exist!';
    }
    else {
        // Edit Form
        ?>
    <!-- Edit Form -->
 
       <form method="post" action="">
            <table width="250">
                <tr>
                    <td>First Name:</td>
                    <td><input type="text" name="date" value="<?php echo $u['date'] ?>" /></td>
                </tr>
                <tr>
                    <td>Last Name:</td>
                    <td><input type="text" name="from" value="<?php echo $u['from'] ?>" /></td>
                </tr>
                <tr>
                    <td>Username:</td>
                    <td><input type="text" name="subject" value="<?php echo $u['subject'] ?>" /></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" name="edit" value="Edit" /></td>
                </tr>
            </table>
        </form>
    
<!-- /Edit Form -->

        
<?php

}
}
// If edit was hit
if ($_POST['edit']) {
    $firstname = $_POST["date"];
    $lastname = $_POST["from"];
    $username = $_POST["subject"];
    
        // Update data
        $query = mysql_query("UPDATE requests SET from='$lastname', WHERE id='$id'") or die (mysql_error());
        
        echo "Update successful";
}

?>

Open in new window

In edit.php you are using the $_POST  var, but you shoud use $_GET var instead.
In edit.php you are using the $_POST  var, but you shoud use $_GET var instead, only in line 6. In the other cases, you should check if the var is set
change in line 6
    $id = $_POST['ID']; => $id = $_GET['id'];
in line 14
     if (!$_POST['edit']) { => if (!isset($_POST['edit'])) {
an in line 53
     if ($_POST['edit']) { => if (isset($_POST['edit'])) {



 


Marco GasiFreelancer
Top Expert 2010
Commented:
Sorry, but I think you shoult change all your $_POST into $_GET. It seems that you pass your both variables act and ID as url parameters, so the conditional expressions

               if (!$_POST['edit'])
or
             
              if ($_POST['edit'])

will always return a wrong value because $_POST array is empty or simply doesn't contains act and ID values that you pass via url

No, the form that is generated by the script edit.php is using the POST method
Marco GasiFreelancer
Top Expert 2010

Commented:
Yes, this is true. But the form generated by the script contains an inoput field named "edit", not "act".  Error messages seem not to be generated by values posted by this form but  by some error when edit.php is loaded the first time, or this is what I've understood: perhaps I'm wrong... It seems there is some confusion with post and get variables. Maybe we have to understan WHEN errors appear: when edit.php page is the first time loaded or when the edit.php form is submitted?
Marco GasiFreelancer
Top Expert 2010

Commented:
Sorry, but now the solution could be this

line 6: $id = $_GET['ID'];

line 14: if (isset($_POST['edit']) {
                   if (!$_POST['edit']) {
                   ...

line 53: if (isset($_POST['edit']) {
                   if ($_POST['edit']) {
                   ...

Try this, please and let us know what happens.

Bye

Author

Commented:
margusG,  Thanks,  I did as you suggested.  Everything is working now.
Line 6:  $id = $_GET['ID'];
Line 14:    if (!isset($_POST['edit'])) {
Line 53:  if (isset($_POST['edit'])) {

However, what is wrong with this statement?  I'm getting You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from = 'test test ' WHERE id = '1000'' at line 1

$result = mysql_query("UPDATE requests SET from = '$lastname' WHERE id = '$id'") or die (mysql_error());
hi,

from is a reserved word, to use it as column name you are going to need to place ` around it

$result = mysql_query("Update requests SET `from` = '$lastname' Where `id` = '$id'") or die(mysql_error());

truthfully i find it easier to just always place ` around my column names in any queries, then you don't have to worry about reserved words

Author

Commented:
Thanks, I put ' around from and the error is still present.  Man, this driving me crazy.
Marco GasiFreelancer
Top Expert 2010

Commented:
First, let me do a stupid question: viewing $lastname, I expected to read 'test' but I read 'test test': have you passed 'test test' as lastname? I ask this because many times I found stupid errors in my code, so...
Second, when you assign poits, remember that the right solution was posted first by bombwriter (see above answer with ID = 33133425): I only repeated what he said, so you have to give points to him.
Third, if it isn't an unhuman work, I think you could change column name 'from' with anotherone, cause I don't see any error in your query syntax and if "test test" and "1000" are right values for $lastname  and $id, I don't imagine any other solution...

Author

Commented:
MarqusG,

oh yes, thank you for pointing that out--I'll make sure to give bombwriter points.

I found out that the UPDATE syntax I was using was wrong.  I was putting ' around my columns instead of `
e.g. I had ("UPDATE tablename SET 'from' = '$from'.....should be SET `from`

Author

Commented:
Thank you to all for helping me on these issues!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial