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
https://forums.phpfreaks.com/topic/117162-putting-form-data-into-a-mysql-array/
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.

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;

}

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){

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.