jesushax Posted July 29, 2008 Share Posted July 29, 2008 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 More sharing options...
jeeva Posted July 29, 2008 Share Posted July 29, 2008 You can just print the submitted values.... <?php print_r($_POST); ?> This will give u like $_POST['<Keys>']="<Values>"...etc That key will be Field Name.... Link to comment https://forums.phpfreaks.com/topic/117162-putting-form-data-into-a-mysql-array/#findComment-602609 Share on other sites More sharing options...
jesushax Posted July 29, 2008 Author Share Posted July 29, 2008 could you explain a lilttle more, example maybe? thankyou Link to comment https://forums.phpfreaks.com/topic/117162-putting-form-data-into-a-mysql-array/#findComment-602611 Share on other sites More sharing options...
jeeva Posted July 29, 2008 Share Posted July 29, 2008 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 https://forums.phpfreaks.com/topic/117162-putting-form-data-into-a-mysql-array/#findComment-602620 Share on other sites More sharing options...
Xurion Posted July 29, 2008 Share Posted July 29, 2008 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 https://forums.phpfreaks.com/topic/117162-putting-form-data-into-a-mysql-array/#findComment-602624 Share on other sites More sharing options...
jesushax Posted July 29, 2008 Author Share Posted July 29, 2008 thats the one i was after xurion i know how to collect information using $_POST how are keys and valuse defined how does that work? Cheers Link to comment https://forums.phpfreaks.com/topic/117162-putting-form-data-into-a-mysql-array/#findComment-602633 Share on other sites More sharing options...
jesushax Posted July 29, 2008 Author Share Posted July 29, 2008 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 https://forums.phpfreaks.com/topic/117162-putting-form-data-into-a-mysql-array/#findComment-602660 Share on other sites More sharing options...
jesushax Posted July 29, 2008 Author Share Posted July 29, 2008 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 https://forums.phpfreaks.com/topic/117162-putting-form-data-into-a-mysql-array/#findComment-602712 Share on other sites More sharing options...
DarkWater Posted July 29, 2008 Share Posted July 29, 2008 Use foreach($_POST as $key=>$value), not while(). Link to comment https://forums.phpfreaks.com/topic/117162-putting-form-data-into-a-mysql-array/#findComment-602769 Share on other sites More sharing options...
jesushax Posted July 29, 2008 Author Share Posted July 29, 2008 thankyou very much Link to comment https://forums.phpfreaks.com/topic/117162-putting-form-data-into-a-mysql-array/#findComment-602804 Share on other sites More sharing options...
Xurion Posted July 29, 2008 Share Posted July 29, 2008 my bad Link to comment https://forums.phpfreaks.com/topic/117162-putting-form-data-into-a-mysql-array/#findComment-602898 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.