-1

I want to update my students table in MySQL using PHP, but it isn't working and I don't know what mistake I've made :/

Here is my code.

BTW I put quotes around id because its varchar not int.

It just says the data isn't updated.

<?php

// PHP code to update data from MySQL database table

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

   $hostname = "localhost";
   $username = "root";
   $password = "";
   $databaseName = "students";

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

   // get values form input text and number

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

   // mysql query to Update data
   $query = "UPDATE `students` SET `fname`='".$fname."',`lname`='".$lname."',`Nationality`='".$country."',`PhoneNumber`=$phone,`Email`='".$email."',`age`= $age WHERE `id` = '$id'";

   $result = mysqli_query($connect, $query);

   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>
Funk Forty Niner
  • 73,764
  • 15
  • 63
  • 131
  • Use `var_dump` for debbuging purposes. What do you get if you use `var_dump($result)` ? – Ionut Nov 30 '16 at 22:53
  • bool(false) this is what i am getting.. But i dont understand how to fix it or anything :/ – Buffy Anne-summers Nov 30 '16 at 22:54
  • The first check would be to echo the actual $query and eye ball it and run it under phpmyadmin in the SQL Tab against the database to see what fun things it has to say. – TimBrownlaw Nov 30 '16 at 22:55
  • Then the problem is with your query. Check it very carefully. Check if all the parameters names are the same as in the database, because that query seems fine to me. – Ionut Nov 30 '16 at 22:56
  • 1
    Also, to lessen problems, you can drop all the concatination you're doing in the query. `"UPDATE students SET fname = '$fname'` and so on will work just fine. And, of course, you should learn to use prepared queries. – junkfoodjunkie Nov 30 '16 at 22:56
  • Does the table have the same name as the database? – D. Fraga Nov 30 '16 at 22:58
  • @D.Fraga yea it does :/ – Buffy Anne-summers Nov 30 '16 at 23:00
  • You shouldn't be building up your queries using string concatenation like you are since it's vulnerable to SQL injection attacks. Paramaterizing your query will solve this problem. See http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Brian Nov 30 '16 at 23:03
  • Is the $phone 'string' bool? – D. Fraga Nov 30 '16 at 23:03
  • @D.Fraga Thanks now its working.. Yea been studying for hours.. Losing my concentration lol btw what if i just want to update one field? Cos with my method, u have to update everything.. – Buffy Anne-summers Nov 30 '16 at 23:03

2 Answers2

0

I think your $query variable is bad. Check out the following query. This should get your problem solved.

$query = "UPDATE students SET fname= '$fname', lname = '$lname', Nationality = '$country', PhoneNumber = '$phone', Email= '$email', age = '$age' WHERE id = '$id'";

I assume that your mistake is you didn't wrap $age with single quotes. You need to wrap it with single quotes no matter if it is integer or string as long as it is value you need to update.

Wolverine
  • 1,634
  • 1
  • 15
  • 17
bocanjis
  • 1
  • 3
  • Thanks.. Yea been studying for hours.. Losing my concentration lol btw what if i just want to update one field? Cos with my method, u have to update everything.. – Buffy Anne-summers Nov 30 '16 at 23:02
  • try adding in top : – bocanjis Nov 30 '16 at 23:10
  • $select = "SELECT * from students WHERE id = '$id'"; – bocanjis Nov 30 '16 at 23:11
  • Didn't work :/ sorry i am new to php and mysql so its confusing for me lol – Buffy Anne-summers Nov 30 '16 at 23:16
  • @bocanjis Oh man! You seriously need to follow some rules before you post answer. Format your answer as per the rules. – Wolverine Nov 30 '16 at 23:16
  • $select = "SELECT * FROM studens WHERE id = '$id'"; $selected = mysqli_query($connect, $select); $row = mysqli_fetch_assoc($selected); and then add " if (empty($_POST['fname'])) {$fname = $row['fname']} else {$fname = $_POST['fname']}" just add that to every variable and change $_POST key and $row key. For example next should be : " if (empty($_POST['country'])) {$country = $row['country']} else {$country = $_POST['country']}". I know this is a little bit messed but I hope it will help you – bocanjis Nov 30 '16 at 23:17
  • `'$var'` and `'".$var."'` both do the same thing. I don't see the reason for the answer. – Funk Forty Niner Nov 30 '16 at 23:19
0

Your query is quite wrongly written :

It should be :

$query = "UPDATE `students` SET `fname`='" . $fname . "',`lname`='" . $lname . "',`Nationality`='" . $country . "',`PhoneNumber`=" . $phone . ",`Email`='".$email."',`age`= " . $age . " WHERE `id` = '" . $id . "'";

N.B. : Assuming your age and phone is an INT type data.

After this query executing $query will be :

UPDATE `students` SET `fname`='john',`lname`='adams',`Nationality`='usa',`PhoneNumber`=456123789,`Email`='aa@bb.com',`age`= 23 WHERE `id` = '1'

If age and phone is CHAR then the query will be like this :

$query = "UPDATE `students` SET `fname`='" . $fname . "',`lname`='" . $lname . "',`Nationality`='" . $country . "',`PhoneNumber`='" . $phone . "',`Email`='".$email."',`age`= '" . $age . "' WHERE `id` = '" . $id . "'";

After this query executing $query will be :

UPDATE `students` SET `fname`='john',`lname`='adams',`Nationality`='usa',`PhoneNumber`='456123789',`Email`='aa@bb.com',`age`= '23' WHERE `id` = '1'

Hope it solves the issue.

Thanks and Regards