编辑mysql的按钮

时间:2015-03-30 10:46:29

标签: php mysql alter

我让我的数据库工作,我可以添加我的数据并再次获得它。这是完美的,因为这是我第一次使用它,它开启了许多可能性。所以我的下一个项目是用一个按钮改变我在MySQL中的表。到现在为止看起来像这样:

Image

我可以添加日期,日期,时间和全天。但是我希望有可能在当天更改fx,如果我在将值添加到数据库时出错。我开始在右侧制作一个编辑按钮。所以加班我做了一个新的行,会有一个新的编辑按钮。但有人知道如何将我的按钮设置为ALTER TABLE查询吗?或者也许提示如何做到这一点?

最好的问候 来自Mads

编辑代码:

我在数据库p_id中创建了主键。我也从p_id

返回
  <html>

<head>
    <link rel="stylesheet" type="text/css" href="css/arrangeTables.css">

</head>

<body>

    <form method="post">
        <h3>Add your worktime to database</h3><br>
            <input type="date" name="date"><br><br>
            <select name="day">
                <option value="Mandag">Mandag</option>
                <option value="Tirsdag">Tirsdag</option>
                <option value="Onsdag">Onsdag</option>
                <option value="Torsdag">Torsdag</option>
                <option value="Fredag">Fredag</option>
                <option value="Lørdag">Lørdag</option>
                <option value="Søndag">Søndag</option>
            </select>
            <input type="time" name="fromtime">
            <input type="time" name="totime">
            <input type="submit" value="submit"><br><br>

    </form>


</body>

<?php
$username = "root";
$password = "root";
$hostname = "127.0.0.1:3306"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
 or die("Unable to connect to MySQL");
echo "Connected to MySQL<br><br>";

//select a database to work with
$selected = mysql_select_db("danskebank",$dbhandle) 
  or die("Could not select any database");

// Insert to database
$date = $_POST['date'];
$day = $_POST['day'];
$fromtime = $_POST['fromtime'];
$totime = $_POST['totime'];

$sql = "INSERT INTO addWorkTime(date, day, fromtime, totime) VALUES('$date', '$day', '$fromtime', 'totime')";

$result = mysql_query($sql);

//Return records from database
$result = mysql_query("SELECT date, day, fromtime, totime FROM addWorkTime");
?>



    <!-- Return from the database -->
    <h3>Return from database:</h3><br>

  <!-- headers -->
        <tr>
            <th class="column1">Date</th>
            <th class="column2">Day</th>
            <th class="column3">From</th>
            <th class="column4">To</th>
        </tr>

        <!-- Now a row for each new set of data, here you probably need to
        loop through some data set you retrieve from the database -->
        <?php while($row = mysql_fetch_array($result)): ?>
        <table>
          <tr>
            <td class="resultcolumn4"><?php echo $row{'p_id'};?></td>
            <td class="resultcolumn1"><?php echo $row{'date'};?><br></td>
            <td class="resultcolumn2"><?php echo $row{'day'};?></td>
            <td class="resultcolumn3"><?php echo $row{'fromtime'};?></td>
            <td class="resultcolumn4"><?php echo $row{'totime'};?></td>
            <td><a href='link_to_the_add_or_edit?id='.<?php $row['id'] ?></td>

            <?php

            $id=$_GET['id'];

            echo '<input type="hidden" name="name_of_hidden_input" value='.$id.'>';
            //and the rest of the form

                if($_GET['submit']){

                    //Some mysql injection prevention first
                    update danskebank SET date=['?'] where id= $_GET['name_of_hidden_input']
                }
            ?>
          </tr>
          <?php endwhile; ?>
    </table>
</html>

2 个答案:

答案 0 :(得分:2)

要编辑特定行,您需要在mysql表中使用主键。例如,你称之为:id。现在您还需要从表中获取id:SELECT id, date, day, fromtime, totime FROM addWorkTime

使用while循环中的$row['id'];并将<input type="button" value="Edit">替换为:<a href='link_to_the_add_or_edit?id='.<?php $row['id'] ?>现在您的网址将如下所示:link_to_the_add_or_edit?id = 1您可以使用:{{1在link_to_the_add_or_edit页面上。现在,当您进入该页面时,确保记住该ID(SESSIONS),以便在填写值时可以在提交操作中使用它。 会话示例:

$_GET['id']
在link_to_the_add_or_edit页面上。在此之后,您可以更新您想要的行(当您提交内容时):

session_start();
$_SESSION['id']=$_GET['id'];

编辑(关于DarkBee的评论):

您可以将$ _GET [&#39; id&#39;]存储在隐藏字段中,而不是在此处使用会话:

update danskebank SET date=['?'] where id= $_SESSION['id']

并在查询中使用:$id=$_GET['id']; echo '<input type="hidden" name="name_of_hidden_input" value='.$id.'>'; //and the rest of the form if($_GET['submit']){ //Some mysql injection prevention first update danskebank SET date=['?'] where id= $_GET['name_of_hidden_input'] }

答案 1 :(得分:0)

如果要编辑行的值,请尝试通过url:

echo '<a href="edit.php?row_id=' . $row_id . '>Edit</a>'; //You should have id for every row

创建edit.php文件,并使用$_GET['row_id']获取行的ID。创建form在其中添加一些input(如下所示:<input type="datetime" />),然后使用另一个php文件处理它。您应该在那里执行UPDATE查询。像这样:

$sql = "UPDATE row SET 
            date     = '$date' ,
            day      = '$day' ,
            fromtime = '$fromtime' ,
            totime   = '$totime'
        WHERE id     = $row_id;
";

mysql_query($sql);

但对于所有这些,您应该为数据库中的每一行id