Jump to content

putting form data into a mysql array


jesushax

Recommended Posts

hi i know theres a quick way of doing this ive seen it, ill explain...

 

i ahve a form with many fields in now if i make the forms the same names as the mysql field names

 

how do i make an array from the data to use as a sql statement

 

hope you understand my question :S

 

Thanks

Link to comment
Share on other sites

If u have table like

Id    Name

1      xxxxx

2      yyyyy

 

Now we will get submitted data like

 

$_POST['Id']="3"

$_POST['Name']="zzzzz";

 

Here we have table field name and values as well...By using this array u can make sql statement....

Link to comment
Share on other sites

Let's assume you have three fields: field1, field2 and field3.

 

Also, let's assume the MySQL database reflects these and you have three columns: field1, field2 and field3.

 

$sql = "UPDATE your_table SET";
while($_POST as $key => $value){
   $sql .= " $key='$value'";
}
mysql_query($sql);

 

You may want to add a WHERE method to the end of the sql.

Link to comment
Share on other sites

i cant figure out how to do it

 

can you show an example?

 

<?php

 

switch (@$_GET["action"]) {

 

case "edit":

 

$sql = "UPDATE tblTest SET";

while($_POST as $key => $value){

    $sql .= " $key='$value'";

}

mysql_query($sql);

 

echo "done\n";

 

default

?>

<form method="post" action="?action=edit>

<input type="text" name="field1" />

<input type="text" name="field2" />

<input type="text" name="field3" />

</form>

<?php

break;

}

Link to comment
Share on other sites

this

 

$sql = "UPDATE your_table SET";
while($_POST as $key => $value){
    $sql .= " $key='$value'";
}
$sql = $sql."WHERE CompanyID='$ID'"

 

gives

 

Parse error: syntax error, unexpected T_AS in C:\Websites\admin\trades\edit_mem.php on line 10

 

line ten is

while($_POST as $key => $value){

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.