3 Replies - 5932 Views - Last Post: 06 August 2010 - 07:14 AM Rate Topic: -----

#1 kofo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-January 10

Dropdown insert value into database

Posted 05 August 2010 - 07:12 PM

hey guys pls can u help..
i'm trying to insert a value from a drop down box( whose options were gotten from a row from another table),into a row in another table
i have a form purchase order which wld choose a value from a drop down company name nd insert the value chosen into the company name row in the purchase order table.
how do i insert the option chosen into the row in purchaseorder
   <?php
$link = mysql_connect("localhost", "root", ".....");
mysql_select_db("new", $link);

$query = "select company_name FROM supplier";
$results = mysql_query($query, $link) or die("Error performing query");

if(mysql_num_rows($results) > 0){
echo("<select name=\"selectItem\">");
while($row = mysql_fetch_object($results)){
echo("<option value=\"$row->record_id\">$row->company_name</option>");
}
echo("</select>");
}
else{
echo("<i>No values found</i>");
}
?>


Is This A Good Question/Topic? 0
  • +

Replies To: Dropdown insert value into database

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3873
  • View blog
  • Posts: 11,408
  • Joined: 18-April 07

Re: Dropdown insert value into database

Posted 05 August 2010 - 07:35 PM

With the code you show there you should now have a listbox with your options. Good. This list is going to be part of a web form right? Well that form is going to submit its contents to another page and on that page you will have PHP code which will accept the form info, create an insert SQL statement based on the value chosen and then insert it into the table of your choice.

The user simply selects an option from the listbox and submits the form...

<?php
$link = mysql_connect("localhost", "root", ".....");
mysql_select_db("new", $link);

// Check if selected item was chosen and submitted through the form.
// Escape it and then insert into the companyname field of purchaseOrder table (along with any other fields).
if (isset($_POST["selectItem"))) {
   $selectedItem = mysql_real_escape_string($_POST["selectItem"]);
   $sql = "insert into purchaseOrder (companyname) values('$selectedItem')";
   mysql_query($sql);
}
?>



Hope you get the idea. You populate the form with your query, the use chooses the item from the drop down and hits submit. It submits to another page where you have PHP code to insert the value into the appropriate table.

Now if this is something automated, then your script would choose the value from the list and submit the form. You can do that from Javascript for instance.

Hope that helps you out. :)
Was This Post Helpful? 0
  • +
  • -

#3 kofo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-January 10

Re: Dropdown insert value into database

Posted 05 August 2010 - 08:38 PM

Actually its a form, this is what i have so far...
2 dropdown and then some text boxes
<?php
	session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Login Form</title>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<center>
  <p>
    <!-- ImageReady Slices (Untitled-1) -->
    <img src="images/Untitled-1.gif" width="459" height="340" alt="">
  </p>


	<form id="purchase" name="loginForm" method="post" action="pur-ord-exec.php">
  <table width="300" border="0" align="center" cellpadding="2" cellspacing="0">
		<table style="text-align:left;" cellpadding="2" cellspacing="0" border="0" bgcolor="#FFFFFF">
			<tr>
			
			</tr><tr>
      <th>date </th>
      <td><input name="date" type="text" class="textfield" id="date" /></td>
    </tr>
    <tr>
      <th>Details </th>
      <td><input name="Details" type="text" class="textfield" id="details" /></td>
    </tr>
    
            
              
  	
  
  <tr valign="top"><td style="" align="center"><font face="Verdana" size="2" color="#000000"><b>Text</b></font>
    <?php
$link = mysql_connect("localhost", "root", "09niyoweda11");
mysql_select_db("new", $link);

$query = "select company_name FROM supplier";
$results = mysql_query($query, $link) or die("Error performing query");

if(mysql_num_rows($results) > 0){
echo("<select name=\"selectItem\">");
while($row = mysql_fetch_object($results)){
echo("<option value=\"$row->record_id\">$row->company_name</option>");
}
echo("</select>");
}
else{
echo("<i>No values found</i>");
}
?>
</tr>
<tr valign="top"><td style="" align="center"><font face="Verdana" size="2" color="#000000"><b>Text</b></font>
				<span style="color:red;"><small></small></span>
                
                <?php
$link = mysql_connect("localhost", "root", "09niyoweda11");
mysql_select_db("new", $link);

$query = "select pro_name FROM product";
$results = mysql_query($query, $link) or die("Error performing query");

if(mysql_num_rows($results) > 0){
echo("<select name=\"selectItem\">");
while($row = mysql_fetch_object($results)){
echo("<option value=\"$row->record_id\">$row->pro_name</option>");
}
echo("</select>");
}
else{
echo("<i>No values found</i>");
}
?>
				</tr>
<tr>
      <th>Quantity</th>
      <td><input name="quantity" type="text" class="textfield" id="quantity" /></td>
    </tr>
            
            <tr>
      <th>price</th>
      <td><input name="total" type="text" class="textfield" id="total" /></td>
    </tr><tr>
      <th>total </th>
      <td><input name="price" type="text" class="textfield" id="price" /></td>
    </tr>
    <tr>
      <th>des </th>
      <td><input name="des" type="text" class="textfield" id="des" /></td>
    </tr>
		
		</tr><tr><td colspan="2" align="center"><input  value="Send email" type="submit" />

		</td></tr>
        </table>
		
	</form>

		
</body></html>




this is to execute after submitting
<?php
	//Start session
	session_start();
	
	//Include database connection details
	require_once('connect-db.php');
	
	//Array to store validation errors
	$errmsg_arr = array();
	
	//Validation error flag
	$errflag = false;
	
	//Connect to mysql server
	$link = mysql_connect($server, $user, $pass);
	if(!$link) {
		die('Failed to connect to server: ' . mysql_error());
	}	
	//Select database
	$db = mysql_select_db($db);
	if(!$db) {
		die("Unable to select database");
	}
	
	//Function to sanitize values received from the form. Prevents SQL injection
	function clean($str) {
		$str = @trim($str);
		if(get_magic_quotes_gpc()) {
			$str = stripslashes($str);
		}
		return mysql_real_escape_string($str);
	}
	//Sanitize the POST values
	$date = clean($_POST['date']);
	$details = clean($_POST['details']);
	$quantity = clean($_POST['quantity']);
	$total = clean($_POST['total']);
	$price = clean($_POST['price']);
	$supname = clean($_POST['supname']);
	$email = clean($_POST['des']);	
	$proname = clean($_POST['proname']);
	
	
	
	
	//Input Validations
	if($date == '') {
		$errmsg_arr[] = 'First name missing';
		$errflag = true;
	}
	if($details == '') {
		$errmsg_arr[] = 'Last name missing';
		$errflag = true;
	}
	if($quantity== '') {
		$errmsg_arr[] = 'Login ID missing';
		$errflag = true;
	}
	
	if($total == '') {
		$errmsg_arr[] = 'Login ID missing';
		$errflag = true;
	}
	if($price == '') {
		$errmsg_arr[] = 'Login ID missing';
		$errflag = true;
	}
	if($supname == '') {
		$errmsg_arr[] = 'Login ID missing';
		$errflag = true;
	
	}
	if($des == '') {
		$errmsg_arr[] = 'Login ID missing';
		$errflag = true;
	}
	if($proname == '') {
		$errmsg_arr[] = 'Login ID missing';
		$errflag = true;
	}
	//If there are input validations, redirect back to the registration form
	if($errflag) {
		$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
		session_write_close();
 header("location:purchaseo.php");
		exit();
	}

	//Create INSERT query
	$qry = "INSERT INTO purchaseorder(pur_order_id, p_ord_date, order_details, quantity_order, pur_amount, cost_price, comp_name, pro_des, pro_name) VALUES('$date','$details','$quantity','$total','$price','$supname','$des','$proname')";
	$result = @mysql_query($qry);
	
	//Check whether the query was successful or not
	if($result) {
header("Location:purchaseo.php");
		exit();
	}else {
		die("Query failed");
	}
?>


i understand how to get the values inserted into my database from the textbox, my problem is from the drop down, how i will make it execute using the new execute file..when i click submit, it does nothing.. meaning input wasnt validated :(
i hope u get what i mean

This post has been edited by kofo: 05 August 2010 - 09:15 PM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,529
  • Joined: 08-August 08

Re: Dropdown insert value into database

Posted 06 August 2010 - 07:14 AM

Perhaps if you shorten your code you'll have a better chance of seeing the problem. Here's how I'd do the validations:
$postarray = array("date", "details","quantity","total", "price", "supname", "des", "propname");
$messages = array('date' => 'Date missing', 'details' => 'Details missing','quantity' => 'Quantity missing','total' => 'Total missing', 'price' => 'Price missing', 'supname' => 'Supname missing', 'des' => 'Description missing', 'propname' => 'Proper name missing');
$errmsg_arr = array();
$clean_input = array();

foreach($postarray as $key)
{	
	if(isset($_POST[$key]))
	{
		$clean_input[$key] = clean($_POST[$key]);
		if($clean_input[$key] == '')
		{
			$errmsg_arr[] = $messages[$key];
		}
	} else
	{
		$errmsg_arr[] = $messages[$key];
	}
}

if(count($errmsg_arr)) {
	$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
	session_write_close();
	header("location:purchaseo.php");
	exit();
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1