473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Decoding the query string array

I am building a very simple form that takes text and inserts data in a
MySQL db.

I would like my "logic" to simply insert the value in to the field in
the database that matches the name from the query string.

Here's the question- can I do this if I do not know how many name/value
pairs are passed to the "logic" ? Can I just step through the $_POST
array and use a do while $i <= count($_POST) or something?

I have tried this but for some reason I cannot get a number back from
the $_POST array. I am also confused about how the $_POST array is
constructed. How do I reference name/value pair $_POST[0,1,2 etc...] get
their "names" and "values" in to variables so I can accomplish the
INSERT SQL(e.g. $name = $_POST[0,0]; $value = $_POST[0,1]; [INSERT
$value INTO $name];- The MySQL fields will match $name)?

Thanks in advance.

David

Jul 17 '05 #1
2 14103

"davidv" <ei*********@yahoo.com> wrote in message
news:WB********************@speakeasy.net...
I am building a very simple form that takes text and inserts data in a
MySQL db.

I would like my "logic" to simply insert the value in to the field in
the database that matches the name from the query string.

Here's the question- can I do this if I do not know how many name/value
pairs are passed to the "logic" ? Can I just step through the $_POST
array and use a do while $i <= count($_POST) or something?

I have tried this but for some reason I cannot get a number back from
the $_POST array. I am also confused about how the $_POST array is
constructed. How do I reference name/value pair $_POST[0,1,2 etc...] get
their "names" and "values" in to variables so I can accomplish the
INSERT SQL(e.g. $name = $_POST[0,0]; $value = $_POST[0,1]; [INSERT
$value INTO $name];- The MySQL fields will match $name)?

Thanks in advance.

David


First off - you ought to becareful writing $_POST data to the database
without first ensuring that its clean. Do you have magic_quotes on (its on
by default I think). Secondly, I have all my db values sent through
trim() - This removes excess space either side of your values, including any
null characters (I don't fully understand the latter other than believing
that some hacks can be performed with buffer overruns which (I believe, I
could be wrong) is performed by passing commands beyond null characters
(again, dunno how its done but believe I'm preventing such a hack from
happening by using trim().

Secondly... You need to know how an associated array works...

If you have a text field in HTML like so:

<input type=text name=firstname>

and it is submitted in a form, your $_POST will have a variable
$_POST['firstname']

If you're ever unsure about what has been passed, have your form posted to a
script that calls phpinfo(); and examine the output.

Secondly...

I'm new to MySQL but I have written a script that takes my HTML field/box
names and passes them to an insert in the correct order.... you have to be
careful with this.

I suggest that you first get to know arrays, single and multipe dimensional
and be sound on manipulating them before you think about writing them to the
database... You've not been entirely clear with your request, but I think I
know what you are trying to do... this is possible... but unless you read up
a little more, you could end up writing a script that you rely on, but
unable to support...

Jul 17 '05 #2
I think this may be what you're looking for (note this is off the top
of my head and may require some work on your part :)

$query = "INSERT INTO mytable (";
$q3 = ") VALUES (";
$q5 = ")";

while(list($key,$value)=explode($_POST)){

//Break $_POST into key,value pairs and iterate through the array

$key=trim(chop($key)); //Clean off White Space
$value=trim(chop($value));
$q2 .="$key,"; //concatenate key to q2
$q4 .="'$value',"; //concatenate value to q4
}
substr_replace($q2,"",strlen($q2)); //Get rid of that extra comma at
the end of q2
$q3= substr_replace($q3,"",strlen($q3));//Get rid of that extra comma
at the end of q3

$query .=$q2.$q3.$q4.$q5; //Add it all together
$result = mysql_query($query)
or die("Error: ".mysql_errno()." ".mysql_error()." while trying
$query"); //Give me my result or die, damn you die! Muhahaha!!!

Note the comments were added for effect :)

Hope that helps (Hope it's correct to :)


davidv <ei*********@yahoo.com> wrote in message news:<WB********************@speakeasy.net>...
I am building a very simple form that takes text and inserts data in a
MySQL db.

I would like my "logic" to simply insert the value in to the field in
the database that matches the name from the query string.

Here's the question- can I do this if I do not know how many name/value
pairs are passed to the "logic" ? Can I just step through the $_POST
array and use a do while $i <= count($_POST) or something?

I have tried this but for some reason I cannot get a number back from
the $_POST array. I am also confused about how the $_POST array is
constructed. How do I reference name/value pair $_POST[0,1,2 etc...] get
their "names" and "values" in to variables so I can accomplish the
INSERT SQL(e.g. $name = $_POST[0,0]; $value = $_POST[0,1]; [INSERT
$value INTO $name];- The MySQL fields will match $name)?

Thanks in advance.

David

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: steve | last post by:
Hi, I am opening a stream that is UTF encoded. I use fgetc to read the stream- which is binary safe. I add every character read to a string. But when I look at the stream, I see some...
40
by: Peter Row | last post by:
Hi all, Here is my problem: I have a SQL Server 2000 DB with various NVarChar, NText fields in its tables. For some stupid reason the data was inserted into these fields in UTF8 encoding. ...
0
by: Johann Blake | last post by:
In my need to decode a JPEG 2000 file, I discovered like many that there was no functionality for this in the .NET Framework. Instead of forking out a pile of cash to do this, I came up with the...
9
by: Mark | last post by:
I've run a few simple tests looking at how query string encoding/decoding gets handled in asp.net, and it seems like the situation is even messier than it was in asp... Can't say I think much of the...
2
by: Jeff Stewart | last post by:
I wrote the following function to accomodate a legacy driver function that only accepted VB6-style strings. The driver expects a string that represents an array of binary data to be sent over USB....
0
by: R L Vandaveer | last post by:
I am having an interesting and rather frustrating issue with the encoding/decoding of parameters with the ASP.NET runtime. What I am doing isn't exactly revolutionary so hopefully someone has an...
5
by: Peter Jansson | last post by:
Hello group, The following code is an attempt to perform URL-decoding of URL-encoded string. Note that std::istringstream is used within the switch, within the loop. Three main issues have been...
25
by: marcin.rzeznicki | last post by:
Hello everyone I've got a little problem with choosing the best decoding strategy for some nasty problem. I have to deal with very large files wich contain text encoded with various encodings....
0
by: Michele | last post by:
Hi there, I'm using a python script in conjunction with a JPype, to run java classes. So, here's the code: from jpype import * import os import random import math import sys
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.