Jump to content

Recommended Posts

I have put together this code for deleting a record from the database and it works but it always shows an 'Undefined Index: name' error... even before i have entered anything.

 

The code i am using is:


<form action ="adminRegister.php" name="thisform" method="get">
          <div align="center">
            <p>
              <INPUT TYPE="text" NAME="name" SIZE="30">
            </p>
            <p><br>
              <input type="submit" value="click to delete!" onclick="submit();">
              <br>
              <?php
$var = $_REQUEST['name'];
//enter the table name and primary key name
$delete = "delete from members where id = \"$var\"";
//you have to put the database connection code yourself.
$DBConnect =
@mysql_connect("localhost", "root");
mysql_select_db("dietetics", $DBConnect) or die(mysql_errno() . ": " . mysql_error() . "<br>");
$result =mysql_query($delete);
if ($result ==true)
{ echo "The row has been deleted successfully"; }
else echo "No rows has been deleted";
?>
[/Code]

 

Can anyone help me get rid of that error?

 

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

Your $result will always return true because it does not error out.  Comparing something to null isn't an error.  mysql_affected_rows() will return the number of rows that have been affected by the previous query executiong.

 

Replace these lines:

 

$result =mysql_query($delete);
if ($result ==true)
{ echo "The row has been deleted successfully"; }
else echo "No rows has been deleted";

 

with these lines:

 

$result =mysql_query($delete);
if (mysql_affected_rows() > 0)
{ 
    echo "The row has been deleted successfully"; 
} 
else 
{    
    echo "No rows has been deleted";
}

Just another quick question, is there a way of getting the echo statement: 'The row has been deleted successfully'  after i have entered something and deleted and not before as this line appears as soon as i enter the page?

your code needs some definite cleaning up.

 

place the sql query inside an if statement that will only execute the delete query if the proper conditions are met.

Here is the current code:


<?php
$var = isset($_REQUEST['name'])?$_REQUEST['name']:null;
$delete = "delete from members where id = \"$var\"";

$DBConnect = @mysql_connect("localhost", "root");
mysql_select_db("dietetics", $DBConnect) or die(mysql_errno() . ": " . mysql_error() . "<br>");
$result =mysql_query($delete);
if (mysql_affected_rows() > 0)
{
    echo "The row has been deleted successfully";
}
else
{   
    echo "No rows has been deleted";
}
?>
[/Code]

 

 

And after trying this code:

 

[Code]
<?php
$var = isset($_REQUEST['name'])?$_REQUEST['name']:null;
$delete = "delete from members where id = \"$var\"";

$DBConnect = @mysql_connect("localhost", "root");
mysql_select_db("dietetics", $DBConnect) or die(mysql_errno() . ": " . mysql_error() . "<br>");
$result =mysql_query($delete);
$result =mysql_query($delete);
if (mysql_affected_rows($result) > 0)
{
    echo "The row has been deleted successfully";
}
else

    echo "No rows has been deleted";
}
?>
[/Code]

 

i have got this error, even though it deletes the record:

Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource

 

And the 'No rows has been deleted' comes before and after anything is entered.

Try this:

 

$DBConnect = mysql_connect("localhost", "root");

if(isset($_POST['submit']))
{
   $var = isset($_POST['name']) ? $_POST['name'] : null;
   $delete = "delete from members where id = $var";
   mysql_select_db("dietetics", $DBConnect) or die(mysql_errno() . ": " . mysql_error() . "
");
   $result = mysql_query($delete) or die(mysql_query());
   if (mysql_affected_rows() > 0)
   { 
      echo "The row has been deleted, successfully"; 
   } 
   else 
   {    
      echo "No rows have been deleted";
   }
}
?>

</pre>
<form action="adminRegister.php" name="thisform" method="get">

  

   
    
      

     

    
  


<

You get your warning because of definition of mysql_affected_rows() function:

int mysql_affected_rows ([ resource $link_identifier ] )

If you got no echo statements it could mean that php stop on error...

Try this one:

<?php
$DBConnect = mysql_connect("localhost", "root");

if(isset($_POST['submit']))
{
   mysql_select_db("dietetics", $DBConnect) or die(mysql_errno() . ": " . mysql_error() . "<br>");
   
   $delete = sprintf("delete from members where id = '%s'",
               isset($_POST['name']) ? $_POST['name'] : null
             );
             
   $result = mysql_query($delete);
   if (mysql_affected_rows() > 0)
   { 
      echo "The row has been deleted, successfully"; 
   } 
   else 
   {    
      echo "No rows have been deleted";
   }
}
?>
...

Did you provide a password for mysql_connect?  It would be the last parameter in your function.

 

I added some debugging lines in here.  Run it and tell me the output, if any...

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

if(isset($_POST['submit']))
{
   $DBConnect = mysql_connect("localhost", "root") or die(mysql_error());
   $var = isset($_POST['name']) ? $_POST['name'] : null;
   $delete = "delete from members where id = $var";
   mysql_select_db("dietetics", $DBConnect) or die(mysql_error());
   $result = mysql_query($delete) or die(mysql_query());
   if (mysql_affected_rows() > 0)
   { 
      echo "The row has been deleted, successfully"; 
   } 
   else 
   {    
      echo "No rows have been deleted";
   }
}
?>

</pre>
<form action="adminRegister.php" name="thisform" method="get">

  

   
    
      

     

    
  

<

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.