Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

Cant seem to add rows into DB

My code:
Code
<form action="admin/rek.php" method="POST" autocomplete="off">
                                   <div class="input-container underlined">
                                       <div class="underline-container">
                                           <input id="username" class="material-input custom-focus" autocomplete="off" autocapitalize="off" type="text" name="gebr" placeholder="Gebruikersnaam" required="">
                                           <div class="highlight-underline"></div>
                                           <div class="base-underline"></div>
                                       </div>
                                   </div>
                                   <div class="input-container underlined">
                                       <div class="underline-container">
                                           <input id="password" class="material-input custom-focus" autocomplete="off" autocapitalize="off" type="password" name="ww" required="" placeholder="Wachtwoord">
                                           <div class="highlight-underline"></div>
                                           <div class="base-underline"></div>
                                       </div>
                                   </div>
                                   <div class="login-call-to-actions">
                                       <button type="submit" id="submitButton" class="material-button orange"><span class="msie-button-fix">Inloggen</span>
                                           <div class="rippleJS"></div>
                                       </button>
                               </form>

rek.php:
Code
<?php include("db/database.php");
$gebruiker = $_POST['gebr'];
$wachtw = $_POST['ww'];
mysqli_query($connect, "INSERT INTO 'data' ('gebruikersnaam', 'wachtwoord')
VALUES ($gebruiker, $wachtw)");
?>

database.php:
Code
<?php
$connect = mysqli_connect("localhost", "root", "", "proj");
?>

I'm expecting it to add a row with the "gebruikersnaam" and "wachtwoord" into the data table but everytime I click on submit I just get redirected to rek.php and thats it, the data doesn't get added to the db. What am I doing wrong? And is there any way that I can send info to the db without getting redirected to another page (rek.php in this case).
One of the first times doing PHP, so excuse me if this is a nooby question lol
You're using MySQLi without prepared statements, which is extremely neglectful. Also, using the include function when you REQUIRE the file is a bad idea. Use "require" for stuff like that.

rek.php
PHP Code

<?php 
// Files
require("db/database.php") or die("no db");

// Variables
$gebruiker = $_POST['gebr'];
$wachtw = $_POST['ww'];

// Check Data (Just an example, use legitimate means of validating)
if (empty($gebruiker) || empty($wachtw)) {
 
  echo "Invalid data";
 
  die();
}

// Statement
if ($stmt = $connect->prepare("INSERT INTO `data` (`gebruikersnaam`, `wachtwoord`) VALUES (?, ?)")) {
 
  $stmt->bind_param("ss", $gebruiker, $wachtw);
 
  if ($stmt->execute() == true) {
 
    echo "success";
 
    die();
 
  } else {
 
    echo "Errors: " . $stmt->error . " | " . $connect->error;
 
  }
} else {
 
  // Debug Only
 
  echo $stmt->error . " | ". $connect->error;
 
  die();
} 

Other question:
I'm not going to write up a full Ajax request because you can do your own research, but you can submit the form via javascript using Ajax.

Je t'aime
[Image: fpZ5a5g.png]

(Apr 22, 2022 04:39 PM)-Scrub Wrote:  Jokes on you, I'm not smart enough to know what he's talking about.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by IcomedyMar 12, 2019 02:55 AM 7 387 Mar 12, 2019 02:55 AM
Last Post: Squito
[PHP] by deadsh0tNov 9, 2018 10:56 AM 11 431 Nov 9, 2018 10:56 AM
Last Post: Squito
[PHP] by MatataSmithMay 5, 2017 08:47 AM 10 323 May 5, 2017 08:47 AM
Last Post: Eight