timmah1 Posted June 2, 2009 Share Posted June 2, 2009 I hope this is posted in the correct forum this time. I have a form that is created dynamically from the fields in the database <table width="100%" border="0" cellspacing="0" cellpadding="5"> <?php $array = array(); $result = mysql_query("SELECT * FROM `main`"); $count = 1; while($row = mysql_fetch_assoc($result)) { foreach ($row as $k => $v) { ?> <tr> <td width="150"><strong><?php echo $k; ?>:</strong></td> <td> <input type="text" name="contact_[<?php echo $k; ?>]" size="15" style='font-family: Verdana; font-size: 8pt' maxlength="50" /></td> </tr> <?php } } ?> </table> It creates the number of fields that is needed from how many fields are in the database. Then, I want to insert that info into the database, here is where I have the problem. I can echo the correct information after posting, but it will not insert into the database Here is my insert <?php if(isset($_POST['submit'])){ while (list ($name,$val) = @each (mysql_real_escape_string($_POST['contact_']))) { echo $name."=".$val."<br />"; $sql = "INSERT INTO main($name) VALUES ( '$val'"; mysql_query($sql) or die("Sorry, there was a problem adding contact<br /> ".mysql_error()); } ?> Can anybody see what I'm doing wrong? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/160694-solved-insert-dynamic-fields-from-dynamic-content/ Share on other sites More sharing options...
Ken2k7 Posted June 3, 2009 Share Posted June 3, 2009 You're missing a closing parenthesis for VALUES. Quote Link to comment https://forums.phpfreaks.com/topic/160694-solved-insert-dynamic-fields-from-dynamic-content/#findComment-848221 Share on other sites More sharing options...
timmah1 Posted June 3, 2009 Author Share Posted June 3, 2009 Thank you Ken2k7, everything inserts now. Now I have another problem, instead of inserting everything on the same row, it adds a new row for every value. See image Would that be because of the while loop? I don't know any other way to do this <?php if($_POST['submit']){ while (list ($name,$val) = @each (mysql_real_escape_string($_POST['contact_']))) { $sql = "INSERT INTO main($name) VALUES ( '$val');"; mysql_query($sql) or die("Sorry, there was a problem adding contact<br /> ".mysql_error()); } echo $name."=".$val."<br />"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/160694-solved-insert-dynamic-fields-from-dynamic-content/#findComment-848229 Share on other sites More sharing options...
Ken2k7 Posted June 3, 2009 Share Posted June 3, 2009 <?php if (isset($_POST['submit'])) { $cols = $vals = ''; foreach ($_POST['contact_'] as $key => $val) { $cols .= $key . ','; $vals .= '\'' . mysql_real_escape_string($val) . '\','; } $cols = rtrim($cols, ','); $vals = rtrim($vals, ','); $sql = 'INSERT INTO main (' . $cols . ') VALUES (' . $vals . ');'; mysql_query($sql) or trigger_error('SQL failed.', E_USER_ERROR); } Quote Link to comment https://forums.phpfreaks.com/topic/160694-solved-insert-dynamic-fields-from-dynamic-content/#findComment-848233 Share on other sites More sharing options...
timmah1 Posted June 3, 2009 Author Share Posted June 3, 2009 Thank you so much Ken2k7, works perfect!! Quote Link to comment https://forums.phpfreaks.com/topic/160694-solved-insert-dynamic-fields-from-dynamic-content/#findComment-848243 Share on other sites More sharing options...
timmah1 Posted June 3, 2009 Author Share Posted June 3, 2009 I made this thread unsolved, because this next problem goes hand-in-hand with it. How do I update the database now with this? The form is the same, but I don't understand the proper way to update. I have this at the bottom of my form to declare the id <?php $array = array(); $result = mysql_query("SELECT * FROM `main` WHERE id = '".mysql_real_escape_string($_GET['id'])."'"); $count = 1; while($row = mysql_fetch_assoc($result)) { foreach ($row as $k => $v) { ?> <tr> <td width="150"><strong><?php echo $k; ?>:</strong></td> <td> <input type="text" name="contact_[<?php echo $k; ?>]" size="15" style='font-family: Verdana; font-size: 8pt' maxlength="50" value="<?php echo $k; ?>" /></td> </tr> <?php if($k == "id"){ $pid = $v; } ?> <?php } } ?> <input type="text" name="id" size="15" value="<?php echo $pid; ?>" /> Then this is the update part <?php if (isset($_POST['submit'])) { $cols = $vals = ''; foreach ($_POST['contact_'] as $key => $val) { $cols .= $key . ','; $vals .= '\'' . mysql_real_escape_string($val) . '\','; } $cols = rtrim($cols, ','); $vals = rtrim($vals, ','); $id = mysql_real_escape_string($_POST['id']); $sql = 'UPDATE main SET (' . $cols = $vals . ') WHERE id = ('.$id.');'; mysql_query($sql) or trigger_error('SQL failed.', E_USER_ERROR); ?>\ The error I get is this Fatal error: SQL failed. in /derek/edit.php on line 19 What did I do wrong this time? Thanks again in advance Quote Link to comment https://forums.phpfreaks.com/topic/160694-solved-insert-dynamic-fields-from-dynamic-content/#findComment-848526 Share on other sites More sharing options...
timmah1 Posted June 4, 2009 Author Share Posted June 4, 2009 bump Quote Link to comment https://forums.phpfreaks.com/topic/160694-solved-insert-dynamic-fields-from-dynamic-content/#findComment-849209 Share on other sites More sharing options...
Ken2k7 Posted June 4, 2009 Share Posted June 4, 2009 What in the world? You really need to learn to debug stuff yourself. Do you even know what that foreach loop does? Perhaps echoing or var_dumping out the values of $col and $vals would help. Quote Link to comment https://forums.phpfreaks.com/topic/160694-solved-insert-dynamic-fields-from-dynamic-content/#findComment-849246 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.