简体   繁体   English

如何显示表格中的值,进行修改,然后再次保存

[英]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.

相关问题 从一个表中选择所有值,从另一个表中选择一些值,并用php显示它们 - select all the values from one table and some values from another table and display them with php 如何从表中获取记录并在另一个视图中显示它们 - how to fetch the a record from a table and display them in another view 如何从单选按钮获取多个值,然后将它们分配给单个变量并显示在页面上? - How can I get multiple values from a radio button and then assign them into single variables and display them on a page? 如何从数据库中获取值,求和并在codeigniter中显示? - How can I grab values from a database, sum them, and display them in codeigniter? 如何从两个 API 调用中获取数据,将它们合并并与我的其他数据一起显示在表格中? - How to get data from two API calls, merge them and display them in a table with my other data? 如何从url中检索多个值并使用相同的id再次将它们回显 - how to retrieve multiple values from url and echo them out again with the same id 如何比较2个数组并将它们显示在一张表中 - How to compare 2 array and display them in one table 从表中获取多个值,然后回显它们 - Get multiple values from table, then echo them 从表中获取值并回显它们 - Taking values from a table and echoing them out 如何显示或保存电子邮件,而不是在本地主机上实际发送电子邮件? - How to display or save emails instead of actual sending them on localhost?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM