php delete works if i use text input.wont work if i use drop down menu from DB by php

Hi

my script works on the html page as it should it shows up the names in a drop down menu from a database called (test) table called (teacher) row called (name)
i have attached image 1 to show my script on the HTML form using php.php script to get the names from the database to the HTML FORM.....

<form action="test2.php" method="POST">  
<?php include('php.php'); ?>

DEL Teacher:  <select name="name[<?php echo $i ?>]"><?php echo $options ?></select><br>

Open in new window


HTML Form shows the teachers names in dropdown menu

*****Script called test2.1.php

<html> 
<head><title>Delete Student Form</title></head> 

<body> 
<h2 align=center><font color=blue>Delete a Teacher Record</font> </h2><p> 

Enter the Name of the Teacher to be deleted:<br> 
<form action="test2.php" method="POST">  
<?php include('php.php'); ?>

DEL Teacher:  <select name="name[<?php echo $i ?>]"><?php echo $options ?></select><br>

<input type="submit" value="Delete"> 
<input type="reset" value="Reset"> 

</form> 

</body> 
</html>

Open in new window


*****Script called php.php

php script looks to the DATABASE test and gets the names of the teachers lines 18 to 22 deal with teachers names
<?php 

// Connect to server 
// Replace username and password by your details  

$db = @mysql_connect("localhost","root","iqonr301"); 
if (!$db) 
{ 
        do_error("Could not connect to the server"); 
} 


// Connect to the database 
// Note that your database will be called username 

@mysql_select_db("test",$db)or do_error("Could not connect to the database"); 

//connect to db first
$options = '';
$teachers = mysql_query('SELECT * FROM teacher ORDER BY name ASC');
while($teacher = mysql_fetch_array($teachers)) {
     $options .= sprintf("<option value='%s'>%s</option>", $teacher['name'], $teacher['name']);
}
$class_options = '';
$classes = mysql_query('SELECT * FROM class');
while($class = mysql_fetch_array($classes)) {
     $class_options .= sprintf("<option value='%s'>%s</option>", $class['name'], $class['name']);
}
$room_options = '';
$rooms = mysql_query('SELECT * FROM room');
while($room = mysql_fetch_array($rooms)) {
     $room_options .= sprintf("<option value='%s'>%s</option>", $room['number'], $room['number']);
}
$subject_options = '';
$subjects = mysql_query('SELECT * FROM subject');
while($subject = mysql_fetch_array($subjects)) {
     $subject_options .= sprintf("<option value='%s'>%s</option>", $subject['name'], $subject['name']);
}
?>

Open in new window


once the user selects the teacher name for deleting the HTML FORM then uses test2.php script to carry out the deleting of that teachers name.....

******PROBLEM SECTION*******

BUT this is where the issue happens im not sure if test2.php is getting the name form the form correctly as if i just make a text box on the HTML FORM page and manually type in a teacher name well then it deletes the name from the database.

but if i use the drop down menu it like test2.php cant see or read the name to be deleted...
i hope i am making sense with all this

use the name to

*****Script called  test2.php  *********
<?php 
 
// Read name from form using $_POST (safest) 

$id=$_POST["name"]; 

// Connect to server 
// Replace username and password by your details 

$db = @mysql_connect("localhost","root","iqonr301"); 
if (!$db) 
{ 
        do_error("Could not connect to the server"); 
} 

// Connect to the database 
// Note that your database will be called username 

@mysql_select_db("test",$db)or do_error("Could not connect to the database"); 
 


$qry = "DELETE FROM teacher WHERE name='$id';";
$result = mysql_query($qry);

echo mysql_error();

if (!$result) {echo "query failed<br>";
}
else {echo "query worked<br>";

print_r($id);      
// $affected is zero so record does not exist         

do_error("No such record"); 

} 

function  do_error($error)
 {       
   echo  $error;
            die; 
} 

?>

Open in new window


and this is the following error i get from this script
Notice: Array to string conversion in C:\xampp\htdocs\test2.php on line 23
query worked
Array ( [
Notice: Undefined variable: i in C:\xampp\htdocs\test2.1.php on line 11
] => ******** ) No such record
paddy086Asked:
Who is Participating?
 
Scott MadeiraConnect With a Mentor Commented:
OK...  Looks like you are in good shape.  The

     array(1) { ["name"]=> string(5) "Test Teachername" }  

text is from the var_dump($_POST) that you put at the top of test2.php on line 4. You can delete that now since you don't need to see what is being passed in.

The other text is from lines 30 to 35 of test2.php.  Not sure why it is there.  You can delete it if you don't want to see the text.
0
 
Scott MadeiraCommented:
In test2.php add this statement at the top (line4 or so)

var_dump($_POST);

and let us know what is in the array you are getting from the form.  

Also, when the form is loaded in the browser, view the source and post the html generated for the <form> .....   </form> so that we can see how the HTML form actually renders.
0
 
paddy086Author Commented:
array(1) { ["name"]=> array(1) { ["
Notice: Undefined variable: i in C:\xampp\htdocs\test2.1.php on line 12
"]=> string(10) "****aaaaaa" } }
Notice: Array to string conversion in C:\xampp\htdocs\test2.php on line 24
query worked
Array ( [
Notice: Undefined variable: i in C:\xampp\htdocs\test2.1.php on line 12
] => ****aaaaaa ) No such record

source form browser HTML FORM
<html> 
<head><title>Delete Student Form</title></head> 

<body> 
<h2 align=center><font color=blue>Delete a Teacher Record</font> </h2><p> 

Enter the Name of the Teacher to be deleted:<br> 
<form action="test2.php" method="POST">  


DEL Teacher:  <select name="name[<br />
<b>Notice</b>:  Undefined variable: i in <b>C:\xampp\htdocs\test2.1.php</b> on line <b>12</b><br />
]"><option value='********'>********</option><option value='****aaaaaa'>****aaaaaa</option><option value='***Free***'>***Free***</option><option value='Boyd, E'>Boyd, E</option><option value='Boylan, P'>Boylan, P</option><option value='Brady, M'>Brady, M</option><option value='Brennan, H'>Brennan, H</option><option value='Byrne, N'>Byrne, N</option><option value='Callaghan, R'>Callaghan, R</option><option value='Cavanagh, S'>Cavanagh, S</option><option value='Cepeda, A'>Cepeda, A</option><option value='Coghlan,J'>Coghlan,J</option><option value='Connor, D'>Connor, D</option><option value='Curley, S'>Curley, S</option><option value='Daly, R'>Daly, R</option><option value='Delaney, B'>Delaney, B</option><option value='Dewberry, M'>Dewberry, M</option><option value='Dowdall, L'>Dowdall, L</option><option value='Dunne, S'>Dunne, S</option><option value='Farrell, M'>Farrell, M</option><option value='Ferns, S'>Ferns, S</option><option value='Fitzpatrick, M'>Fitzpatrick, M</option><option value='Foskin, F'>Foskin, F</option><option value='Hanley, K'>Hanley, K</option><option value='Harte, M'>Harte, M</option><option value='Hughes, P'>Hughes, P</option><option value='Kavanagh, C'>Kavanagh, C</option><option value='Keane, G'>Keane, G</option><option value='Kearns, J'>Kearns, J</option><option value='Keating, C'>Keating, C</option><option value='Kelly, R'>Kelly, R</option><option value='Lawlor, B'>Lawlor, B</option><option value='Madden, D'>Madden, D</option><option value='Maguire, O'>Maguire, O</option><option value='McMillan, C'>McMillan, C</option><option value='Moyne, R'>Moyne, R</option><option value='Ni Dhomhnaill, N'>Ni Dhomhnaill, N</option><option value='O'Brien, M'>O'Brien, M</option><option value='O'Connor, A'>O'Connor, A</option><option value='O'Connor, D'>O'Connor, D</option><option value='O'Donnell, M'>O'Donnell, M</option><option value='O'Grady, D'>O'Grady, D</option><option value='O'Keefe, O'>O'Keefe, O</option><option value='O'Kelly, P'>O'Kelly, P</option><option value='Sayers, C'>Sayers, C</option><option value='Scally, E'>Scally, E</option><option value='Ui Chiardha, M'>Ui Chiardha, M</option><option value='Vaughan, J'>Vaughan, J</option><option value='Wall, G'>Wall, G</option><option value='Wrynne, C'>Wrynne, C</option></select><br>

<input type="submit" value="Delete"> 
<input type="reset" value="Reset"> 

</form> 

</body> 
</html>

Open in new window

0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Scott MadeiraCommented:
your first problem is in test2.1.php on line 11.  You are trying to do name[$i] where $i should have a numeric value.

BUT, there is no reference to $i anywhere in your code.  It has no value and therefore generates the error for undefined variable.

Change the line to this:  

DEL Teacher:  <select name="name"><?php echo $options ?></select><br>

Then you should get to the point where you have data in the $_POST in test2.php
0
 
paddy086Author Commented:
Hay Smadeira that worked as fas as it deleted from my teacher name from the DB

JUST THIS POPS UP

array(1) { ["name"]=> string(5) "Test Teachername" } query worked
Test Teachername No such record
0
 
paddy086Author Commented:
Thank you Smadeira
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.