dylan.bathurst Posted August 14, 2007 Share Posted August 14, 2007 I am working with online surveys that have roughly 50 fields for the users to fill out. I have done smaller INSERT INTO's before. Such as the tutorial on this site, but never on a large scale. I was just wondering what would be a way to simplify this process. Instead of having: $sql = "INSERT INTO table (column1, ..., column 52) VALUE ('$_POST[ id1]','...','$POST[ id53]')"; Thanks in advance. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 14, 2007 Share Posted August 14, 2007 That's the statement you need. You can build it in a loop if you don't want to write the whole thing out yourself, but the SQL statement that you send to the server will be the same, there's only one way to insert data. Link to comment Share on other sites More sharing options...
dylan.bathurst Posted August 14, 2007 Author Share Posted August 14, 2007 A loop is what I was thinking, and I have worked with loops before, but a little guidance would be nice. I was thinking of using a for loop. Any suggestions on that? Link to comment Share on other sites More sharing options...
justsomeguy Posted August 14, 2007 Share Posted August 14, 2007 If you know how many you have (i.e. 1 to 52), you can do this: $sql = "INSERT INTO table (";for (i = 1; i <= 52; i++) $sql .= "column{$i},";$sql = substr($sql, 0, strlen($sql) - 1);$sql .= ") VALUES (";for (i = 1; i <= 52; i++) $sql .= "'" . mysql_real_escape_string($_POST['id' . $i]) . "',";$sql = substr($sql, 0, strlen($sql) - 1);$sql .= ")"; Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.