0

我正在创建购物网站,在管理部分我只是完成了类别以及 mysql 数据库表名类别中的类别列表。

我编写了 ajax 代码,允许我使用它来填充类别的下拉列表,这很好用这里是代码

<script type="text/javascript">
function changeContent(str)
{
if (str=="")
  {
    // if blank, we'll set our innerHTML to be blank.
    document.getElementById("content").innerHTML="";
    return;
  } 
if (window.XMLHttpRequest)
    {   // code for IE7+, Firefox, Chrome, Opera, Safari
        // create a new XML http Request that will go to our generator webpage.
        xmlhttp=new XMLHttpRequest();
    }
else
    {   // code for IE6, IE5
        // create an activeX object
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    // on state change
    xmlhttp.onreadystatechange=function()
    {
    // if we get a good response from the webpage, display the output
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        document.getElementById("content").innerHTML=xmlhttp.responseText;
    }
  }
 // use our XML HTTP Request object to send a get to our content php. 


}
</script>

这是从mysql填充的选择下拉列表菜单的我的php代码..

        <?php

$link = mysql_connect('localhost', 'root', '3250');
mysql_select_db('shopping', $link);

$sql = "select * FROM category";

$result = mysql_query($sql);

echo "<select name=\"muppetname\" onchange=\"changeContent(this.value)\">";

while ($ary = mysql_fetch_array($result)){

    echo "<option value=\"" . $ary['category']  . "\">" . $ary ['category']  . "</option>";
}

echo "</select>";


?>

<?php


mysql_close($link);

?>

我的下一步是在 mysql 中创建一个从下拉列表中选择的类别,称为类别,然后插入另一个表名“add_product”和列名“类别”

<?php
$categories_list = "";
$sql = mysql_query("SELECT * FROM category ORDER BY category ASC");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
    while($row = mysql_fetch_array($sql)){ 
             $id = $row["id"];
             $category_name = $row["category"];
             $categories_list .= "Category ID #:  &nbsp; $id - <strong>$category_name</strong> &nbsp; &nbsp; &nbsp; &nbsp;  <br /><br />";
    }
} else {
    $categories_list = "You have no categories listed in your database yet";
}
?> 

有没有人可以帮助我更正插入 mysql 语句的代码?

4

0 回答 0