简体   繁体   English

一次从数据库更新多个记录

[英]Updating multiple records from database at once

I just added the names of those undefined index in my edit_mision_vision.php. 我只是在我的edit_mision_vision.php中添加了那些未定义索引的名称。 I have updated my code and it's working now. 我已经更新了代码,现在可以正常工作了。 Thanks for the help :) 谢谢您的帮助 :)

update_mission_vision.php // where the update happens and where the notice showed (but it is fixed now) that says vision, mission, and values are undefined update_mission_vision.php //更新发生的位置以及显示愿景,任务和价值观的通知未显示(但现在已修复)的位置

<?php
    $conn=mysql_connect("localhost", "root");
    mysql_select_db("alumni", $conn);
    if (! $conn)
    {
        DIE('Could not connect: ' . mysql_error());
    }

    if(isset($_POST['update']))
    {
    $vision = $_POST['vision'];
    $mission = $_POST['mission'];
    $values = $_POST['values']; 

    $query="UPDATE about SET description='$vision' WHERE title='Vision'";
    $retval = mysql_query( $query, $conn );
    $query1="UPDATE about SET description='$mission' WHERE title='Mission'";
    $retval = mysql_query( $query1, $conn );
    $query2="UPDATE about SET description='$values' WHERE title='Values'";
    $retval = mysql_query( $query2, $conn );

    if(! $retval )  {
        DIE('Could not enter data: ' . mysql_error());
    }else{
        echo "<script type='text/javascript'>alert('Changes saved!!');
                    window.location.assign('/alumni/mission_vision.php');</script>";
    }
    }
    if (isset($_POST['cancel']))
    {
        header("Location: /alumni/admin_profile.php");
    }
    mysql_close($conn);
?>

edit_mission_vision.php where i edit the mission, vision, and values description // this is where i added the name="vision", name="mission", name="values" and that solved my problem. edit_mission_vision.php ,我在其中编辑任务,远景和值描述,// 在这里添加了name =“ vision”,name =“ mission”,name =“ values”,从而解决了我的问题。

<form action="/alumni/admin/update_mission_vision.php" method="post">
<?php
    include_once 'db.php';
    $sql="SELECT * FROM about";
    $result_set=mysql_query($sql);
    while($row=mysql_fetch_array($result_set))
    {
?>

<?php if($row['title']=='Vision') { ?>
 <label>Vision</labrl><br><br>
<textarea id="vision" name="vision" maxlength="1000" height="1000000px" rows="10" cols="100"><?php echo $row['description']; ?></textarea>
<br><br>
<?php  } ?>

<?php if($row['title']=='Mission') { ?>
<label>Mision</label><br><br>
<textarea id="mission" name="mission" maxlength="1000" height="1000000px" rows="10" cols="100"><?php echo $row['description']; ?></textarea>
<br><br>
<?php  } ?>

<?php if($row['title']=='Values') { ?>
<label>Values</label><br><br>
<textarea id="values" name="values" maxlength="1000" height="1000000px" rows="10" cols="100"><?php echo $row['description']; ?></textarea>
<br><br>

<?php  } ?>


  <?php
    }
    ?>

    <input type="submit" name="update" value="Save Changes"/>
    <input type="submit" name="cancel" value="Cancel"/>
</form>

replace this code with your "edit_mission_vision.php" file 将此代码替换为您的“ edit_mission_vision.php”文件

<form action="/alumni/admin/update_mission_vision.php" method="post">
<?php
    include_once 'db.php';
    $sql="SELECT * FROM about";
    $result_set=mysql_query($sql);
    while($row=mysql_fetch_array($result_set))
    {
?>

<?php if($row['title']=='Vision') { ?>
 <label>Vision</labrl><br><br>
<textarea name="vision" id="vision" maxlength="1000" height="1000000px" rows="10" cols="100"><?php echo $row['description']; ?></textarea>
<br><br>
<?php  } ?>

<?php if($row['title']=='Mission') { ?>
<label>Mision</label><br><br>
<textarea name="mission" id="mission" maxlength="1000" height="1000000px" rows="10" cols="100"><?php echo $row['description']; ?></textarea>
<br><br>
<?php  } ?>

<?php if($row['title']=='Values') { ?>
<label>Values</label><br><br>
<textarea name="values" id="values" maxlength="1000" height="1000000px" rows="10" cols="100"><?php echo $row['description']; ?></textarea>
<br><br>

<?php  } ?>


  <?php
    }
    ?>

    <input type="submit" name="update" value="Save Changes"/>
    <input type="submit" name="cancel" value="Cancel"/>
</form>

if you get same value in all the three records then you need to create array and then update array in your query via while loop.. and add "[]" after name attribute of textarea. 如果在所有三个记录中获得相同的值,则需要创建数组,然后通过while循环在查询中更新数组。并在textarea的name属性后面添加“ []”。 hope it will help. 希望会有所帮助。

1) you need to define name in your all textareas. 1)您需要在所有文本区域中定义名称。

2) Why you need three queries in order to update single field? 2)为什么您需要三个查询才能更新单个字段?

Try this query : 试试这个查询:

update `about` set `description` =  
                  case 
                       when title='Vision' then '$vision' 
                       when title='Mission' then '$mission' 
                       when title='Values' then '$values' 
                  end

Side Note : Stop using mysql_* . mysql_* :停止使用mysql_* It's no more available. 它不再可用。 use mysqli_* or PDO . 使用mysqli_*PDO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM