Jump to content

[SOLVED] problem inserting into mysql with fields from an array


Poddy

Recommended Posts

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());
?>

Link to comment
Share on other sites

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' )";

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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