需要帮助我的<select>

时间:2016-03-10 06:51:30

标签: php html mysql

我为PC Rigs创建了一个表单,现在的问题是,我想确保将所选项添加到我的localhost(PHPMyAdmin)并显示添加的信息。 编辑!!! 我怎么能解决这个问题,如果我有多个&lt; select&gt;   &lt; form action =&#34; pcbuild.php&#34;方法=&#34;后&#34;&GT;                 &LT; DIV&GT;                     &lt; label for =&#34; customer&#34;&gt;客户名称:&lt; / label&gt;                     &lt; input type =&#34; text&#34; ID =&#34;顾客&#34;名称=&#34;顾客&#34;&GT;                 &LT; / DIV&GT;                 &LT峰; br&GT;                 &LT; DIV&GT;                     &lt; label for =&#34; email&#34;&gt;电子邮件地址:&lt; / label&gt;                     &lt; input type =&#34; text&#34; ID =&#34;电子邮件&#34;名称=&#34;电子邮件&#34;&GT;                 &lt; / div&gt;&lt; label&gt;处理器:&lt; / label&gt;             &lt; select name =&#34; processor&#34; ID =&#34;处理器&#34;&GT;             &lt;选择选项=&#34;选择1&#34;&gt;请选择...&lt; / option&gt;             &lt; option value =&#34; intel_high&#34;&gt; Intel i5&lt; / option&gt;             &lt; option value =&#34; intel_mid&#34;&gt; Intel Pentium&lt; / option&gt;             &lt; option value =&#34; intel_low&#34;&gt; Intel Core2Duo&lt; / option&gt;             &lt; option value =&#34; amd_high&#34;&gt; AMD A6&lt; / option&gt;             &lt; option value =&#34; amd_mid&#34;&gt; AMD Anthlon&lt; / option&gt;             &lt; option value =&#34; amd_low&#34;&gt; AMD Geode&lt; / option&gt;             &LT; /选择&GT;   &lt; label&gt;视频卡:&lt; / label&gt;             &lt; select name =&#34; gpu&#34; ID =&#34; GPU&#34;&GT;             &lt;选择选项=&#34;选择2&#34;&gt;请选择...&lt; / option&gt;             &lt; option value =&#34; nvidia_high&#34;&gt; Nvidia GTX 960&lt; / option&gt;             &lt; option value =&#34; nvidia_mid&#34;&gt; Nvidia GT 450&lt; / option&gt;             &lt; option value =&#34; nvidia_low&#34;&gt; Nvidia Quadro&lt; / option&gt;             &lt; option value =&#34; ati_high&#34;&gt; ATI Radeon R9 290X&lt; / option&gt;             &lt; option value =&#34; ati_mid&#34;&gt; ATI Radeon R7 260X&lt; / option&gt;             &lt; option value =&#34; ati_low&#34;&gt; ATI Radeon HD 6570&lt; / option&gt;             &LT; /选择&GT;   &lt; label&gt;处理器风扇:&lt; / label&gt;             &lt; select name =&#34; procfan&#34; ID =&#34; procfan&#34;&GT;             &lt;选择选项=&#34;选择3&#34;&gt;请选择...&lt; / option&gt;             &lt; option value =&#34; proc_high&#34;&gt; Deepcool Gammaxx 400&lt; / option&gt;             &lt; option value =&#34; proc_mid&#34;&gt; Sheen 775&lt; / option&gt;             &lt; option value =&#34; proc_low&#34;&gt; Stock Fan&lt; / option&gt;             &LT; /选择&GT; 如果我选择Intel i5,GTX 960和Deepcool Gammaxx,它应该添加到我的服务器。 $ customer = $ _POST [&#34; customer&#34;]; $ email = $ _POST [&#34; email&#34;]; $ selectOption = $ _POST [&#39; processor&#39;]; $ selectOption1 = $ _POST [&#34; gpu&#34;]; $ selectOption2 = $ _POST [&#34; procfan&#34;]; require(&#34; connect.php&#34;); $ stmt = $ conn-&gt; prepare(&#34; INSERT INTO pc_rig(客户,电子邮件,处理器,gpu,procfan) VALUES(:customer,:email,:processor,gpu,procfan)&#34;); $ stmt-&gt; execute(); 标题(&#39;位置:rig_finish_list.php&#39;); 并在这里显示 require(&#34; connect.php&#34;);        $ stmt = $ conn-&gt; prepare(&#34; SELECT *                               来自pc_rig                               订购客户,电子邮件&#34;);        echo&#34;&lt; a href =&#39; php_pc_rig.php&#39;&gt;返回表单&lt; / a&gt;&lt; br&gt;&lt; br&gt;&#34; ;        $ stmt-&GT;执行();        而($行= $ stmt-&GT;取(PDO :: FETCH_ASSOC))        {            echo&#34;&lt; tr&gt;&#34 ;;            echo&#34;&lt; td&gt; &#34; $行[&#34;顾客&#34;]&#34。 &LT; / TD&GT;&#34 ;;            echo&#34;&lt; td&gt; &#34; $行[&#34;电子邮件&#34]。&#34; &LT; / TD&GT;&#34 ;;            echo&#34;&lt; td&gt; &#34; $行[&#34;处理器&#34]。&#34; &LT; / TD&GT;&#34 ;;            echo&#34;&lt; td&gt; &#34; $行[&#34; GPU&#34;]&#34。 &LT; / TD&GT;&#34 ;;            echo&#34;&lt; td&gt; &#34; $行[&#34; procfan&#34;]&#34。 &LT; / TD&GT;&#34 ;;            echo&#34;&lt; / tr&gt;&#34 ;;        }    ?&GT; 可悲的是,似乎无法在服务器上看到代码输入。

2 个答案:

答案 0 :(得分:1)

您需要将值绑定到预准备语句。您可以使用$statement->bindValue(':placeholder', $value)

绑定它们

您的代码看起来像这样:

 $stmt = $conn->prepare("INSERT INTO pc_rig (customer,email,processor,gpu,procfan)
VALUES (:customer, :email, :processor, :gpu, :procfan)");

$stmt->bindValue(':customer', $customer);
$stmt->bindValue(':email', $email);
$stmt->bindValue(':processor', $selectOption);
$stmt->bindValue(':gpu', $selectOption1);
$stmt->bindValue(':procfan',selectOption1);

$stmt->execute();

答案 1 :(得分:0)

<form action ="pagename.php" name="formname" method = "POST">
<select name="processor" id="processor">
        <option selected="select1">Please select...</option>
        <option selected="select1">Please select...</option>
        <option value="intel_high">Intel i5</option>
        <option value="intel_mid">Intel Pentium</option>
        <option value="intel_low">Intel Core2Duo</option>
        <option value="amd_high">AMD A6</option>
        <option value="amd_mid">AMD Anthlon</option>
        <option value="amd_low">AMD Geode</option>
        </select>
      <input type="submit" name="submit" value="Insert">
</form>
<?php 
$selectOption = $_POST['processor'];
require ("connect.php");
$stmt = $conn->prepare("INSERT INTO pc_rig (processor)VALUES('$selectOption')");
$stmt->execute ();
header ('Location:rig_finish_list.php');
?>

了解更多http://indiainfotip.com/insert-drop-down-list-selected-value-to-mysql-db-using-php/