如何使用php更新数据库中的数据

时间:2017-02-18 06:29:09

标签: php database

我当前正在创建一个edit.php页面,该页面使用户能够编辑用户想要的指定数据。我按照教程,但它没有给出我想要的结果。当我运行edit.php时,它只是显示我想要的但是在我点击submit按钮后,数据库中的数据没有任何变化。

这是代码:

    <?php
    require('db.php');
    include("auth.php");
    $student_ID=$_REQUEST['student_ID'];
    $query = "SELECT * from student_info where student_ID='$student_ID'"; 
    $result = mysqli_query($con, $query) or die ( mysqli_error());
    $row = mysqli_fetch_assoc($result);
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Update Student</title>
    <link rel="stylesheet" href="css/style.css" />
    </head>
    <body>
    <div class="form">
    <p><a href="dashboard.php">Dashboard</a> 
     | <a href="insert.php">Insert New Record</a> 
     | <a href="logout.php">Logout</a></p>
    <h1>Update Student</h1>
    <?php
    $status = "";
    if(isset($_POST['submit']) && $_POST['submit']==1)
    {
    $student_ID=$_POST['student_ID'];
    $school_ID =$_POST['school_ID'];
    $student_name =$_POST['student_name'];
    $student_class =$_POST['student_class'];
    mysqli_query($con, $update) or die(mysqli_error());
    $status = "Student Record Updated Successfully. </br></br>
    <a href='view.php'>View Updated Record</a>";
    echo '<p style="color:#FF0000;">'.$status.'</p>';
    }else {
    ?>
    <div>
    <form name="form" method="post" action=""> 
    <p><input type="text" name="student_ID" placeholder="Enter Student ID"
    required value=<?php echo $row['student_ID'];?> /></p>
    <p><input type="text" name="school_ID" placeholder="Enter School ID" 
    required value="<?php echo $row['school_ID'];?>" /></p>
    <p><input type="text" name="student_name" placeholder="Enter Student Name" 
    required value="<?php echo $row['student_name'];?>" /></p>
    <p><input type="text" name="student_class" placeholder="Enter Student     Class"
    required value="<?php echo $row['student_class'];?>" /></p>
    <p><input name="submit" type="submit" value="Submit" /></p>
    </form>
    <?php } ?>
    </div>
    </div>
    </body>
    </html>

如果您有任何建议或任何改进或任何链接可以帮助我解决此问题,请执行此操作。

这是作者给出的原始代码:

    <?php
    require('db.php');
    include("auth.php");
    $id=$_REQUEST['id'];
    $query = "SELECT * from new_record where id='".$id."'"; 
    $result = mysqli_query($con, $query) or die ( mysqli_error());
    $row = mysqli_fetch_assoc($result);
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Update Record</title>
    <link rel="stylesheet" href="css/style.css" />
    </head>
    <body>
    <div class="form">
    <p><a href="dashboard.php">Dashboard</a> 
    | <a href="insert.php">Insert New Record</a> 
    | <a href="logout.php">Logout</a></p>
    <h1>Update Record</h1>
    <?php
    $status = "";
    if(isset($_POST['new']) && $_POST['new']==1)
    {
    $id=$_REQUEST['id'];
    $trn_date = date("Y-m-d H:i:s");
    $name =$_REQUEST['name'];
    $age =$_REQUEST['age'];
    $submittedby = $_SESSION["username"];
    $update="update new_record set trn_date='".$trn_date."',
    name='".$name."', age='".$age."',
    submittedby='".$submittedby."' where id='".$id."'";
    mysqli_query($con, $update) or die(mysqli_error());
    $status = "Record Updated Successfully. </br></br>
    <a href='view.php'>View Updated Record</a>";
    echo '<p style="color:#FF0000;">'.$status.'</p>';
    }else {
    ?>
    <div>
    <form name="form" method="post" action=""> 
    <input type="hidden" name="new" value="1" />
    <input name="id" type="hidden" value="<?php echo $row['id'];?>" />
    <p><input type="text" name="name" placeholder="Enter Name" 
    required value="<?php echo $row['name'];?>" /></p>
    <p><input type="text" name="age" placeholder="Enter Age" 
    required value="<?php echo $row['age'];?>" /></p>
    <p><input name="submit" type="submit" value="Update" /></p>
    </form>
    <?php } ?>
    </div>
    </div>
    </body>
    </html>

顺便说一句,我不知道这段代码的含义。我只是按照教程。

    if(isset($_POST['submit']) && $_POST['submit']==1)

我是这个编码世界的新手。感谢

4 个答案:

答案 0 :(得分:0)

你的情况是错误的

if(isset($_POST['submit']) && $_POST['submit']==1)

应该是

if(isset($_POST['submit']) && $_POST['submit']=="Submit")

因为提交名称的输入值为&#34;提交&#34;

答案 1 :(得分:0)

这意味着// if(isset($ _ POST [&#39;提交&#39;])&amp;&amp; $ _POST [&#39;提交&#39;] == 1)当该帖子传递提交值并且帖子提交值匹配1以下代码应该运行。按照你的说法更新数据库,如果它有效,请联系更多

        $updatequery = "UPDATE student_info SET student_ID = '$student_ID', school_ID = '$school_ID', student_name= '$student_name', student_class='$student_class'";

        $runthequery($con, $updatequery);
        if($runthequery){
         means if run the run variable is successfull do something
            }

答案 2 :(得分:0)

因为代码中没有更新查询:

$update = "UPDATE student_info SET school_ID='{$school_ID}', student_name = '{$student_name}', student_class = '{$student_class}'
WHERE student_ID='$student_ID'"; 

mysqli_query($con, $update);

尝试类似这样的事情:

<?php
require('db.php');
include("auth.php");
$student_ID=$_REQUEST['student_ID'];
$query = "SELECT * from student_info where student_ID='$student_ID'"; 
$result = mysqli_query($con, $query) or die ( mysqli_error());
$row = mysqli_fetch_assoc($result);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Update Student</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="form">
<p><a href="dashboard.php">Dashboard</a> 
 | <a href="insert.php">Insert New Record</a> 
 | <a href="logout.php">Logout</a></p>
<h1>Update Student</h1>
<?php
$status = "";
if(isset($_POST['submit']))
{
$student_ID=$_POST['student_ID'];
$school_ID =$_POST['school_ID'];
$student_name =$_POST['student_name'];
$student_class =$_POST['student_class'];
$update = "UPDATE student_info SET school_ID='".$school_ID."', student_name = '".$student_name."', student_class = '".$student_class."' WHERE student_ID='".$student_ID."'"; 
mysqli_query($con, $update) or die(mysqli_error());
$status = "Student Record Updated Successfully. </br></br>
<a href='view.php'>View Updated Record</a>";
echo '<p style="color:#FF0000;">'.$status.'</p>';
}else {
?>
<div>
<form name="form" method="post" action=""> 
<p><input type="text" name="student_ID" placeholder="Enter Student ID"
required value=<?php echo $row['student_ID'];?> /></p>
<p><input type="text" name="school_ID" placeholder="Enter School ID" 
required value="<?php echo $row['school_ID'];?>" /></p>
<p><input type="text" name="student_name" placeholder="Enter Student Name" 
required value="<?php echo $row['student_name'];?>" /></p>
<p><input type="text" name="student_class" placeholder="Enter Student     Class"
required value="<?php echo $row['student_class'];?>" /></p>
<p><input name="submit" type="submit" value="Submit" /></p>
</form>
<?php } ?>
</div>
</div>
</body>
</html>

isset($ _ POST ['submit'])=&gt; isset()函数用于检查是否设置了变量。如果单击了“提交”按钮,则isset()函数返回true。

答案 3 :(得分:0)

$_POST$_GET类似。当对页面进行调用时,它们将被提供给PHP文件。您<form>操作为空,因此它将表单数据发送给自己。当用户看到表单时,页面加载一次GET,第二次加载数据时页面加载。

if(isset($_POST['submit']) && $_POST['submit']==1)

此行是一个条件,表示仅在接收表单数据时运行代码。现在你的代码没有缩进,所以你很难阅读。确保使用带语法突出显示的代码编辑器并修复缩进。它会让你的生活更轻松。

所有这一切......您未在示例中定义$update,因此您实际上并未执行任何SQL。