使用mysql数据填充表单

时间:2014-08-12 11:59:59

标签: php html mysql forms

希望有人可以帮我解决问题,我有一个使用MySQL的数据库,我有一个发布到数据库的表单,这一切都运行良好。

我创建了一个客户查找表单,使用HTML和PHP回显表,然后表填充了MySQL数据,这一切都正常。

我的问题是,我想单击该行中的表行或文本,并以HTML格式打开它,并包含MySQL数据库中该行的所有数据。

<div id="content">
<h3>Customer Lookup</h3>
<?php
$con=mysqli_connect("localhost","root","","repairsdb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM customers");

echo "<table border='1'>
<tr>
<th>First Name</th>
<th>Surname</th>
<th>Address</th>
<th>Postcode</th>
<th>Landline</th>
<th>Mobile</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['addressl1'] . "</td>";
echo "<td>" . $row['postcode'] . "</td>";
echo "<td>" . $row['landline'] . "</td>";
echo "<td>" . $row['mobile'] . "</td>";
echo "<td>" . $row['businessname'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>
<FORM>
</FORM>
</div>

新代码

<h3>Customer Lookup</h3>
  <?php
$con=mysqli_connect("localhost","root","","repairsdb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM customers");

echo "<table border='1'>
<tr>
<th>First Name</th>
<th>Surname</th>
<th>Address</th>
<th>Postcode</th>
<th>Landline</th>
<th>Mobile</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><a href='/repairdb/customer_form.php?customerid=" . $row['customerid'] . "'>" . $row['firstname'] . "</td>";
    echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['addressl1'] . "</td>";
echo "<td>" . $row['postcode'] . "</td>";
echo "<td>" . $row['landline'] . "</td>";
echo "<td>" . $row['mobile'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);

&GT;

customer_form.php

<?php
$con=mysqli_connect("localhost","root","","repairsdb");
if( isset( $_GET['customerid'] ) ) {
  $customerid = $_GET['customerid'];
}
$result = mysqli_query($con,"SELECT * FROM customers where customerid = " . $customerid . " Limit 1");

?>
<form method="post">
  <input type="text" name="firstname" value="$result['firstname']" />
  <input type="text" name="surname" value="$result['surname']" />
  <input type="text" name="addressl1" value="$result['addressl1']" />
  <input type="text" name="postcode" value="$result['postcode']" />
  <input type="text" name="landline" value="$result['landline']" />
  <input type="text" name="mobile" value="$result['mobile']" />
  <input type="text" name="businessname" value="$result['businessname']" />
</form>

3 个答案:

答案 0 :(得分:0)

最简单的方法是在新页面中打开表单。为此,我们首先添加一个指向表的链接。

我假设您的数据库中每个客户都有唯一ID - 我们将在链接中使用此内容:

echo "<tr>";
    echo "<td><a href='/customer_form.php?customer_id=" . $row['ID'] . "'>" . $row['firstname'] . "</a></td>";
    echo "<td>" . $row['surname'] . "</td>";
    echo "<td>" . $row['addressl1'] . "</td>";
    echo "<td>" . $row['postcode'] . "</td>";
    echo "<td>" . $row['landline'] . "</td>";
    echo "<td>" . $row['mobile'] . "</td>";
    echo "<td>" . $row['businessname'] . "</td>";
echo "</tr>";

如您所见,我已将新页面命名为 customer_form.php 。我们将通过网址 将ID传递给此页面。

www.example.com/customer_form.php?customer_id=3

在新的 customer_form.php 页面中,您首先需要从网址检索ID

if( isset( $_GET['customer_id'] ) ) {
  $customer_id = $_GET['customer_id'];
}

然后我们将稍微修改这个代码块,使用ID从数据库中检索客户,然后我们可以将这些数据添加到新表单中。

if( isset( $_GET['customer_id'] ) ) {
  $customer_id = $_GET['customer_id'];

  $result = mysqli_query($con,"SELECT * FROM customers where ID = " . $customer_id . " Limit 1");

  echo '
  <form method="post">
    <input type="text" name="firstname" value="'. $result['firstname'] .'" />
    <input type="text" name="surname" value="'. $result['surname'] .'" />
    <input type="text" name="addressl1" value="'. $result['addressl1'] .'" />
    <input type="text" name="postcode" value="'. $result['postcode'] .'" />
    <input type="text" name="landline" value="'. $result['landline'] .'" />
    <input type="text" name="mobile" value="'. $result['mobile'] .'" />
    <input type="text" name="businessname" value="'. $result['businessname'] .'" />
  </form>
  ';
}

您可以通过其他方式,在弹出窗口中,甚至使用JavaScript在同一页面上执行此操作。但这是最不复杂的。

我应该注意,我编写的SQL查询很容易受到SQL注入的影响,因为它从地址栏中获取一个值并将其直接注入查询中。但这有点超出了问题的范围,所以如果有兴趣,我建议你查阅一下。

答案 1 :(得分:0)

听起来这样可行......

使用点击链接(选择内容)

然后 - jqueryAJAX POSTs FORM

然后 - php processPOST

然后 - mysql getDataFrom DB

然后 - php进程MYSQL结果&amp;&amp; echo $ jsoResults

然后 - jqueryAJAX流程结果打开新表单,输入填充...

答案 2 :(得分:0)

我设法对错误进行排序,我已经为任何有相同问题的人发布了代码。我之所以回答这个问题仅仅是因为我可以发布最终的代码,但是信用证必须转到@ user1100149以及YouTube上的一个小伙伴。

Link to youtube video editing mysql with PHP

<?php 
 // Connects to Our Database 
 mysql_connect("localhost", "root", "") or die(mysql_error()); 
 mysql_select_db("repairsdb") or die(mysql_error()); 

    if(!isset ($_POST['submit'])) {
        $q = "SELECT * FROM customers WHERE customerid = $_GET[customerid]";
        $result = mysql_query($q);
        $person = mysql_fetch_array($result)or die (mysql_error()); 
    }
?>
<form name="submit_customer" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <div id="frcol3">
      <fieldset>
      <label id="label" for="webaddress">Business</label>
        <input name="businessname" type="text" class="inputBox" id="" value="<?php echo $person['businessname']; ?>" />
        <label id="label" for="firstname">First Name</label>
        <input class="inputBox" type="text" name="firstname" id="" value="<?php echo $person['firstname']; ?>" />
        <label id="label" for="Surname">Surname</label>
        <input name="surname" type="text" class="inputBox" id="" value="<?php echo $person['surname']; ?>" />
        <label id="label" for="landline">Telephone No.</label>
        <input name="landline" type="text" class="inputBox" id="" value="<?php echo $person['landline']; ?>" />
        <label id="label" for="mobile">Mobile No.</label>
        <input name="mobile" type="text" class="inputBox" id="" value="<?php echo $person['mobile']; ?>" />
        <label id="label" for="email">Email</label>
        <input name="email" type="text" class="inputBox" id="" value="<?php echo $person['email']; ?>" />


      </fieldset>
    </div>
    <div id="frcol2">
      <fieldset>
        <label id="label" for="webaddress">Web Address</label>
        <input name="webaddress" type="text" class="inputBox" id=""  value="<?php echo $person['webaddress']; ?>" />
        <label id="label" for="addressl1">Address Line 1</label>
        <input name="addressl1" type="text" class="inputBox" id="" value="<?php echo $person['addressl1']; ?>" />
        <label id="label" for="addressl2">Address Line 2</label>
        <input name="addressl2" type="text" class="inputBox" id="" value="<?php echo $person['addressl2']; ?>" />
        <label id="label" for="town">Town</label>
        <input name="town" type="text" class="inputBox" id="" value="<?php echo $person['town']; ?>" />
        <label id="label" for="county">County</label>
        <input name="county" type="text" id="" class="inputBox"  value="<?php echo $person['county']; ?>" />
        <label id="label" for="postcode">Postcode</label>
        <input name="postcode" type="text" id="" class="inputBox"  value="<?php echo $person['postcode']; ?>" />
      </fieldset>
    </div>
    <div id="frcol1">
      <textarea name="notes" id="notes" placeholder="<?php echo $person['notes']; ?>" cols="45" rows="5"></textarea>
      <br />
      <input type="hidden" name="customerid" value="<?php echo $_GET['customerid']; ?>" />

      <input type="submit" name="submit" id="" class=" button" value="Submit" />
    </div>
  </form>
相关问题