add, edit, delete with a select option tag

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jive
Forum Newbie
Posts: 20
Joined: Thu May 23, 2002 1:34 pm
Contact:

add, edit, delete with a select option tag

Post by jive »

I have three pages that handle adding, updating and removing an aircraft from my mysql db. They are called newac_add.php, newac_edit.php, and newac_delete.php respectively. All three pages have the <select> dropdown box that has the ID, and the name of the aircraft category that the aircraft belongs to, but I'm having problem with my update page. My add page works perfectly. Here are my add and update pages:

newac_add.php (working perfectly)

Code: Select all

<?php
include ('connect.php');
$dbconnect = mysql_select_db('aircraftsales');
$actypelist = mysql_query('SELECT ID, type FROM ac_type');

?>
<form name="addaircraft" method="post" action="<?=$_SERVER['PHP_SELF']?>"
enctype="multipart/form-data">
Add aircraft to which aircraft type?:
<select name="typeID">
<option value="">Aircraft Type</option>
<option value="">- - - - - - - -</option>
<?php
while($actype = mysql_fetch_array($actypelist)) {
  $typeID = $actype['ID'];
  $type = $actype['type'];
  echo('<option value="' . $typeID . '">' . $type . "</option>\n" );
}
?>
</select>
Aircraft order: <input type="text" size="5" maxlength="10" name="description_order">
<br><br>
Aircraft Name: <input type="text" size="50" maxlength="100" name="ac_name">
<br>
Serial Number: <input type="text" size="50" maxlength="100" name="serial_num"><BR>
Registration Number: <input type="text"  size="50" maxlength="100" name="registration_num"><BR>
Engines<BR>
Left:<input type="text"  size="50" maxlength="100" name="L_engine"><BR>
Right:<input type="text"  size="50" maxlength="100" name="R_engine"><BR>
Propellers<br>
Left:<input type="text"  size="50" maxlength="100" name="L_propeller"><BR>
Right:<input type="text"  size="50" maxlength="100" name="R_propeller"><BR>
Center:<input type="text"  size="50" maxlength="100" name="C_propeller"><BR>
Interior:
<br>
<textarea cols="80" rows="10" name="interior"></textarea>
<script language="JavaScript1.2" defer>
editor_generate('topic_text');
</script>
<br><br>
Exterior<br>
<textarea cols="80" rows="10" name="exterior"></textarea><br>
Optional Equipment:<br>
<textarea cols="80" rows="10" name="optional_equip"></textarea><br>
Avionics:<br>
<textarea cols="80" rows="10" name="avionics"></textarea>

<br>
Exterior Image: <input type="file" name="img_exterior"> <br>
Cockpit Image: <input type="file" name="img_cockpit"> <br>
Interior Image: <input type="file" name="img_interior"> <br>




<input type="submit" value="submit" name="submit">
</form>


<?php

if(isset($_POST['submit'])) {
  $description_order = (int) $_POST['description_order'];	
  $typeID = (int) $_POST['typeID']; 
  $ac_name = addslashes($_POST['ac_name']);
  $serial_num = addslashes($_POST['serial_num']);
  $registration_num = addslashes($_POST['registration_num']);
  $L_engine = addslashes($_POST['L_engine']);
  $R_engine = addslashes($_POST['R_engine']);
  $L_propeller = addslashes($_POST['L_propeller']);
  $R_propeller = addslashes($_POST['R_propeller']);
  $C_propeller = addslashes($_POST['registration_num']);
  $interior = addslashes($_POST['interior']);
  $exterior = addslashes($_POST['exterior']);
  $optional_equip = addslashes($_POST['optional_equip']);
  $avionics = addslashes($_POST['avionics']);

//confused somewhere here.  		
if ($_FILES['img_exterior']['type'] == "image/gif" or $_FILES['img_exterior']['type'] == "image/pjpeg"){ 
move_uploaded_file($_FILES['img_exterior']['tmp_name'], "images/".$_FILES['img_exterior']['name']) 
    or die ("Could not copy"); }
else { 
          
            echo "Could Not Copy, Wrong Filetype (".$_FILES['img_exterior']['name'].")"; 
			
        } 
//} 			
//interior upload
if ($_FILES['img_interior']['type'] == "image/gif" or $_FILES['img_interior']['type'] == "image/pjpeg"){ 
move_uploaded_file($_FILES['img_interior']['tmp_name'], "images/".$_FILES['img_interior']['name']) 
    or die ("Could not copy"); }
else { 
          
            echo "Could Not Copy, Wrong Filetype (".$_FILES['img_interior']['name'].")"; 
			
        } 
//} 			

if ($_FILES['img_cockpit']['type'] == "image/gif" or $_FILES['img_cockpit']['type'] == "image/pjpeg"){ 
move_uploaded_file($_FILES['img_cockpit']['tmp_name'], "images/".$_FILES['img_cockpit']['name']) 
    or die ("Could not copy"); }
else { 
          
            echo "Could Not Copy, Wrong Filetype (".$_FILES['img_cockpit']['name'].")"; 
			
        } 
//} 			




  echo($actype_ID);
  $sql = 'INSERT INTO ac_description SET
  		description_order = '''. $description_order . ''',
        actype_ID = ''' . $typeID . ''',
        ac_name = ''' . $ac_name . ''',
        serial_num = ''' . $serial_num . ''',
		registration_num = ''' . $registration_num . ''',
        L_engine = ''' . $L_engine . ''',
        R_engine = ''' . $R_engine . ''',
		L_propeller = ''' . $L_propeller . ''',
        R_propeller = ''' . $R_propeller . ''',
		C_propeller = ''' . $C_propeller . ''',
		interior = ''' . $interior . ''',
        exterior = ''' . $exterior . ''',
		optional_equip = ''' . $optional_equip . ''',
		avionics = ''' . $avionics . ''',
		img_exterior = ''' . $_FILES['img_exterior']['name'] . ''', 
        img_interior = ''' . $_FILES['img_interior']['name'] . ''',
		img_cockpit = ''' . $_FILES['img_cockpit']['name'] . '''';
		




		
		
  if (@mysql_query($sql)) {
    echo('The new aircraft has been added ');
  } else {
    echo('could not add new aircraft:' . mysql_error());
  }
}
?>
The way the select box works in the newac_add page is that the typeID and type is pulled from a table called ac_type. When the ac type is selected, the ID of that ac type is entered in a different table (ac_description) into a field called actype_ID. (joining the two).



newac_edit.php (page I'm having problems with)

Code: Select all

<?php

$type = $_GET['type'];
$ac_desc_ID = $_GET['ac_desc_ID'];

//echo($type);
//echo($ac_desc_ID);
include ("connect.php");
echo("Edit from the <b> $type </b>aircraft type ");

$aircraftlist = mysql_query("SELECT * FROM ac_description WHERE ID='$ac_desc_ID'");

$aircraft = mysql_fetch_array($aircraftlist);
  $description_order = $aircraft['description_order'];	
  $ac_desc_ID = $aircraft['ac_desc_ID']; 
  $ac_name = $aircraft['ac_name'];
  //$serial_num = $aircraft['serial_num'];
  $registration_num = $aircraft['registration_num'];
  $L_engine = $aircraft['L_engine'];
  $R_engine = $aircraft['R_engine'];
  $L_propeller = $aircraft['L_propeller'];
  $R_propeller = $aircraft['R_propeller'];
  $C_propeller = $aircraft['registration_num'];
  $interior = $aircraft['interior'];
  $exterior = $aircraft['exterior'];
  $optional_equip = $aircraft['optional_equip'];
  $avionics = $aircraft['avionics'];


?>




<form name="editaircraft" method="post" action="<?=$_SERVER['PHP_SELF']?>" enctype="multipart/form-data">

Add aircraft to which aircraft type?:


<select name="typeID">
<option value="">Aircraft Type</option>
<option value="">- - - - - - - -</option>
<?php 
//problems here....
$actypelist = mysql_query("Select * from ac_type where ID='$ac_desc_ID' AND type='$type'"); 
while($actype = mysql_fetch_array($actypelist)) { 
  $typeID = $actype['ID']; 
  $type = $actype['type']; 
  echo 
   ' 
<option value="'.$typeID.'">'.$type.'</option> 
   '; 
} 
?> 


</select>
Aircraft order: <input type="text" size="5" maxlength="10" name="description_order" value="<?=$description_order?>">
<br><br>
Aircraft Name: <input type="text" size="50" maxlength="100" name="ac_name" value="<?=$ac_name?>">
<br>
Serial Number: <input type="text" size="50" maxlength="100" name="serial_num" value="<?=$serial_num?>"><BR>
Registration Number: <input type="text"  size="50" maxlength="100" name="registration_num" value="<?=$registration_num?>"><BR>
Engines<BR>
Left:<input type="text"  size="50" maxlength="100" name="L_engine" value="<?=$L_engine?>"><BR>
Right:<input type="text"  size="50" maxlength="100" name="R_engine" value="<?=$R_engine?>"><BR>
Propellers<br>
Left:<input type="text"  size="50" maxlength="100" name="L_propeller" value="<?=$L_propeller?>"><BR>
Right:<input type="text"  size="50" maxlength="100" name="R_propeller" value="<?=$R_propeller?>"><BR>
Center:<input type="text"  size="50" maxlength="100" name="C_propeller" value="<?=$C_propeller?>"><BR>
Interior:
<br>
<textarea cols="80" rows="10" name="interior"><?=$interior?></textarea>
<script language="JavaScript1.2" defer>
editor_generate('topic_text');
</script>
<br><br>
Exterior<br>
<textarea cols="80" rows="10" name="exterior"><?=$exterior?></textarea><br>
Optional Equipment:<br>
<textarea cols="80" rows="10" name="optional_equip"><?=$optional_equip?></textarea><br>
Avionics:<br>
<textarea cols="80" rows="10" name="avionics"><?=$avionics?></textarea>

<br>
Exterior Image: <input type="file" name="img_exterior" > <br>
Cockpit Image: <input type="file" name="img_cockpit"> <br>
Interior Image: <input type="file" name="img_interior"> <br>







<input type="hidden" name="ac_desc_ID" value="<?=$ac_desc_ID?>">
<input type="submit" value="submit" name="edit_aircraft">
</form>


<?php




if(isset($_POST['edit_aircraft'])) {
 $description_order = (int) $_POST['description_order'];	
  $typeID = (int) $_POST['typeID']; 
  $ac_name = addslashes($_POST['ac_name']);
  $serial_num = addslashes($_POST['serial_num']);
  $registration_num = addslashes($_POST['registration_num']);
  $L_engine = addslashes($_POST['L_engine']);
  $R_engine = addslashes($_POST['R_engine']);
  $L_propeller = addslashes($_POST['L_propeller']);
  $R_propeller = addslashes($_POST['R_propeller']);
  $C_propeller = addslashes($_POST['registration_num']);
  $interior = addslashes($_POST['interior']);
  $exterior = addslashes($_POST['exterior']);
  $optional_equip = addslashes($_POST['optional_equip']);
  $avionics = addslashes($_POST['avionics']);

//Update photos if any.  		
if ($_FILES['img_exterior']['type'] == "image/gif" or $_FILES['img_exterior']['type'] == "image/pjpeg"){ 
move_uploaded_file($_FILES['img_exterior']['tmp_name'], "images/".$_FILES['img_exterior']['name']) 
    or die ("Could not copy"); }
else { 
          
            echo "Could Not Copy, Wrong Filetype (".$_FILES['img_exterior']['name'].")"; 
			
        } 
//} 			
//interior upload
if ($_FILES['img_interior']['type'] == "image/gif" or $_FILES['img_interior']['type'] == "image/pjpeg"){ 
move_uploaded_file($_FILES['img_interior']['tmp_name'], "images/".$_FILES['img_interior']['name']) 
    or die ("Could not copy"); }
else { 
          
            echo "Could Not Copy, Wrong Filetype (".$_FILES['img_interior']['name'].")"; 
			
        } 
//} 			

if ($_FILES['img_cockpit']['type'] == "image/gif" or $_FILES['img_cockpit']['type'] == "image/pjpeg"){ 
move_uploaded_file($_FILES['img_cockpit']['tmp_name'], "images/".$_FILES['img_cockpit']['name']) 
    or die ("Could not copy"); }
else { 
          
            echo "Could Not Copy, Wrong Filetype (".$_FILES['img_cockpit']['name'].")"; 
			
        } 





$sql = 'UPDATE ac_description SET
  		description_order = '''. $description_order . ''',
        actype_ID = ''' . $typeID . ''',
        ac_name = ''' . $ac_name . ''',
        serial_num = ''' . $serial_num . ''',
		registration_num = ''' . $registration_num . ''',
        L_engine = ''' . $L_engine . ''',
        R_engine = ''' . $R_engine . ''',
		L_propeller = ''' . $L_propeller . ''',
        R_propeller = ''' . $R_propeller . ''',
		C_propeller = ''' . $C_propeller . ''',
		interior = ''' . $interior . ''',
        exterior = ''' . $exterior . ''',
		optional_equip = ''' . $optional_equip . ''',
		avionics = ''' . $avionics . ''',
		img_exterior = ''' . $_FILES['img_exterior']['name'] . ''', 
        img_interior = ''' . $_FILES['img_interior']['name'] . ''',
		img_cockpit = ''' . $_FILES['img_cockpit']['name'] . ''',
		WHERE ID = "$ac_desc_ID"';
		
		
		
if(mysql_query($sql)) {
echo('The chapter has been edited');
}
else {
echo('error editing chapter:' . mysql_error());
}
}
?>
I'm having problems with the select option area...can anybody see what I'm doing wrong??
ghost007
Forum Commoner
Posts: 49
Joined: Sat Nov 22, 2003 10:10 am

Post by ghost007 »

hi,

I think you made a mistake in naming your type var in html and php:

PHP:

Code: Select all

<?php
$type = $_GET['type']; 

?>
HTML:
<select name="typeID"> ...
===
=> this should be: <select name="type">


hope this helps
siech
jive
Forum Newbie
Posts: 20
Joined: Thu May 23, 2002 1:34 pm
Contact:

Post by jive »

I appreciate your help :). That doesn't seem to work. The data is not showing up in my drop down menu. :( . I've been toiling over this for a day now. If there is more info I can post to help out ....

I'm starting to feel a little beat up now.. :x
Post Reply