Update database using a form and php

Im new to PHP and MySQLi so I hope someone can help me.

I want to make a page where I can update info from my database using a form.

This is my PHP codes:

    <?php
// Create connection
require_once "configuration.php";
$Conf = new JConfig;
$conn = mysqli_connect($Conf->host, $Conf->user, $Conf->password, $Conf->db);    
// Create connection

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
?>

<?
$result = mysqli_query($conn, $sql);
error_reporting(E_ALL); 
ini_set('display_errors', 1);
$id = $_GET['id'];
?>


<?php
// php code to Update data from mysql database Table
if(isset($_POST['update']))
{
   // get values form input text and number
   $int_bemerkning = $_POST['int_bemerkning'];
   $navn = $_POST['navn'];

   // mysql query to Update data
   $query = "UPDATE `dykkerkurser_owd` SET `int_bemerkning`='".$int_bemerkning."',`navn`='".$navn."' WHERE `id`='" . $id . "'";
   $result = mysqli_query($connect, $query);
   if($result)
   {
       echo 'Data Updated';
   }else{
       echo 'Data Not Updated';
   }
   mysqli_close($connect);
}
?>

and my form looks like this:

<form action="#" method="post">
    bemerkning:<input type="text" name="int_bemerkning"  value="<?php echo $int_bemerkning; ?>" ><br><br>
    navn:<input type="text" name="navn"   value="<?php echo $navn; ?>" ><br><br>
    <input type="submit" name="update" value="Update Data">
</form>

So Issue is: 1. the form above dont show what is saved in the database using

value="<?php echo $navn; ?>"

and 2. I can not update any info.

Can some one see what im doing wrong?

Use prepared statements for one thing.

Is all of that in the same file?

Yes, its all in the same file. Is that an issue?

Thanks for looking into this.

Move that to the very top of the file.

Thanks.

When I do that I get this error messege:
Warning : mysqli_query(): Empty query in /home/dykkerhu/public_html/staff/staff-owd-kursistinfo-update.php on line 15

Line 15 starts with:
$result = mysqli_query($conn, $sql);
error_reporting(E_ALL);
$id = $_GET[‘id’];

How do I deal with that warning message?

That’s a pretty explicit error message. There is no query.

I see. Can you help me how it then should look like. Im nut sure how the query request should look like and what codes needs to be add. Hope for your advice :slight_smile:

I have no idea what should be there, and if you don’t either, how did it get there? What should it do? If you can’t answer those, then why is it there in the first place?

Ok thanks.

Its placed there because I just used the codes from the page that show the data from the database, I was then hopening that it was the same codes to use again only change it to UPDATE, but I get it working :confused:

There is an issue when beginners copy-paste readymade code.
I speak of experience, never, never, never ever copy code when you’re only starting out.
I am caught in the middle by using intermediate code snippets while only half understanding them, and going through the beginner courses at this stage is quite frustrating. I keep telling myself that I shouldn’t have moved on until I really understood and practised on the earlier levels first.
My advice is that when you’re learning how to code anything, start out from scratch and understand what and why you’re doing it, don’t move on too far ahead until you understand the programming concept you’re working on.

Sponsor our Newsletter | Privacy Policy | Terms of Service