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.....
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']);}?>
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; } ?>
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
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.
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
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.
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.