Link to home
Start Free TrialLog in
Avatar of jws2bay
jws2bay

asked on

SQL insert from values by id

I'm working in mysql/php.  I want to do a sql insert, but I want to pull data from the form by the id tags.

I am doing something like this:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO Products (Part_no, Description, Base_Price, Weight, Height, `Depth`) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['Part_no'], "text"),
             GetSQLValueString($_POST['txtHint'], "text"),
                       GetSQLValueString($_POST['Total'], "double"),
                       GetSQLValueString($_POST['ship_Wt'], "text"),
                       GetSQLValueString($_POST['Height'], "text"),
                       GetSQLValueString($_POST['Depth'], "text"));

  mysql_select_db($database_gdsquirrelcart, $gdsquirrelcart);
  $Result1 = mysql_query($insertSQL, $gdsquirrelcart) or die(mysql_error());

This doesn't work.  In the above example 'txtHint' is an id tag in the form. I want to do something like getvaluebyid(), but don't know what the php syntax would be.
Avatar of bartvd
bartvd
Flag of Netherlands image

The values are placed in the POST array based on their names, you need to add a name to the field with id txtHint.
Please post the HTML form that sends the data to this PHP script, thanks.
Avatar of jws2bay
jws2bay

ASKER

The page I am working on is used to define a product.  The customer builds the part number by making selections from pull-downs and radio buttons.  As the part number is modified I am using Ajax to build the product description.  At the end of this process the customer is able to insert the product information into my products table, and then go into the store and purchase the item.  I want to be able to write the product description which was placed on the page using id tags into my product table.  Here is the link to the page I am working on. I am still sorting out the format.

http://www.glassdivider.com/W2-Std-Prod-1_newA.php?Series=CRY140-1
My sense is that you're working with several technologies at once and they are interacting in counterproductive ways.  The way I would build this application would go something like this...

First, establish a data base that contains all of the options for each aspect of the thing the client is building.  Load the data base and be sure that all the elements are in the right place in the right tables. Shield height, supports, corners, ends - get all of the allowable values in the data base and carefully verify that they are correct.

Next create a GET-method form and action script that can access all the allowable fields.  By using GET method, you can test the scripts you are about to build by simply changing the URL parameters.  Eventually you will want to change the script to the POST method, but for debugging, GET is very helpful.  You can do something like this in the top of your action script, then it will be easy to convert the GET requests to the POST requests when it is time to put the script into production.
// CHANGE THIS TO USE $_POST FOR PRODUCTION WORK
$req = $_GET;
// SUBSEQUENT ACCESSES TO CLIENT INPUT REFER TO $req

Open in new window

Since you are dealing with external client input you will want your script to verify each and every input against a list of allowable values (those are kept in your data base).  Write the validation code and test it using the GET method requests.  Test both valid and invalid inputs for all of the different elements of the external input.  Be careful that your script ignores any input not contained in the acceptable list of information kept on your server.  That means things like SELECT * or extract($_POST) are off limits.  Use var_dump() to print out the external input and the created information (model numbers) and check to see that the outputs make sense for the inputs.  You may want to have an automated "build-and-test" process for this part of the work.

Once you know that your script is handling expected and unexpected inputs with accuracy, you are ready to move on to the next step, which is building the client experience, probably by using jQuery or a similar AJAX technology.  

My executive summary - do this in parts, not trying to do it all at the same time.  You will be happier with both the process and outcome if you take incremental steps toward your ultimate goal. The first step is to get the server side of things correct.  Next get the form-to-action communication correct.  Finally, get the client experience to be engaging, descriptive and perfect.
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jws2bay

ASKER

DaveBaldwin / Ray Paseur - Thank you for your answers.  Yes I am struggeling to use Ajax.  I was trying to build the table along with the cell content, and finding it hard to get the format to be what I wanted.  In that my overall understanding is weak I think I should ask for advice on how to approch the real problem instead of bits and pieces.


I have in my mysql products table I have all of the individual part_no for the parts which can be selected in any combination to comprise the larger final product.  All of the different combinations of parts and dimensions is a huge number making it impractical to list individually in the products table.  This page is used by my customers to define their final product. I want to display brief_ discriptions of the selections on the page and then insert them into my products table so the the final product can be purchased. I want the look on the page to be the same as it will look in the cart in the store.

Approach 1:

I have been trying to use Ajax to fetch a brief_description of the componets being selected.  First I wanted to put the brief_description into a table on the page for display to the user,  then I was going to insert it into my mysql products table so the customer could go through the store a purchase the customized product.

Approach 2:

I know the product-line when the page is loaded, so I can pull in all the sub_product data on page load.  Would it be easier to pull from this recordset based on the part_no?
I was thinking about assigning a hidden field the "brief_description" as the customer makes selections, so they can be inserted into the products table.


It is the first time I am trying to use Ajax & innerHTML, so I'm having a real learning experience.  Any addition advice/help is greatly appreciated.

Thanks
I would first make a non-AJAX version of the pages to make sure the basic functions are understood.  I have often put up a 'form page' and a 'response page' and keep going back and forth between the two until I have it all working.  When it works properly, then you will know what has to be done and it will be easier to convert it to an AJAX page that does the same thing in a 'friendlier' manner.
What DaveBaldwin said!
Avatar of jws2bay

ASKER

Thanks for the help guys.  It always comes down to doing things in baby steps.
Couldn't agree more about the baby steps.  I rarely write more than 3 or 4 lines of code without testing my little code block.  I probably spend more time testing than any of my colleagues.  What I find is that time spent in the baby steps is inexpensive, whereas debugging a large piece of code takes a lot more effort, time and money!

thanks for the points, ~Ray