douzhen9428 2012-12-06 03:05
浏览 37
已采纳

autopopulate字段用javascript和mysql查询下拉列表

I have a question on how to autopopulate a dropdown menu from column in mysql database, then autofill fields below based upon the user's selection. Please help! My code wont post correctly. I know how to query database and populate the dropdown but i cant get the fields to autofill below.

<?php
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);
header("Location: dashboard.html")
?> 
<?php
$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());

else { 
mysql_select_db("Transgen", $con);?>
$query = "SELECT * FROM PartnerSetup" or die(mysql_error()); 
$result = mysql_query($query) or die(mysql_error()); 

    if(mysql_num_rows($result) > 0) {
        while($row = mysql_fetch_assoc($result)) { 
            echo ($row['Partner']); echo "<br/>"; 
        } 
    ?>
  • 写回答

1条回答 默认 最新

  • donglue8180 2012-12-06 03:37
    关注

    Time to get cracking;

    First is your errors within the code;

    if (!mysql_query($sql,$con))

    This should be handled within your database connection information:

    Example:

    $host = "HOST";
    $Username = "user"; 
    $Password = "password"; 
    $sql = mysql_connect("$host", "$Username", "$Password");
    $conn = mysql_select_db("DATABASENAME", $sql); // Depending on what $sql and $con contain on your code. 
    

    Then your if statement:

    if (!mysql_query($sql,$con)) {...}

    Should be replaced by:

    if (!($sql))
    {
       die('Error: ' . mysql_error());
    } // Kills the script if correct information could not be used to access the database
    

    You have an echo

    echo "1 record added";

    Which is outside your brackets { or }

    I am presuming this is inside either a function or a $_POST/$_GET If so, then ignore the above

    If not, then your script will be echoing out that string without need. This should be revised

    You have also prematurely closed your PHP tags after: mysql_select_db("Transgen", $con);?>

    ^ In addition; You have !$sql and !con prior to $sql being set; on the line:

    $con = mysql_connect("localhost","root","password");

    Which $con is stated after the initial variables, this might return an error if you not already have set $con and $sql prior to the first line displayed.

    So your $query will not be executed, rather be shown in plain text to the user/person viewing the page.`

    and in response to your question; if you are set on doing it through javascript, then look online.

    This can be done through PHP by using a while loop with a few queries to populate your drop down menu within PHP it's self.

    <select name="SELECTNAME"> 
        <?PHP
            $DropDownQuery = mysql_query("SELECT * FROM PartnerSetup");
            while ($Rows = mysql_fetch_array($DropDownQuery))
            {
                $Partner = $Rows['Partner'];
                echo "<option value=\"$Partner\">$Partner</option>";
            } // This will enable the user to submit a "partner" if needed in a <form> </form> setup
        ?>
        </select>
    

    I would recommend appending errors within your code, I'm going by what has been submitted, and what I can see. From what I can see, I have pointed out areas that might/will return an error.

    I would also look into the PDO functions; as MySQL is depreciated and will be removed from future PHP Revisions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题