简体   繁体   English

更新一个特定数据会使用PHP更新MYSQL中的整个记录

[英]Updating one particular data updates the entire record in MYSQL using PHP

I wanna update just one data from a record using php form but the thing is, when i do that, the rest of the data gets removed from the record.. What do i do :/ here are my codes for updating. 我想使用php表单更新记录中的一个数据,但事实是,当我这样做时,其余数据将从记录中删除。我该怎么办:/这是我的更新代码。 What is the mistake i am making.. I am very confused. 我犯的错误是什么..我很困惑。 Would really appreciate some help. 非常感谢您的帮助。

<?php

include('db.php');

if(isset($_POST['update']))
{

   $hostname = "localhost";
   $username = "root";
   $password = "";
   $databaseName = "winc sports";

   $connect = mysqli_connect($hostname, $username, $password, $databaseName);



   $id = $_POST['id'];
   $fname = $_POST['fname'];
   $lname = $_POST['lname'];
   $age = $_POST['age'];
   $country=$_POST['country'];
    $phone=$_POST['phone'];
    $email=$_POST['email'];

    $select = "SELECT * FROM studens WHERE id = '$id'"; 
    $selected = mysqli_query($connect, $select); 
    $row = mysqli_fetch_assoc($selected); 
    if (empty($_POST['fname'])) {$fname = $row['fname'];} else {$fname = $_POST['fname'];}
    if (empty($_POST['country'])) 
    {
    $country = $row['country'];
    } 
    else {
    $country = $_POST['country'];
    }
    if (empty($_POST['id'])) {
    $id = $row['id'];
        } 
    else {
    $id = $_POST['id'];
    }
    if (empty($_POST['age'])) {$age = $row['age'];} else {$age = $_POST['age'];}
    if (empty($_POST['phone'])) {$phone = $row['phone'];} else {$phone = $_POST['phone'];}
    if (empty($_POST['email'])) {$email = $row['email'];} else {$email = $_POST['email'];}
   $query = "UPDATE students SET Fname= '$fname', Lname = '$lname', Nationality = '$country', PhoneNumber = '$phone', Email= '$email', Age = '$age' WHERE Id = '$id'";

   $result = mysqli_query($connect, $query);
   var_dump($result);
   if($result)
   {
       echo 'Data Updated';
   }else
   {
       echo 'Data Not Updated';
   }
   mysqli_close($connect);
}

?>

<!DOCTYPE html>

<html>

    <head>

        <title>PHP INSERT DATA USING PDO</title>

        <meta charset="UTF-8">

        <meta name="viewport" content="width=device-width, initial-scale=1.0">

    </head>
    <body>
        <form action="updating.php" method="post">
    <input type="text" name="id"  placeholder="Enter new ID"><br><br>
    <input type="text" name="fname"  placeholder="Enter new First Name"><br><br>
    <input type="text" name="lname"  placeholder="Enter new Last Name"><br><br>
    <input type="number" name="age"  placeholder="Enter new age" min="13" max="90"><br><br>
    <input type="text" name="country"  placeholder="Enter new Nationality"><br><br>
    <input type="number" name="phone"  placeholder="Enter new Phone Number"><br><br>
    <input type="text" name="email"  placeholder="Enter new Email"><br><br>
    <input type="submit" name="update" value="update">
    </form>

    </body>

</html>

The select statement is fetching data from a table called studens . select语句正在从称为studens的表中获取数据。 This looks like a typo of the actual table so it won't actually fetch any results for you to update. 这看起来像是实际表的错字,因此它实际上不会获取任何要更新的结果。 Thus, the data you wind up updating the table with is empty. 因此,您最终用来更新表的数据为空。 Rename the initial select table to students and it should properly fetch the data. 将初始选择表重命名为学生 ,它应该正确获取数据。

Also, please look into prepared statements or various other methods to sanitize inputs. 另外,请查看准备好的语句或其他各种方法来清理输入。 Using POST variables directly in a query makes you extremely vulnerable to SQL Injection. 直接在查询中使用POST变量使您极易受到SQL注入的攻击。

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

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