【问题标题】:Dynamically change php table动态更改php表
【发布时间】:2016-04-19 10:43:41
【问题描述】:

首先 - 感谢您的帮助!:)

我正在尝试根据用户输入的文本创建一个表格。

例如 - 如果用户类型为 123,我将在 <p> 中打印与该输入相关联的 sql 表中的表。

我在index.php文件中写了这段代码:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <html xmlns="http://www.w3.org/1999/xhtml">
  <script>
  $("#testnum").change(function()
  {
      $("#dates").load("getter.php?choice="+$("#testnum").val());
  }
  </script>
</head>
<body>
<input id="testnum" type="int" placeholder="Insert the test num">
<p id="dates">

</p>

这在getter.php 文件中:

<?php
include_once "dbconnect.php";
$testnum=$_GET['choice'];
$result = mysqli_query($con,"SELECT * FROM `dates` WHERE testnum={$testnum}");
echo "<table>"
while($result2=mysqli_fetch_array($result))
{
    echo "<td>
    <tr>{$result2['date']}</tr>
    <tr>{$result2['testnum']}</tr>
    <tr>{$result2['result']}</tr>
    </td>";
}
echo "</table>";
?>

这是dbconnect.php

<?php
$servername = "localhost";
$username = "admin";
$password = "123456";
$dbname = "testdb";
// Create connection
$con=mysqli_connect($servername, $username, $password, $dbname) or die ( "DB error" );
// Check connection
if ($con->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
mysqli_query($con,"SET NAMES utf8");
$_POST['con']=$con;
?>

怎么了?

【问题讨论】:

  • 错字。变量名错误...设置choise vs 获取choice ;)
  • JavaScript 中的 ID 名称也是错误的 .. 日期与日期
  • 对于初学者来说,您已经对 SQL 注入攻击敞开了大门。始终清理输入。快速搜索一下 SQL 注入以及如何保护自己。其次,您是否检查过查询是否符合您的预期,如果您在 PHPMyAdmin 中运行它会返回结果?另外,尝试var_dump(mysqli_fetch_array($result); 看看它在拉回什么。
  • 将 $("#date") 更改为 $("#dates")
  • 随时更新您的问题我的任何问题的任何答案

标签: javascript php jquery html sql


【解决方案1】:

我使用了错误的“phpmyadmin”数据库... 我尝试在我的 wamp 服务器上使用那个,而我使用在线服务器中的名称...

任何想杀我的人- 明天14:00-16:00我家附近

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    • 2016-06-19
    • 2019-08-30
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 2017-07-30
    相关资源
    最近更新 更多