1

「[+]」記号をクリックするとフィールドを増やすことができるフォームがあります。

フォームに使用するコードは、説明数量価格'です。

// Loop to prepare the display of 100 product lines
for ($i=0; $i<100; $i++) {

   if ($text['quantity'][$i] == "") $text['quantity'][$i] = 1;
   if ($text['unit'][$i] == "") $text['unit'][$i] = "0.00";
   // Display only the first line
   if ($nbr_ligne == 0) $nbr_ligne = 1;
   if ($i >= $nbr_ligne) $display = 'style="display:none"';
   echo '
   <tr id="cell'.$i.'" '.$display.'>
       <td>
           <textarea name="text[detail]['.$i.']">'.stripslashes($text['detail'][$i]).'</textarea>
           <br />
           <a href="javascript:void(0)" onclick="javascript:document.getElementById(\'cell'.($i+1).'\').style.display=\'table-row\'; this.style.display=\'none\'">[+]</a>
       </td>
       <td>
           <input name="text[quantity]['.$i.']" id="q'.$i.'" value="'.stripslashes($text['quantity'][$i]).'" size="4" />
       </td>
       <td>
           <input name="text[unit]['.$i.']" id="u'.$i.'" value="'.stripslashes($text['unit'][$i]).'" size="7" /> USD
       </td>
   </tr>';
}

echo '
   </table>
   <input type="submit" name="save" value="Save" />
</form>
';
if(isset($_POST['save']))
{
    echo $text['quantity']['.$i.'];

       mysql_connect("localhost","root","");
       mysql_select_db("rvt") or die("unable to select db");
       extract($_POST);
       $insert=mysql_query("insert into add(description, quantity, price) values('$text[detail][".$i."]','$text[quantity][".$i."]','$text[unit][".$i."]')");
    if($insert)
    {
        echo "hi";
    }
}
?>

データをデータベースに保存したい。動作しません。

Plzは、データベースにデータを保存するのに役立ちます。

4

1 に答える 1

0

Your query will not work due to a PHP parser "bug". It is not a greedy parser, so something like:

echo "$array[1][2]";

will actually produce as output

Array[2]   <---yes, the literal word 'Array'.

and not whatever value is stored at index 2 of the sub-array at index 1.

because PHP's parser stops considering array references after the first set of []. You need to use {} notation:

... values('{$text['detail'][$i]}', etc...
            ^                   ^

note the position of the {}, and the additional ' around the detail key.

As well, please do not use extract(), especially on _GET/_POST. You're essentially reproducing the very horribly nastily ugly bad days of when register_globals was turned on in PHP. It's a security hole just waiting to get exploited.

于 2012-09-24T04:32:42.850 に答える