2cool Posted April 16, 2009 Share Posted April 16, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/154385-undefined-index-error/ Share on other sites More sharing options...
premiso Posted April 16, 2009 Share Posted April 16, 2009 $var = isset($_REQUEST['name'])?$_REQUEST['name']:null; Quote Link to comment https://forums.phpfreaks.com/topic/154385-undefined-index-error/#findComment-811722 Share on other sites More sharing options...
2cool Posted April 16, 2009 Author Share Posted April 16, 2009 Thank you premiso that has taken away the error. Quote Link to comment https://forums.phpfreaks.com/topic/154385-undefined-index-error/#findComment-811724 Share on other sites More sharing options...
2cool Posted April 16, 2009 Author Share Posted April 16, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/154385-undefined-index-error/#findComment-811729 Share on other sites More sharing options...
Maq Posted April 16, 2009 Share Posted April 16, 2009 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/154385-undefined-index-error/#findComment-811730 Share on other sites More sharing options...
mrMarcus Posted April 16, 2009 Share Posted April 16, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/154385-undefined-index-error/#findComment-811732 Share on other sites More sharing options...
2cool Posted April 16, 2009 Author Share Posted April 16, 2009 Ive replaced the code but now it doesnt change from 'No row has been deleted' at any stage Quote Link to comment https://forums.phpfreaks.com/topic/154385-undefined-index-error/#findComment-811737 Share on other sites More sharing options...
mrMarcus Posted April 16, 2009 Share Posted April 16, 2009 try... $result =mysql_query($delete); if (mysql_affected_rows($result) > 0) { echo "The row has been deleted successfully"; } else { echo "No rows has been deleted"; } Quote Link to comment https://forums.phpfreaks.com/topic/154385-undefined-index-error/#findComment-811738 Share on other sites More sharing options...
Maq Posted April 16, 2009 Share Posted April 16, 2009 Ive replaced the code but now it doesnt change from 'No row has been deleted' at any stage Post your current code. Quote Link to comment https://forums.phpfreaks.com/topic/154385-undefined-index-error/#findComment-811741 Share on other sites More sharing options...
2cool Posted April 16, 2009 Author Share Posted April 16, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/154385-undefined-index-error/#findComment-811748 Share on other sites More sharing options...
Maq Posted April 16, 2009 Share Posted April 16, 2009 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"> < Quote Link to comment https://forums.phpfreaks.com/topic/154385-undefined-index-error/#findComment-811750 Share on other sites More sharing options...
2cool Posted April 16, 2009 Author Share Posted April 16, 2009 ok firstly... no echo statements show right at the beginning before entering anything so thats good and it deletes the record BUT no echo statements show saying for example 'the row has been deleted'. Quote Link to comment https://forums.phpfreaks.com/topic/154385-undefined-index-error/#findComment-811756 Share on other sites More sharing options...
gurroa Posted April 16, 2009 Share Posted April 16, 2009 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"; } } ?> ... Quote Link to comment https://forums.phpfreaks.com/topic/154385-undefined-index-error/#findComment-811793 Share on other sites More sharing options...
2cool Posted April 16, 2009 Author Share Posted April 16, 2009 Ive tried the latest code but that has no effect at all, it doesnt even delete the record. Quote Link to comment https://forums.phpfreaks.com/topic/154385-undefined-index-error/#findComment-811863 Share on other sites More sharing options...
Maq Posted April 16, 2009 Share Posted April 16, 2009 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"> < Quote Link to comment https://forums.phpfreaks.com/topic/154385-undefined-index-error/#findComment-811869 Share on other sites More sharing options...
2cool Posted April 16, 2009 Author Share Posted April 16, 2009 No i havent provided any password for mysql_connect. Im afraid it has no reaction,did not delete the record and no output too. I didnt know this could get so complicated... Quote Link to comment https://forums.phpfreaks.com/topic/154385-undefined-index-error/#findComment-811882 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.