0

您好,第一次这样做,基本上我对如何从数据库中重新填充文本框感到很困惑。

我当前的问题是,基本上我的数据库“USER”和“STATISTICS”中有两个表。目前正在工作的是我的代码正在“USER”表中查找“User_ID”的值并填充下拉列表中的值。

我想要从那里填充文本字段,这些字段对应于数据库中查找“User_ID”EG“goal_scored”、“assist”、“clean_sheets”等的那些值。

我很困惑,我查看了各种不同的问题,但找不到我要找的东西。

<?php
$link = mysql_connect("localhost","root","");
mysql_select_db("f_club",$link);
$sql = "SELECT * FROM user ";
$aResult = mysql_query($sql);

?>

<html>
<body>
<title>forms</title>
<link rel="stylesheet" type="text/css" href="css/global.css" />
</head>
<body>
<div id="container">

<form action="update.php" method="post">
<h1>Enter User Details</h1>
<h2>
<p> <label for="User_ID"> User ID: </label> <select id="User_ID" id="User_ID" name="User_ID" >
   <br> <option value="">Select</option></br>
    <?php
        $sid1 = $_REQUEST['User_ID'];

        while($rows=mysql_fetch_array($aResult,MYSQL_ASSOC))
        {

        $User_ID = $rows['User_ID'];
        if($sid1 == $id)
        {
        $chkselect = 'selected';

        }
        else
        {
        $chkselect ='';
        }
        ?>
        <option value="<?php echo  $id;?>"<?php echo $chkselect;?>>
        <?php echo $User_ID;?></option>
        <?php }
        ?>
        I had to put this in because everytime I have text field under the User_ID it goes next to it and cuts it off :S


         <p><label for="null"> null: </label><input type="text" name="null" /></p>
    <p><label for="goal_scored">Goal Scored: </label><input type="text" name="Goal_Scored" /></p>
    <p><label for="assist">assist: </label><input type="text" name="assist"  /></p>
    <p><label for="clean_sheets">clean sheets: </label><input type="text" name="clean_sheets" /></p>
    <p><label for="yellow_card">yellow card: </label><input type="text" name="yellow_card" /></p>
    <p><label for="red_card">red card: </label><input type="text" name="red_card" /></p>

    <p><input type="submit" name="submit" value="Update" /></p></h2>
</form>
</div>
</body>
</html>

如果有人可以帮助了解如何进入下一阶段,将不胜感激谢谢 x

4

1 回答 1

0

与其把时间花在像 AJAX 这样复杂的东西上,我建议使用带有查询的简单页面路线,例如user.php?id=1.

制作一个user.php文件(如您的文件),如果id已设置(if isset($_GET['id']))从数据库中选择该用户(当然,在清理了您的输入之后)select * from users where id = $id(我当然假设您id对每个用户都有一个)。

您仍然可以拥有<select>,但请记住使用 关闭它</select>。你可能会得到这样的结果:

<form method="get">
  <label for="user">Select user:</label>
  <select name="id" id="user">
    <option value="1">User 1</option>
    ...
  </select>
  <submit name="submit" value="Select user" />
</form>

这将发送?id=<id>到当前页面,然后您可以填写表格。如果您还想编辑该数据,请创建一个新表单,其中填充有代码的数据,<input type="text" name="goal_scored" value="<?php echo $result['goal_scored']; ?>" />然后确保method="post"监听isset($_POST['submit'])并更新您的数据库。

一个例子:

<?php
// init
// Use mysqli_ instead, mysql_ is deprecated
$result = mysqli_query($link, "SELECT id, name FROM users");
// Create our select
while ( $row = mysqli_fetch_array($link, $result, MYSQL_ASSOC) ) {?>
  <option value="<?php echo $result['id']; ?>"><?php echo $result['name'] ?></option>
<?php}

// More code ommitted

if (isset($_GET['id'])) {
  $id = sanitise($_GET['id']); // I recommend creating a function for this,
                               // but if only you are going to use it, maybe
                               // don't bother.
  $result = mysqli_query($link, "SELECT * FROM users WHERE id = $id");
  // now create our form.

  if (isset($_POST['submit'])) {
    // data to be updated
    $data = sanitise($_POST['data']);
    // ...

    mysqli_query($link, "UPDATE users SET data = $data, ... WHERE id = $id");
    // To avoid the 'refresh to send data thing', you might want to do a
    // location header trick
    header('Location: user.php?id='.$id);
  }
}

请记住,这只是我正在谈论的想法的一个示例,很多代码已被省略。我通常不喜欢在标签之外编写真正的 HTML <?php ?>,但我猜它可以工作。特别是对于较小的东西。

于 2013-03-09T16:03:50.700 回答