我长期坚持这个问题并试图寻找解决方案,但无法弄明白。 我有输入字段,其中用户将键入已存在于事件表中的Occation名称,然后我编写了php代码,以便显示该occationname的整行。 下面是我的代码,我总是得到表的第一行(这意味着它没有进行检查或WHERE条件)。我试图分配$ X值,如: $ X = gh(gh是表中存在的名称),它完美地运行。 似乎php是在页面加载时执行的,意味着在用户输入输入值之前。 (但这没有任何意义,为什么它显示第一行呢?)
注意:我使用if(issset($ _ POST [' s'])来获取用户是否点击按钮,但是因为在点击
之前执行了php代码它无效
function GetI()
{
var x = document.getElementById("tt").value;
document.getElementById("tvalue").value = x ;
}

<input type="text" id="tt" value="" class="contactField"/> </br>
<input type="submit" class="pageapp-login-button button button-small button-green button-fullscreen " style="font-size:13px" value="getValue" onclick="GetI()" />
<form method="post" name="addingform1" id="addingform1" action="testing.php" >
<!-- Here in tvalue, I just show this input to make sure the variable is copying the same input that user typed -->
<input type="text" id="tvalue" value="" class="contactField"/>
<input type="submit" name="s" class="pageapp-login-button button button-small button-green button-fullscreen " style="font-size:13px" value="Fordata"/>
<?php header("Content-type: text/html; charset=utf-8");
if(isset($_POST['s']))
$X= $_POST['tvalue'];
include('database/connect-mysql.php');
mysql_query("set character_set_server='utf8'");
mysql_query("set names 'utf8'");
$sqls = "SELECT Date, Address, City, TotalGuest FROM Events WHERE OccationName = '$X'";
echo " <table id='tid'style= 'border-collapse: collapse;
border-spacing: 0;
width: 100%;
border: 1px solid #ddd;
position:absolute;
top:20%;
right:20%;
'>
<th><div style='overflow: auto; height: 30px; width: 100px;'>Date</th>
<th><div style='overflow: auto; height: 30px; width: 100px;'>Address</th>
<th><div style='overflow: auto; height: 30px; width: 100px;'>City</th>
<th><div style='overflow: auto; height: 30px; width: 100px;'>TotalGuest</th>
</tr>
";
foreach ($dbcon->query($sqls) as $row){
echo "<tr >";
echo "<td>" . $row['Date'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "<td>" . $row['City'] . "</td>";
echo "<td>" . $row['TotalGuest'] . "</td>";
echo "</tr>";
}
?>
</form>
&#13;
答案 0 :(得分:-1)
哇这个代码有很多问题。我会查看/ var / logs / httpd,看看你做错了什么。
无论其
第一个问题是您的<input>
字段没有name='tvalue'
或类似的内容。您无法使用$_POST['tvalue']
第二个问题(我相信)是查询的foreach
。我不知道您可以使用foreach
翻转数据库,通常是在while循环中执行。
答案 1 :(得分:-1)
正如其他人所说,你应该使用Mysqli。无论这里是答案,我希望你从中学习。
<?php
$form = "
<form id=\"addingform1\" action=\"testing.php\" name=\"addingform1\" method=\"post\">
<input id=\"tvalue\" type=\"text\" class=\"contactField\" value=\"".$xvar."\"/>
<input type=\"submit\" name=\"s\" class=\"pageapp-login-button button button-small button-green button-fullscreen \" style=\"font-size:13px\" value=\"Fordata\"/>
</form>";
IF (isset($_POST['s'])) { // form has been submitted
$err = ""; // form errors
// Associate and Sanitize form variables
$xvar = htmlspecialchars(strip_tags(trim($_POST['tvalue'])));
// Validate form variables
IF (empty($xvar)) { $err .="- tvalue is empty"; }
IF (!empty($err)) {
echo($err); // display errors
// display the form
echo($form);
}ELSE{ // no errors
// DB connection -- only include/establish when needed.
include('database/connect-mysql.php');
mysql_query("set character_set_server='utf8'");
mysql_query("set names 'utf8'");
$_xvar = mysql_real_escape_string($xvar);
$result = "";
// sql
$sql = "SELECT Date, Address, City, TotalGuest FROM Events WHERE OccationName = '$_xvar'";
$query = $dbcon->query($sql);
IF ($query) {
While ($row = mysql_fetch_array($query)) {
// Loop through and build tr rows.
$result .= "<tr><td>".$row['Date']."</td><td>".$row['Address']."</td><td>".$row['City']."</td><td>".$row['TotalGuest']."</td></tr>";
}
mysql_free_result($result);
}ELSE{
// No results from query
$result .= "<tr><td colspan=\"4\">No results for ".$xvar."</td></tr>";
}
mysql_close($dbcon); // close your db connection
// Display results
echo("\n
<table id=\"tid\" >
<th><div style=\"overflow: auto; height: 30px; width: 100px;\">Date</th>
<th><div style=\"overflow: auto; height: 30px; width: 100px;\">Address</th>
<th><div style=\"overflow: auto; height: 30px; width: 100px;\">City</th>
<th><div style=\"overflow: auto; height: 30px; width: 100px;\">TotalGuest</th>
</tr>
".$result."
</table>");
}
}ELSE{
// Form has not been submitted, thus show the form.
echo($form);
}
?>