duanjian7343 2017-03-05 09:07
浏览 103
已采纳

如何显示表中的值,修改它们,再次保存它们

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. 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:

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]
  • 写回答

1条回答 默认 最新

  • doujiaozhao2489 2017-03-05 09:15
    关注

    First, the following line should be in the <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 e.g

    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.

    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:

    $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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看