Solved

SQL insert from values by id

Posted on 2012-04-12
11
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

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 83

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
 
LVL 110

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 83

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 110

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 110

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP: Best way to scan folders and process files 10 60
PHP mail() function not working... 8 33
Survey branching tutorial 11 39
Why is my $_POST not going to results page 10 36
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

756 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