编辑按钮 mysql
edit button to mysql
我的数据库可以正常工作,我可以添加我的数据并再次获取 return。这太完美了,因为这是我第一次让它发挥作用,它开辟了很多可能性。所以我的下一个项目是用一个按钮改变 MySQL 中的 table。到目前为止它看起来像这样:
我可以添加日期、日期、时间和时间。但我希望有可能在当天更改 fx,如果我在将值添加到数据库时出错。我开始在右侧制作一个编辑按钮。所以加班我做了一个新行,会有一个新的编辑按钮。但是有人知道如何将我的按钮分配给 ALTER TABLE 查询吗?或者提示如何操作?
向大家致以最良好的问候
来自 Mads
编辑代码:
我在数据库中做了主键p_id。我还从 p_id
得到一个 return
<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>
如果您想编辑行的值,请尝试通过 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
。
要编辑特定行,您需要 mysql table 中的 主键 。例如你称它为:id。现在您还需要从 table 获取 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'] ?>
现在您的 url 将如下所示:link_to_the_add_or_edit?id =1 并且您可以在 link_to_the_add_or_edit 页面上使用:$_GET['id']
。现在,当您在该页面上时,请确保记住 id(SESSIONS) 以便在填写值时可以在提交操作中使用它。
会话示例:
session_start();
$_SESSION['id']=$_GET['id'];
在 link_to_the_add_or_edit 页面上。在此之后,您可以像这样更新您想要的行(当您提交内容时):
update danskebank SET date=['?'] where id= $_SESSION['id']
编辑(关于 DarkBee 的评论):
除了在此处使用会话,您还可以将 $_GET['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']
}
并在查询中使用:$_GET['name_of_hidden_input'];
我的数据库可以正常工作,我可以添加我的数据并再次获取 return。这太完美了,因为这是我第一次让它发挥作用,它开辟了很多可能性。所以我的下一个项目是用一个按钮改变 MySQL 中的 table。到目前为止它看起来像这样:
我可以添加日期、日期、时间和时间。但我希望有可能在当天更改 fx,如果我在将值添加到数据库时出错。我开始在右侧制作一个编辑按钮。所以加班我做了一个新行,会有一个新的编辑按钮。但是有人知道如何将我的按钮分配给 ALTER TABLE 查询吗?或者提示如何操作?
向大家致以最良好的问候 来自 Mads
编辑代码:
我在数据库中做了主键p_id。我还从 p_id
得到一个 return <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>
如果您想编辑行的值,请尝试通过 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
。
要编辑特定行,您需要 mysql table 中的 主键 。例如你称它为:id。现在您还需要从 table 获取 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'] ?>
现在您的 url 将如下所示:link_to_the_add_or_edit?id =1 并且您可以在 link_to_the_add_or_edit 页面上使用:$_GET['id']
。现在,当您在该页面上时,请确保记住 id(SESSIONS) 以便在填写值时可以在提交操作中使用它。
会话示例:
session_start();
$_SESSION['id']=$_GET['id'];
在 link_to_the_add_or_edit 页面上。在此之后,您可以像这样更新您想要的行(当您提交内容时):
update danskebank SET date=['?'] where id= $_SESSION['id']
编辑(关于 DarkBee 的评论):
除了在此处使用会话,您还可以将 $_GET['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']
}
并在查询中使用:$_GET['name_of_hidden_input'];