Solved

SQL insert from values by id

Posted on 2012-04-12
11
202 Views
Last Modified: 2012-04-26
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.
0
Comment
Question by:jws2bay
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 6

Expert Comment

by:bartvd
ID: 37839558
The values are placed in the POST array based on their names, you need to add a name to the field with id txtHint.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37842763
Please post the HTML form that sends the data to this PHP script, thanks.
0
 

Author Comment

by:jws2bay
ID: 37846726
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
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37846786
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.
0
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 200 total points
ID: 37847811
Since txtHint is a <div> and not a tag used for input in a form, you can't get anything from it by submitting a form.  There is a table for the innerHTML.  Are you wanting to POST that entire table?

<div class="style14" id="txtHint" name="txtHint">
<table class="style14">
<tbody><tr>
<th>Model #   C140_n20cc12S</th>
</tr></tbody></table><table class="style12"><tbody><tr><td>     </td></tr></tbody></table><table class="style12"><tbody><tr><td>     </td></tr></tbody></table><table class="style12"><tbody><tr><td>     </td></tr></tbody></table><table class="style12"><tbody><tr><td>     </td></tr></tbody></table><table class="style12"><tbody><tr><td>     </td></tr></tbody></table><table class="style12"><tbody><tr><td>     </td></tr></tbody></table><table class="style12"><tbody><tr><td>     </td></tr></tbody></table><table class="style12"><tbody><tr><td>     </td></tr></tbody></table><table class="style12"><tbody><tr><td>     </td></tr></tbody></table><table class="style12"><tbody><tr><td>     </td></tr></tbody></table><table class="style12"><tbody><tr><td>     </td></tr></tbody></table><table class="style12"><tbody><tr><td>     </td></tr></tbody></table><table class="style12"><tbody><tr><td>     </td></tr></tbody></table><table class="style12"><tbody><tr><td>     </td></tr></tbody></table><table class="style12"><tbody><tr><td>     </td></tr></tbody></table><table class="style12"><tbody><tr><td>     </td></tr></tbody></table><table class="style12"><tbody><tr><td>     </td></tr></tbody></table><table class="style12"><tbody><tr><td>     </td></tr></tbody>

Open in new window

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 300 total points
ID: 37848155
DaveBaldwin makes a good point.  It looks like there may be some confusion about what happens on the client (browser) side of things vs what happens on the server (PHP) side of things.  

HTML forms must have <input> tags for data to be submitted.  The <input> tags must have the name attribute for the action script to find the data.  To see how this works, please install this script on your server and run it.  It is simplified and uses data visualization with var_dump() to show you what is going on.  Note that the id= attributes do not come through in the request.

http://www.laprbass.com/RAY_temp_jws2bay.php
<?php // RAY_temp_jws2bay.php
error_reporting(E_ALL);

// MAKE var_dump() OUTPUT EASIER TO READ
echo "<pre>";


// SHOW THE DATA IN THE HTTP REQUEST
var_dump($_GET);


// CREATE THE HTML FOR USING HEREDOC NOTATION
$form = <<<FORM
<form>
THIS INPUT HAS NO NAME ATTRIBUTE BUT HAS AN ID ATTRIBUTE
<input type="text" value="NONAME" id="txtHint" />
THIS INPUT HAS A NAME (Fatso) BUT NO ID
<input name="Fatso" />
THIS INPUT HAS A NAME (Skinny) AND AN ID (Fimmle)
<input name="Skinny" id="Fimmle" />
THIS INPUT HAS THE TYPE=SUBMIT ATTRIBUTE BUT NO NAME
<input type="submit" value="Click Me" />
</form>
FORM;

echo $form;

Open in new window

0
 

Author Comment

by:jws2bay
ID: 37857469
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
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 37857527
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37858710
What DaveBaldwin said!
0
 

Author Closing Comment

by:jws2bay
ID: 37898456
Thanks for the help guys.  It always comes down to doing things in baby steps.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37898769
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
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now