Poddy Posted June 21, 2008 Share Posted June 21, 2008 I am trying to get a dynamic form(one which you can add questions into) to post the dynamic questions, it fetches the questions from a questions database then inserts it into the form. however on the processing of the form when i try inserting into the database all i get is 0 0 0 when the result is supposed to be 5 5 5 the post variables are working since i echoed them.. the problem seems to be with the query yet it does do the insert query but with the wrong data <?php $sql = "select `short` from `questions`"; $result = mysql_query($sql) or die ('error 43' . mysql_error()); while ($row = mysql_fetch_assoc($result)) { $short[] = $row['short']; $varshort[] = "'$" . $row['short'] . "'"; } $fields = implode(',',$short); echo $fields . "<br /> "; $values = implode(',',$varshort); echo $values; $sql = "insert into `data` (name, spot, piret, text, $fields) VALUES ('$name', '$spot', '$piret', '$text', $values )"; mysql_query($sql) or die ('ERROR' . mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/111238-solved-problem-inserting-into-mysql-with-fields-from-an-array/ Share on other sites More sharing options...
hitman6003 Posted June 21, 2008 Share Posted June 21, 2008 Not 100% sure what you're trying to accomplish... while ($row = mysql_fetch_assoc($result)) { $short[] = $row['short']; $varshort[] = $$row['short']; } $fields = implode(',',$short); echo $fields . "<br /> "; $values = implode("','",$varshort); echo $values; $sql = "insert into `data` (name, spot, piret, text, $fields) VALUES ('$name', '$spot', '$piret', '$text', '$values' )"; Quote Link to comment https://forums.phpfreaks.com/topic/111238-solved-problem-inserting-into-mysql-with-fields-from-an-array/#findComment-570945 Share on other sites More sharing options...
.josh Posted June 21, 2008 Share Posted June 21, 2008 so..where are these vars being assigned? $name $spot $piret $text Quote Link to comment https://forums.phpfreaks.com/topic/111238-solved-problem-inserting-into-mysql-with-fields-from-an-array/#findComment-570968 Share on other sites More sharing options...
Poddy Posted June 21, 2008 Author Share Posted June 21, 2008 they were being assigned from a form, as i knew that part was fine i left it alone anyway, thanks for your help for some reason replacing $varshort[] = "'$" . $row['short'] . "'"; in $varshort[] = $$row['short']; fixed the problem, as it was getting the variable name, and not the content. thank you very much, you both Quote Link to comment https://forums.phpfreaks.com/topic/111238-solved-problem-inserting-into-mysql-with-fields-from-an-array/#findComment-571013 Share on other sites More sharing options...
DarkWater Posted June 21, 2008 Share Posted June 21, 2008 Poddy, it just magically populates those variables? That must mean you have register_globals on. Turn them off before the world explodes. D: No, seriously. They were turned off by default in PHP 4.2 or 4.3 (I forget) and they're completely out in PHP 6. They're obsolete, so turn it off and code properly. Quote Link to comment https://forums.phpfreaks.com/topic/111238-solved-problem-inserting-into-mysql-with-fields-from-an-array/#findComment-571020 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.