[英]How to display values from table, modify them , save them again
I am trying to display values from table and want to modify the values on form submit. 我正在尝试显示表中的值,并想修改表单提交中的值。
here is my code: 这是我的代码:
<?php
$sql = mysql_query("SELECT * FROM TABLENAME");
//Get and store values into new variable
while ($row = mysql_fetch_array($sql)) {
$name[] = $row['name'];
$status[] = $row['status'];
}
//run for loop to identify values //运行循环以识别值
for ($i = 0; $i < count($name); $i++) {
echo "<tr> ";
echo "<td>" . $name[$i] . "</td>";
echo "<td><input name='offer_new_name' size='5' type='text' value='$status[$i]' /></td>";
}
echo '<form action="" method="post">';
echo '<input type="submit" value="Save New Values" name="save_new">';
echo "</form>";
if (isset($_REQUEST['save_new'])) {
for ($i = 0; $i < count($name); $i++) {
echo "name:$name[$i]<br>";
echo "status:$status[$i]";
}
}
When I click Save New Values , it displays the old values ,, taken from db. 当我单击Save New Values时,它将显示取自db的旧值。 Please guide me how to save 请指导我如何保存
echo "<td><input name='new_status' size='5' type='text' value='$status[$i]' /></td>";
into new variable for each name and save them into the same table again. 输入每个名称的新变量,然后将它们再次保存到同一表中。
Please guide me if my approach is wrong towards this requirement. 如果我的方法对这个要求不对,请指导我。
Update1: UPDATE1:
table structure: 表结构:
id name: status:
auto name1 0
auto name2 1
auto name3 0
Where this runs, 在哪里运行,
while ($row = mysql_fetch_array($sql)) {
$name[] = $row['name'];
$status[] = $row['status'];
}
values are stored like this 值像这样存储
$name[0]
$status[0]
$name[1]
$status[1]
$name[2]
$status[2]
I can run update query like this 我可以这样运行更新查询
UPDATE TABLE set status=$status[0] where name= $name[0]
First, the following line should be in the <form></form>
首先,以下行应位于<form></form>
echo "<td><input name='new_status' size='5' type='text' value='$status[$i]' /></td>";
Second, this code block should be on top of your script: 其次,此代码块应位于脚本顶部:
if(isset($_REQUEST['save_new'])){
//Your update query
}
In this code block you need an update query, which updates your database eg 在此代码块中,您需要一个更新查询,该查询更新您的数据库,例如
UPDATE TABLENAME SET a = new_value WHERE b = c;
This is the variable you can access the entered value; 这是您可以访问输入值的变量。
$_POST['new_status'];
Lastly I can strongly recommend to use prepared statements. 最后,我强烈建议您使用准备好的语句。 Otherwise your code will be open for SQL-Injections. 否则,您的代码将为SQL注入打开。
Pseudo code: 伪代码:
<?php
//if the submit button is pressed, update the Database
if (isset($_REQUEST['save_new'])) {
//Update your Database
}
//get the new values from Database
$sql = mysql_query("SELECT * FROM TABLENAME");
//store values into new variable
$name = array();
$status = array();
$id = array();
while ($row = mysql_fetch_array($sql)) {
$name[] = $row['name'];
$status[] = $row['status'];
$id[] = $row['id'];
}
//create HTML-From
echo '<form action="" method="post">';
for ($i = 0; $i < count($name); $i++) {
echo "<tr> ";
echo "<td>" . $name[$i] . "</td>";
echo "<td><input name='new_status' size='5' type='text' value='$status[$i]' /></td>";
}
echo '<input type="submit" value="Save New Values" name="save_new">';
echo "</form>";
?>
This 'new id' can be saved as a hidden input in your script: 可以将这个“新ID”保存为脚本中的隐藏输入:
$indecipherable_id = (int)$id * 7258289382234; //Only a example --> use a more complex algorithm!
Before your update query you can decrypt the id
and select the row. 在更新查询之前,您可以解密id
并选择该行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.