Jump to content

[SOLVED] Getting user's $_POST data


Schlo_50

Recommended Posts

Hi guys,

 

I am trying to create an order form which collects an order number and quantity. A checkbox is

 

used to select an item and a textbox is for the user to enter their required quantity.

 

Below is a picture of the order form layout.

 

40266340du7.jpg

 

The following code echo's out the database items and order form:

 

echo "	
<p class=main2>

<strong>{$ProductName}</strong> - 

({$Type} , {$Habit} , {$Colour} , {$Status}, Item#{$ProductId}) 

<span class=address1>£{$Price} <input name=\"orderid[]\" value=\"{$ProductId}\" 
type=\"checkbox\"/> <input name=\"quantity[{$ProductId}]\" type=\"text\" size=\"3\" 
maxlength=\"3\" /></span>

</p>
";

 

 

What i am trying to do is get both the selected $ProductId's and $quantity's into my database. The

 

selected items and quantities need to be inputed into the database as an array separated by

 

commas.

 

48741538ex7.jpg

 

Code so far:

<?php
if ($_POST[submit] == "Submit")
{

  $orderid = array();
  $quantity = array();
  foreach($_POST['orderid'] as $id){
    $orderid[] = $id;
    $quantity[] = $_POST['quantity'][$id];
  }
  $orderid = implode(',',$orderid);
  $quantity = implode(',',$quantity);
  }
  
  

$sql = "INSERT INTO Order1 " . "(ProductNotes,CustomerName,OrderDate,OrderNotes,Quantity) VALUES 

('$value','$cust_name','$date','$orderid','$quantity')";
?>

 

This however, doesn't actually add anything into my database.

 

Any help would be appreciated. Thanks

Link to comment
Share on other sites

<?php
include('odbc/odbc.php');
require_once('operate.php');
session_start();
// check to see if user is logged in
checkUser();

$cust_name = $_SESSION['userName'];
?><head>
<title>W D Smith & Sons</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<style type="text/css"></style>
</head>




<form name="the_form" id="the_form" method="post" action="<?php $_SERVER[php_SELF]; ?>">
<?php

// connect to access database and find the table 'Product1'
$sql2 = odbc_exec($odbc, "SELECT * FROM Product1 ORDER BY CategoryName") or die (odbc_errormsg());


// define variables
$prevCat='';
while($row = odbc_fetch_array($sql2))
{
$Category = $row["CategoryName"];
$ProductId = $row["ProductId"];
$ProductName = $row["ProductName"];
$Price = $row["Price"];
$Type = $row["Type"];
$Habit = $row["Habit"];
$Colour = $row["Colour"];
$Price = $row["Price"];
$Status = $row["Status"];

// find out if category has changed
// if so, print it, and it's contents
if ($Category != $prevCat)  {
	echo "<p class=title><u>$Category</u></p>";
}
echo  "	
<p class=main2>
<strong>{$ProductName}</strong> - 
({$Type} , {$Habit} , {$Colour} , {$Status}, Item#{$ProductId}) 
<span class=address1>£{$Price} <input name=\"orderid[]\" value=\"{$ProductId}\" type=\"checkbox\"/> <input name=\"quantity[{$ProductId}]\" type=\"text\" size=\"3\" maxlength=\"3\" /></span>
</p>
";	

$prevCat = $Category;
}
?>
<hr />

<div align="center">
  <p align="left"><span class="main"><Br />
    </span><span class="title">Order Quantity-</span><span class="main"> If you would like to order more than one batch of any of the above, please state in the textbox below the Item# and quantity you wish to order in the format shown:</span> <span class="main">(If you do not wish to order more than one of any item do not fill in the textbox below.)</span><br />
    <br />
    <textarea name="ProductNotes" cols="70" rows="10">1 of Selected</textarea>
     <img src="images/eg.jpg" /><br />
     <br />
     <input name="submit" type="Submit" value="Submit" />
    </p>
  </p>
</div>
</form>

<?php
if ($_POST[submit] == "Submit")
{

  $orderid = array();
  $quantity = array();
  foreach($_POST['orderid'] as $id){
    $orderid[] = $id;
    $quantity[] = $_POST['quantity'][$id];
  }
  $orderid = implode(',',$orderid);
  $quantity = implode(',',$quantity);
  }
  
  
?>

<?php
$conn = odbc_connect('CentralProduct', 'root', '') or die('Could not Connect to ODBC Database!');

$date = date('d m y');
$notes = htmlspecialchars($_POST['ProductNotes']);



// execute SQL Statement
$sql = "INSERT INTO Order1 " . "(ProductNotes,CustomerName,OrderDate,OrderNotes,Quantity) VALUES ('$value','$cust_name','$date','$orderid','$quantity')";


$rs = @odbc_exec($conn,$sql);

if (!$rs)
{

echo "";

}

else

{

echo "<span class=\"main\">The record was successfully inserted.</span>";

}
odbc_close($conn);
?>

Link to comment
Share on other sites

My post highlights coding mistakes amongst others, though I cannot say the script will "run". I wanted to point out the problems in your script so that you can learn from your mistakes rather then post a script were you learn nothing.

 

<?php
session_start(); // Always put this at the top of your script were possible
include_once('odbc/odbc.php'); // use the include_once statement rather then include
require_once('operate.php');

// check to see if user is logged in
checkUser();

$cust_name = $_SESSION['userName'];
$recordupdate = ""; // New variable for your update information

/**
* Copied your submit test from below (though I do not like the original approach you have used
* so I have slightly updated it, always perform a check before output, isset() will check to see if the
* post variable exists
*/
if (isset($_POST['update']) && ($_POST['update']==="upd")) {

  $orderid = array();
  $quantity = array();
  
  foreach($_POST['orderid'] as $id){
    $orderid[] = $id;
    $quantity[] = $_POST['quantity'][$id];
  }
  
  $orderid = implode(',', $orderid);
  $quantity = implode(',', $quantity);
  
  // Below code is copied from the bottom of your original document
  
  	// Why are you reconnecting to the database here? Surely you already have an open connection above?
//$conn = odbc_connect('CentralProduct', 'root', '') or die('Could not Connect to ODBC Database!');

$date = date('d m y');
$notes = htmlspecialchars($_POST['ProductNotes']); // Good, glad to see some validation, read up on the, 
// function your using it has extra attributes you could use here

// execute SQL Statement

// Access SQL is rather funny in its syntax all field and table names in an insert must be bracketed, 
// plus you do not need to concatenate your sql string and spacing values out makes it easier to read
//$sql = "INSERT INTO Order1 " . "(ProductNotes,CustomerName,OrderDate,OrderNotes,Quantity) VALUES ('$value','$cust_name','$date','$orderid','$quantity')";
$sql = "INSERT INTO [Order1] ([ProductNotes], [ContactName], [OrderDate], [OrderNotes], [Quantity]) VALUES ('$value', '$cust_name', '$date', '$orderid', '$quantity')";

// As $conn has been removed, just use your original connection variable.
$rs = odbc_exec($conn, $sql);

/*
All wrong

if (!$rs) {
	echo ""; //this does nothing
} else {
	echo "<span class=\"main\">The record was successfully inserted.</span>";
}*/

if ($rs===false) {
	$recordupdate = "<span class=\"main\">The record was successfully inserted.</span>"; 
}  
  
}

// You forgot the HTML tag, I won't go any further about DTD tags etc but you should look them up
?>
<html>
<head>
<title>W D Smith & Sons</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<style type="text/css"></style>
</head>
<form name="the_form" id="the_form" method="post" action="<?php $_SERVER[php_SELF]; ?>">

<?php
// connect to access database and find the table 'Product1'
$sql2 = odbc_exec($odbc, "SELECT * FROM Product1 ORDER BY CategoryName") or die (odbc_errormsg());

// define variables - use lowercase for variables names
$prevcat = "";


while($row = odbc_fetch_array($sql2))
{
$category = $row["CategoryName"];
$productid = $row["ProductId"];
$productname = $row["ProductName"];
$price = $row["Price"];
$type = $row["Type"];
$habit = $row["Habit"];
$colour = $row["Colour"];
$price = $row["Price"];
$status = $row["Status"];

// find out if category has changed
// if so, print it, and it's contents
if ($category != $prevcat)  {
	echo "<p class='title'><u>{$category}</u></p>"; // try to surround html attributes with quotes
}

echo  "<p class=main2>
<strong>{$productName}</strong> - 
({$type} , {$habit} , {$colour} , {$status}, Item#{$productid}) 
<span class=address1>£{$price} <input name=\"orderid[]\" value=\"{$productid}\" type=\"checkbox\"/> 
<input name=\"quantity[{$productid}]\" type=\"text\" size=\"3\" maxlength=\"3\" /></span></p>";	

$prevcat = $category;
}
?>
<hr />
<div align="center">
  <p align="left"><span class="main"><Br />
    </span><span class="title">Order Quantity-</span><span class="main"> If you would like to order more than one batch of any of the above, please state in the textbox below the Item# and quantity you wish to order in the format shown:</span> <span class="main">(If you do not wish to order more than one of any item do not fill in the textbox below.)</span><br />
    <br />
    <textarea name="ProductNotes" cols="70" rows="10">1 of Selected</textarea>
     <img src="images/eg.jpg" /><br />
     <br />
     <? echo $recordupdate; ?>
     <input name="update" type="submit" value="upd" />
    </p>
  </p>
</div>
</form>
</body>
</html>
<?php
// Close your body and html tags

// Testing your form has been submitted here is a waste of time.
/*
if ($_POST[submit] == "Submit") {

  $orderid = array();
  $quantity = array();
  foreach($_POST['orderid'] as $id){
    $orderid[] = $id;
    $quantity[] = $_POST['quantity'][$id];
  }
  $orderid = implode(',',$orderid);
  $quantity = implode(',',$quantity);
  }

There is no need to close then reopen PHP tags here

?>
<?php

*/
odbc_close($conn);
?>

Link to comment
Share on other sites

i've read your commented version, and just run it.

 

Now for debugging! i've not seen these error messages before?

 

Warning: Invalid argument supplied for foreach() in C:\apachefriends\xampp\htdocs\wdlayout\products.php on line 22

 

Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC Microsoft Access Driver] Field 'Order1.OrderNotes' cannot be a zero-length string., SQL state S1000 in SQLExecDirect in C:\apachefriends\xampp\htdocs\wdlayout\products.php on line 47

Link to comment
Share on other sites

I am unable to modify the post but I made a slight cut and paste error when posting,

 

at line 124 I removed some doubled php tags

</html>
<?php
// Close your body and html tags

// Testing your form has been submitted here is a waste of time.

odbc_close($conn);
?>

Link to comment
Share on other sites

The first error,

 

if (isset($_POST['orderid'])) {
// do some sort of validation here
if (is_array($_POST['orderid']) {
$data = $_POST['orderid'];

foreach($data as $id){
    $orderid[] = $id;
    $quantity[] = $_POST['quantity'][$id];
}
} else {
// return some error message here
}
}

 

The zero length string refers to an Access issue, text fields in Access tables can be set with a "do not allow zero length" attribute, remove this in Access.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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