drh96824 2018-05-31 20:10
浏览 59
已采纳

PHP SQL表单 - 如何将值传递给下一个Select

Hi I have manage to put this script together as a newbe. I have divided the script up in two parts each part works individually, but when putting it together and when I make the WHERE as a changeable value it won't work.

(the reason for the line in error everywhere/shortcode.php(15) is because its running on a php wordpress plugin, but as I say the to parts works individual)

Can someone tell me how I can pass that value

$myvalue

and open the connection in part two of the script, without getting error on the connection part. The value do get past to the varible but makes an error.

Here is the errors I get:

Warning: mysqli_query(): Couldn't fetch mysqli in /home/asports/public_html/calendar/wp-content/plugins/php-everywhere/shortcode.php(15) : eval()'d code on line 34

Warning: mysqli_error(): Couldn't fetch mysqli in /home/asports/public_html/calendar/wp-content/plugins/php-everywhere/shortcode.php(15) : eval()'d code on line 52

ERROR: Could not able to execute SELECT men_slope FROM allcourses WHERE id='3'.

Warning: mysqli_close(): Couldn't fetch mysqli in /home/asports/public_html/calendar/wp-content/plugins/php-everywhere/shortcode.php(15) : eval()'d code on line 56

// part one

<?php 
require_once(
$_SERVER['DOCUMENT_ROOT'].'/calendar/courses/admin/connect.php'); 
?>
<form method=post>
<select name="myvalue">
<?php
// Attempt select query execution
$sql = "SELECT id, Name, color FROM allcourses";
$sql = mysqli_query($link, $sql);
while ($row = $sql->fetch_assoc()){
echo "<option value='".$row['id']."'>".$row['Name'] . " (" . $row['color']. 
")" . "</option>";

}
// Close connection
mysqli_close($link);
$myvalue=$_POST['myvalue'];
$myhdc=$_POST['hdc'];
?>
</select>
<br />
<p>Handicap: </p>
<input class="tex" type="text" name="hdc"\></input>

<br />

<input type=submit>
</form>

// Part two

<?php


// Attempt select query execution
$sqll = "SELECT men_slope FROM allcourses WHERE id='$myvalue'";
if($result = mysqli_query($link, $sqll)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
        echo "<tr>";

            echo "<td>" . $row['men_slope'] . "</td>";

        echo "</tr>";
    }

    // Free result set
    mysqli_free_result($result);
} else{
    echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sqll. " . mysqli_error($link);
}

// Close connection
mysqli_close($link);
?>

展开全部

  • 写回答

1条回答 默认 最新

  • dongshou7903 2018-05-31 20:19
    关注

    The problem is that you call mysqli_close($link); in Part One. Then when you try to perform a query in Part Two, the link is closed, so you can't use it any more.

    IHMO, it's usually not important to call mysqli_close(). The connection will be closed automatically when the script ends. So unless your script runs for a long time after making all its database queries, it will not be idle for very long.

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

报告相同问题?

悬赏问题

  • ¥15 cmake+mingw使用<mysqlx/xdevapi.h>报错
  • ¥15 eNSP中防火墙的使用
  • ¥15 不能对数据库增删改但是可以查询
  • ¥15 在触控设备上启动TabTip.exe打不开键盘界面,怎么用代码启动进程打开界面
  • ¥15 关于#mlnet#的问题:mlnet相关请求(语言-c#)
  • ¥15 lvgl7.11怎么做出文字被选中的效果
  • ¥50 如何快速查看手机目标app的主要服务器ip
  • ¥15 (标签-stm32|关键词-m3)
  • ¥15 matlab中频率调制法代码的解读
  • ¥15 ceph的对象、块、文件相关问题求解答