-2

I have this problem with mysql on phpmyadmin databases that I want to update my database, for instance change the username, but with the current code I have, if I leave something empty, it updates the database with empty value, I wanted to change this that IF the post is empty it doesnt update the database to empty space

here is my code:

<!doctype html>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "eerstedatabase";

//create connection
$connection = new mysqli($servername, $username, $password, $dbname);

if ($_POST) {

//check connection
if ($connection->connect_error) {
    die("Connection failed: " . $connection->connect_error);
}
//if these posts are empty it updates them to empty in the database aswell
$sql = "UPDATE gebruikers SET Gebruikersnaam='" . $_POST['Gebruikersnaam'] . "',
Wachtwoord='" . $_POST['Wachtwoord'] . "',
Email='" . $_POST['Email'] . "' 
WHERE ID='" . $_POST['ID'] . "' ";

if ($connection->query($sql) === TRUE) {
    echo "Record updated successfully";

    include 'Opdracht1.php';
} else {
    echo "Error updating record: " . $conn->error;
}
}
else
{

?>
<html>
<head>
<meta charset="utf-8">
<title>Naamloos document</title>
</head>

<body>
<center>
<table>
<form name="update" action="OpdrachtDW6.php" method="POST">
  <tr>
    <td>ID</td>
    <td><input type="text" name="ID" rquired /></td>
  </tr>
  <tr>
    <td>Gebruikersnaam</td>
    <td><input type="text" name="Gebruikersnaam" required /></td>
  </tr>
  <tr>
    <td>Wachtwoord</td>
    <td><input type="text" name="Wachtwoord" required /></td>
  </tr>
  <tr>
    <td>Email</td>
    <td><input type="text" name="Email" required /></td>
  </tr>
  <tr>
    <td><input type="submit" value="Updaten" /></td>
  </tr>
  </td>
</form>
</center>
</body>
</html>

<?php
}
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

5 Answers5

0

You need to check the value of the $_POST fields on the server side.

$errs = false;

if ($_POST['ID'] == "") { 
   $errs = true;
   echo "ID Field Missing";
}
if ($_POST['Gebruikersnaam'] == "") { 
   $errs = true;
   echo "Gebruikersnaam Field Missing";
}

/* And so on... */

if (!$errs) {
    //check connection
    if ($connection->connect_error) {
       die("Connection failed: " . $connection->connect_error);
    }

    $sql = "UPDATE gebruikers SET Gebruikersnaam='" . $_POST['Gebruikersnaam'] . "',
    Wachtwoord='" . $_POST['Wachtwoord'] . "',
    Email='" . $_POST['Email'] . "' 
    WHERE ID='" . $_POST['ID'] . "' ";

    if ($connection->query($sql) === TRUE) {
       echo "Record updated successfully";

       include 'Opdracht1.php';
    } else {
       echo "Error updating record: " . $conn->error;
    }
}
chris85
  • 23,846
  • 7
  • 34
  • 51
omnichad
  • 1,635
  • 1
  • 11
  • 10
  • yes, but, this means that you need to fill in those forms, i want it to be that if you dont fill them in the code doesnt update the database – Jake Imhoff Mar 31 '16 at 15:45
  • You have them set as required in your HTML code. Most web browsers won't give the user the choice anyway. You must remove `required` from the form fields. And if you want it to silently fail with no error messages, just remove the lines that echo an error out to the user. – omnichad Mar 31 '16 at 16:42
0

you can check if the POST values are empty or not:

if(empty($_POST['Email'])
    // Do something

There are other options like check the length of string, if the name is too short, maybe it is not acceptable. Hope this help.

Tu DUONG
  • 94
  • 3
0

You can use this logic

if(isset($_POST['Email'])){
    //post email is not empty! and you can insert in database!
    if ($connection->query($sql) === TRUE) {
        echo "Record updated successfully";
        include 'Opdracht1.php';
    } else {
        echo "Error updating record: " . $conn->error;
    }
}
Han Arantes
  • 775
  • 1
  • 7
  • 19
0

Here is the (basic) logic to check against (not) empty fields.

Sidenote: See comments in code to process as desired.

if( !empty($_POST['ID']) 

 && !empty($_POST['Gebruikersnaam']) 
 && !empty($_POST['Wachtwoord']) 
 && !empty($_POST['Email']) 

  )

 {

 // Execute the query

 }

 else {

 // kill the process or do something else

 }

If you only want to perform the UPDATE if only the email is empty, then simply do:

if(!empty($_POST['Email'])) {

    $sql = "UPDATE gebruikers ...

    // Rest of your code

}

Consult the following on PHP's logical operators:

and empty():


Footnotes:

Your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements.

Also on doing an UPDATE and to check if it truly was successful, use mysqli_affected_rows():

as if ($connection->query($sql) === TRUE) could give you a false positive.

You can also alter your column(s) to not accept NULL values.

Consult http://dev.mysql.com/doc/refman/5.7/en/working-with-null.html

and making it/them as NOT NULL.


On an added note, this if ($_POST) { could be changed to if(isset($_POST['submit'])){ and adding the submit name attribute to your submit button.

<input type="submit" name="submit" value="Updaten" />
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Thank you for your help, im trying this out atm, this will not update the fields in my database if they're empty – Jake Imhoff Mar 31 '16 at 16:08
  • @JakeImhoff Exactly and you're welcome. On an added note and as I posted in comments, you can also alter your column(s) to not accept NULL values and just deal on the level error. Plus, and something I will add to my answer is to use `mysqli_affected_rows()` http://php.net/manual/en/mysqli.affected-rows.php for actual truthness if the update was truly successful. – Funk Forty Niner Mar 31 '16 at 16:09
  • Sidenote to editing: I appreciate the edits for my answer but it's fine just the way it is, *thanks*. I'll edit the first line, there ;-) – Funk Forty Niner Mar 31 '16 at 16:17
  • @JakeImhoff So, how are you making out? I also made a few edits to my answer so you might want to reload it. – Funk Forty Niner Mar 31 '16 at 16:30
0

What I've done before is build my query into variables, then if a variable is empty it will skip that bit of the query.

For example

if($_POST['fielda']) {
    $fieldasql = "fielda = '".$_POST['fielda']."',";
} else {
    $fieldasql = "";  
}

if($_POST['fieldb']) {
    $fieldbsql = "fieldb = '".$_POST['fieldb']."',";
} else {
    $fieldbsql = "";  
}

then your query would be:

$sql = "UPDATE gebruikers SET " . $fieldasql . " " . $fieldbsql;
Zoe Epix
  • 63
  • 1
  • 10