Jump to content

Dymanic drop down box / insert


Angelojoseph17

Recommended Posts

Created a dynamic drop down box which pulls values from three columns from the database. However my insert function is not injecting the values in and i get an undefined index error.

html>

 

<div id = "form" align="center">

 

<h1> Create a repair Ticket </h1>

 

<?php

// Connects to Database

mysql_connect("localhost", "bla", "bla") or die(mysql_error());

mysql_select_db("bla bla") or die(mysql_error());

 

?>

 

<form>
<p> Choose the machine to be repaired: </p> 
<select>
<?php 
// This query selects the machine_id and name from the machine table and stores in the "result" variable. 
$sql="SELECT machine_id,description FROM machine";
$result =mysql_query($sql);
while ($data=mysql_fetch_assoc($result))
{
echo ("<option value=".$data['machine_id'].">". $data['description']."</option>"); 
?>
<?php } ?>
</select>

<p> Choose the engineer to be allocated: </p> 
<select>
<?php 
// This query selects engineer_id and name 
$sql="SELECT engineer_id,engineer_name FROM engineer";
$result =mysql_query($sql);
while ($data=mysql_fetch_assoc($result))
{
echo ("<option value=".$data['engineer_id'].">". $data['engineer_name']."</option>"); 
?>
<?php } ?>
</select>

<p> Choose the part to be allocated: </p> 
<select>
<?php 
// This query selects the part_number and description 
$sql="SELECT part_number,description FROM parts";
$result =mysql_query($sql);
while ($data=mysql_fetch_assoc($result))
{
echo ("<option value=".$data['part_number'].">". $data['description']."</option>"); 
?>
<?php } ?>
</select>		

<p> Reported By: <input type="text" name="reported_by" /> </p>

<p><b>Fault Description</b> </p>
<textarea cols="50" rows="4" name="fault_description" align="right" wrap="virtual"></textarea>

</form> 
<form action="insert.php" method="post">

<input type="Submit">

</div>   

 

 

<?php
ini_set("display_errors", "1");
error_reporting(E_ALL);
$con = mysql_connect("localhost","bla","bla");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("bla", $con);

$machine_id= $_POST['machine_id'];
$reportedby= $_POST['reported_by'];
$Date = date("d/m/y");
$fault_description =$_POST['fault_description'];
$repaired_by = $_POST['engineer_name'];
$part_used = $_POST['description'];

$sql="INSERT INTO repairs (machine_number,reported_by,repair_start_date,fault description,repaired_by,part_used)
VALUES
('".$machine_id."', '".$reportedby."','".$Date."','".$fault_description."','".$repairedby."','".$part_used."')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
mysql_close($con);


echo "You records have been updated";
?>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/216473-dymanic-drop-down-box-insert/
Share on other sites

I've added the select statements and it still creates that error message

 

<form>
<p> Choose the machine to be repaired: </p> 
<select name ="machine_id">
<?php 
// This query selects the machine_id and name from the machine table and stores in the "result" variable. 
$sql="SELECT machine_id,description FROM machine";
$result =mysql_query($sql);
while ($data=mysql_fetch_assoc($result))
{
echo ("<option value=".$data['machine_id'].">". $data['description']."</option>"); 
?>
<?php } ?>
</select> 

<p> Choose the engineer to be allocated: </p> 
<select name ="engineer_name">
<?php 
// This query selects engineer_id and name 
$sql="SELECT engineer_id,engineer_name FROM engineer";
$result =mysql_query($sql);
while ($data=mysql_fetch_assoc($result))
{
echo ("<option value=".$data['engineer_id'].">". $data['engineer_name']."</option>"); 
?>
<?php } ?>
</select>

<p> Choose the part to be allocated: </p> 
<select name ="description">
<?php 
// This query selects the part_number and description 
$sql="SELECT part_number,description FROM parts";
$result =mysql_query($sql);
while ($data=mysql_fetch_assoc($result))
{
echo ("<option value=".$data['part_number'].">". $data['description']."</option>"); 
?>
<?php } ?>
</select>		

<p> Reported By: <input type="text" name="reported_by" /> </p>

<p><b>Fault Description</b> </p>
<textarea cols="50" rows="4" name="fault_description" align="right" wrap="virtual"></textarea>

</form> 
<form action="insert.php" method="post">

<input type="Submit">

</div>   

Notice: Undefined index: machine_id in /var/www/engineering/insert.php on line 13 Notice: Undefined index: reported_by in /var/www/engineering/insert.php on line 14 Notice: Undefined index: fault_description in /var/www/engineering/insert.php on line 16 Notice: Undefined index: engineer_name in /var/www/engineering/insert.php on line 17 Notice: Undefined index: description in /var/www/engineering/insert.php on line 18 Notice: Undefined variable: repairedby in /var/www/engineering/insert.php on line 22 You records have been updated

some minor changes..

 

the form

<form action="insert.php" method="post">
<p> Choose the machine to be repaired: </p> 
<select name ="machine_id">
<?php 
// This query selects the machine_id and name from the machine table and stores in the "result" variable. 
$sql="SELECT machine_id,description FROM machine";
$result =mysql_query($sql);
while ($data=mysql_fetch_assoc($result)){
	?>
	<option value="<?PHP echo $data['machine_id']; ?>"><?PHP echo $data['description']; ?></option><br> 
	<?php
}
 ?>
</select> 

<p> Choose the engineer to be allocated: </p> 
<select name ="engineer_name">
<?php 
// This query selects engineer_id and name 
$sql="SELECT engineer_id,	engineer_name FROM engineer";
$result =mysql_query($sql);
while ($data=mysql_fetch_assoc($result)){
	?>
	<option value="<?PHP ech $data['engineer_id']; ?>"><?PHP echo $data['engineer_name']; ?></option><br>
	<?PHP
}
?>
</select>

<p> Choose the part to be allocated: </p> 
<select name ="description">
<?php 
// This query selects the part_number and description 
$sql="SELECT part_number,description FROM parts";
$result =mysql_query($sql);
while ($data=mysql_fetch_assoc($result)){
	?>
	<option value="<?PHP echo $data['part_number']; ?>"><?PHP echo $data['description']; ?></option><br>
	<?php 
}
</select>
<p> Reported By: <input type="text" name="reported_by" /> </p>
<p><b>Fault Description</b> </p>
<textarea cols="50" rows="4" name="fault_description" align="right" wrap="virtual"></textarea>
<br>
<input type="Submit">
</form> 

the insert

?php
ini_set("display_errors", "1");
error_reporting(E_ALL);
$con = mysql_connect("localhost","bla","bla");
if (!$con){
die('Could not connect: ' . mysql_error());
}

mysql_select_db("bla", $con);

$machine_id= $_POST['machine_id'];
$reportedby= $_POST['reported_by'];
$Date = date("d/m/y");
$fault_description =$_POST['fault_description'];
$repaired_by = $_POST['engineer_name'];
$part_used = $_POST['description'];


$sql="INSERT INTO repairs (machine_number, reported_by, repair_start_date, fault description, repaired_by, part_used) VALUES ('$machine_id', '$reportedby', '$Date', '$fault_description' , '$repairedby', '$part_used')";

$result = mysql_query($sql);

mysql_close($con);


echo "You records have been updated";
?>

thanks for that lightbear what is wierd is that it creates a new record on the database with empty fields when i press submit and it churns this error message out now.

 

Notice: Undefined index: part_number in /var/www/engineering/insert.php on line 16 You records have been updated

<html>

<div id = "form" align="center"> 

<h1> Create a repair Ticket </h1>	

<?php
// Connects to Database
mysql_connect("localhost", "angelo", "password") or die(mysql_error());
mysql_select_db("church_engineers") or die(mysql_error());

?>

<form action="insert.php" method="post">
<p> Choose the machine to be repaired: </p> 
<select name ="machine_id">
<?php 
// This query selects the machine_id and name from the machine table and stores in the "result" variable. 
$sql="SELECT machine_id,description FROM machine";
$result =mysql_query($sql);
while ($data=mysql_fetch_assoc($result)){
	?>
	<option value="<?PHP echo $data['machine_id']; ?>"><?PHP echo $data['description']; ?></option><br> 
	<?php
}
 ?>
</select> 

<p> Choose the engineer to be allocated: </p> 
<select name ="engineer_name">
<?php 
// This query selects engineer_id and name 
$sql="SELECT engineer_id,engineer_name FROM engineer";
$result =mysql_query($sql);
while ($data=mysql_fetch_assoc($result)){
	?>
	<option value="<?PHP echo $data['engineer_id']; ?>"><?PHP echo $data['engineer_name']; ?></option><br>
	<?PHP
}
?>
</select>

<p> Choose the part to be allocated: </p> 
<select name ="part_number">
<?php 
// This query selects engineer_id and name 
$sql="SELECT part_number,description FROM parts";
$result =mysql_query($sql);
while ($data=mysql_fetch_assoc($result)){
	?>
	<option value="<?PHP echo $data['description']; ?>"><?PHP echo $data['part_number']; ?></option><br>
	<?PHP
}
?>
</select>

<p> Reported By: <input type="text" name="reported_by" /> </p>
<p><b>Fault Description</b> </p>
<textarea cols="50" rows="4" name="fault_description" align="right" wrap="virtual"></textarea>
<br>
<input type="Submit">
</form> 

 

 

 

 

 

<?php
ini_set("display_errors", "1");
error_reporting(E_ALL);
$con = mysql_connect("localhost","angelo","password");
if (!$con){
die('Could not connect: ' . mysql_error());
}

mysql_select_db("church_engineers", $con);

$machine_id= $_POST['machine_id'];
$reportedby= $_POST['reported_by'];
$Date = date("d/m/y");
$fault_description =$_POST['fault_description'];
$repaired_by = $_POST['engineer_name'];
$part_used = $_POST['part_number'];


$sql="INSERT INTO repairs (machine_number, reported_by, repair_start_date, fault description, repaired_by, part_used) VALUES ('$machine_id', '$reportedby', '$Date', '$fault_description' , '$repaired_by', '$part_used')";

$result = mysql_query($sql);

mysql_close($con);


echo "You records have been updated";
?>

 

 

 

| Field            | Type        | Null | Key | Default            | Extra                                                                                                          |

+-------------------+--------------+------+-----+---------------------+---------                                                                                                -------+

| repair_id        | mediumint(9) | NO  | PRI | NULL                | auto_inc                                                                                                rement |

| machine_number    | varchar(100) | YES  | MUL | NULL                |                                                                                                                |

| reported_by      | varchar(100) | YES  |    | NULL                |                                                                                                                |

| repair_start_date | datetime    | NO  |    | 0000-00-00 00:00:00 |                                                                                                                |

| repair_end_date  | datetime    | NO  |    | 0000-00-00 00:00:00 |                                                                                                                |

| fault_description | varchar(100) | YES  |    | NULL                |                                                                                                                |

| repaired_by      | varchar(100) | YES  | MUL | NULL                |                                                                                                                |

| part_used        | mediumint(9) | YES  | MUL | NULL                |     

try changing this...

$sql="INSERT INTO repairs (machine_number, reported_by, repair_start_date, fault description, repaired_by, part_used) VALUES ('$machine_id', '$reportedby', '$Date', '$fault_description' , '$repaired_by', '$part_used')";

 

to this...

$sql="INSERT INTO repairs (machine_number, reported_by, repair_start_date, fault_description, repaired_by, part_used) VALUES ('$machine_id', '$reportedby', '$Date', '$fault_description' , '$repaired_by', '$part_used')";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.