Please Help. Update Record in DB

Im trying to update record in database but when I submit the changes it creates a new record instead… code is below.

<?php
session_start();
error_reporting(0);
include('include/dbconnection.php');
if (strlen($_SESSION['pdaid']==0)) {
  header('location:logout.php');
  } else{

if(isset($_POST['submit']))
  {
    $fullname=$_POST['fullname'];
    $profession=$_POST['profession'];
    $email=$_POST['email'];
    $mobilenumber=$_POST['mobilenumber'];
    $address=$_POST['address'];
    $account_n=$_POST['account_n'];
    $sort=$_POST['sort'];
    $rent=$_POST['rent'];
    $bonus=$_POST['bonus'];
    $notes=$_POST['notes'];
    $bywho=$_POST['bywho'];
    $city=$_POST['city'];
    $admsta=1;
     
    $query=mysqli_query($con, "insert into tbldirectory (FullName,Profession,Email,MobileNumber,Address,account_n,sort,rent,bonus,notes,bywho,City,Status) value('$fullname','$profession','$email','$mobilenumber','$address','$account_n','$sort','$rent','$bonus','$notes','$bywho','$city','$admsta')");
    if ($query) {
    $msg="Client Details Updated Successfully";
  }
  else
    {
      $msg="Something Went Wrong. Please try again";
    }

  
}
  ?>

I have a question when you debug your own code do you have it in that format? I bet the answer is no, so why don’t you put it a format that so people here can find it easier to help you?

It on top fifth from the left </> that will put your code in a proper format and I bet you will get more people to help you.

This is the same code you posted in your last thread about updating a record. It contains an INSERT query, not an UPDATE query. You would need to research what a (my)sql UPDATE query is.

i have tried to change the query but it does not work

We cannot help you with anything you tried unless you post your code. Please post your current code in a reply in this thread.

When posting code, please use bbcode [code]...[/code] tags around it. I have edited your 1st post in this thread so that it is readable.

this is what i have tried

<?php
session_start();
error_reporting(0);
include('include/dbconnection.php');
if (strlen($_SESSION['pdaid']==0)) {
  header('location:logout.php');
  } else{

if(isset($_POST['submit']))
  {
    $fullname=$_POST['fullname'];
    $profession=$_POST['profession'];
    $email=$_POST['email'];
    $mobilenumber=$_POST['mobilenumber'];
    $address=$_POST['address'];
    $account_n=$_POST['account_n'];
    $sort=$_POST['sort'];
    $rent=$_POST['rent'];
    $bonus=$_POST['bonus'];
    $notes=$_POST['notes'];
    $bywho=$_POST['bywho'];
    $city=$_POST['city'];
    $admsta=1;
     
    $query=mysqli_query($con, "UPDATE tbldirectory SET (FullName,Profession,Email,MobileNumber,Address,account_n,sort,rent,bonus,notes,bywho,City,Status) value('$fullname','$profession','$email','$mobilenumber','$address','$account_n','$sort','$rent','$bonus','$notes','$bywho','$city','$admsta')");
    if ($query) {
    $msg="Client Details Updated Successfully";
  }
  else
    {
      $msg="Something Went Wrong. Please try again";
    }

  
}
  ?>

Unfortunately, the programmers who wrote this forum software weren’t very good. The bbcode tags must be on their own lines. I have corrected your post above.

That’s not the correct syntax for an UPDATE query. Programming is an EXACT science. All of the fundamental information you need to do this can be found in the relevant documentation and in countless examples posted on the web - https://dev.mysql.com/doc/refman/8.0/en/update.html

1 Like

I do not understand any of that please help sir :frowning:

hey,
here is the correct format for using the update query.
UPDATE *table_name* SET *column1* = *value1* , *column2* = *value2* , ... WHERE *condition* ;
then put your variables in the “value1” spaces. You are using “values” to insert new values in your table.

We don’t put variables directly into the sql query any more. Use a prepared query instead, and eliminate all the extra code and sql syntax needed to make values safe and get them into the query.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service