0

So the PHP script is actually having a connection with the server, but somehow the database at localhost doesn't fill. What did I possibly do wrong?

script.php

<?php
$link = mysqli_connect("127.0.0.1", "root", "", "testdb");

if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is 
great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

$klas = isset($_POST['klasDown']);
$naam = isset($_POST['naamTxt']);

$stmt = $link->prepare("INSERT INTO resultaten (Klas, Naam) VALUES (?, ?");
if (!$stmt) {
echo "false";
} else {
$stmt->bind_param("ss", $klas, $naam);
$stmt->execute();
}


mysqli_close($link);
?>

HTML:

<form id="myForm" method="post" action="script.php">
Naam: <input name="naamTxt" type="text" maxlength="512" 
id="naamTxt" class="searchField"/> <br>

Klas: <select name="klasDown">
  <option value="H4A" selected="selected">H4A</option>
  <option value="H4B" >H4B</option>
  <option value="H4C">H4C</option>
  <option value="H4C">H4D</option>
  <option value="H4C">V4A</option>
  <option value="H4C">V4B</option>
  <option value="H4C">V4C</option>
  <option value="H4C">V4D</option>  
</select> 


<div class="row">
<div class="col-4">1. Kies 1/5 </div>
<div class="col text-center"><input type="radio" name="v1" id="v1_1" 
value="Helemaal mee oneens"></div>
<div class="col text-center"><input type="radio" name="v1" id="v1_2" 
value="Deels oneens"></div> 
<div class="col text-center"><input type="radio" name="v1" id="v1_3" 
value="Neutraal"></div> 
<div class="col text-center"><input type="radio" name="v1" id="v1_4" 
value="Deels mee eens"></div> 
<div class="col text-center"><input type="radio" name="v1" id="v1_5" 
value="Helemaal mee eens"></div>
</div>

<input type= "submit" style="text-align: center" 
onclick="bereken()">
</form>

^ Above is the Html Form

Bereken() function:

function bereken() {

    var e = 0;
    var c = 0;


    if(document.getElementById('v1_1').checked)  {
      c = c - 0
    }else if(document.getElementById('v1_2').checked)  {
      c += 1;
    }else if(document.getElementById('v1_3').checked)  {
      c += 2;
    }else if(document.getElementById('v1_4').checked)  {
      c += 3;
    }else if(document.getElementById('v1_5').checked){
      c += 4;
    }

    var klas = li.options[li.selectedIndex].value;
    var naam = document.getElementById('searchTxt').value;

    alert(klas + ", " + naam);
}

^ This is the Javascript bereken() function . Apparently too much code, so have to add some text.

12
  • That has nothing to do with phpmyadmin. Commented Aug 22, 2017 at 21:19
  • @SLaks could you kindly explain then? Commented Aug 22, 2017 at 21:20
  • 1
    PHPMyAdmin is not a database. It is a web interface for your MySQL database. Commented Aug 22, 2017 at 21:37
  • 1
    Note: The object-oriented interface to mysqli is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete mysql_query interface. Before you get too invested in the procedural style it’s worth switching over. Example: $db = new mysqli(…) and $db->prepare("…”) The procedural interface is an artifact from the PHP 4 era when mysqli API was introduced and should not be used in new code. You're mixing and matching styles here for no apparent reason. Try and use the newer form exclusively. Commented Aug 22, 2017 at 22:23
  • 1
    Consistency is important. In programming every character can matter, and all too often a simple mistake caused by confusion or a typo can take hours or days to eliminate. Try to keep things as organized as practical, as consistent as possible, and you won't make silly mistakes like that. Commented Aug 23, 2017 at 3:51

3 Answers 3

2

Replace

$klas = isset($_POST['klasDown']);
$naam = isset($_POST['naamTxt']);

with

$klas = $_POST['klasDown'];
$naam = $_POST['naamTxt'];

isset will return a boolen, not a string that you are expecting.

Sign up to request clarification or add additional context in comments.

16 Comments

Thanks for you reply, but now I get the messages: Notice: Undefined index: klasDown
and Notice: Undefined index: naamTxt
Where those data are supposed to come from?
From a html page with a form, I will update the question
Everything seems to be ok. Are you using a <button type="submit"> or submitting with Ajax?
|
0

On script.php do the following

If(isset($_POST['submit''])){

$klas = $_POST['klasDown'];

$naam = $_POST['naamTxt'];

$stmt = $link->prepare("INSERT INTO resultaten (Klas, Naam) VALUES (?, ?");

if (!$stmt) {

echo "false";

} else {

$stmt->bind_param('ss', $klas, $naam);

$stmt->execute();

}

}

Replace the button With the following

input type= "submit"

name="submit"

value=" Verstuur"

style="text-align: center"

onsubmit="bereken()"

10 Comments

Just did that, but still no records in my DB :(. The error messages like 'Notice: Undefined index: naamTxt' are gone though
just try to rename your input name and use another name
if(isset($_POST['submit'])){ <- should this get the same name then?
Follow The following example and apply 100% works <form id="myForm" method="post" action="index.php"> Naam: <input name="naamTxt" type="text" maxlength="512" id="naamTxt" class="searchField"/> <br> <input type="submit" name="submit"> </form> <?php If(isset($_POST['submit'])){ echo $naam = $_POST['naamTxt']; } ?>
just copy and test
|
0

I hope the following might prove of use in getting the database updated. The javascript function itself now submits the form ( based upon original idea ) and by doing so allows you to use an alert statement with the values to be sent. I was initially getting an error about undefined li in the javascript function - hence using querySelector to identify the form element.

In this instance the form submits to the same page but the form action could easily be set as you had previously, I only did this to write the code now. The sql uses prepared statements - not tested but should(?!) be OK.

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' ){

        try{

            $dbuser='root';
            $dbpwd='';
            $dbhost='localhost';
            $dbname='testdb';

            $link=new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );


            if( !$link ) {
                $message=sprintf( "Error: Unable to connect to MySQL.\nDebugging errno: %s\nDebugging error: %s" );
                throw new Exception( $message, $link->errno, $link->error );
            }

            $klas = !empty( $_POST['klasDown'] ) ? filter_input( INPUT_POST, 'klasDown', FILTER_SANITIZE_STRING ) : false;
            $naam = !empty( $_POST['naamTxt'] ) ? filter_input( INPUT_POST, 'naamTxt', FILTER_SANITIZE_STRING ) : false;

            if( !$klas ) throw new Exception('klas cannot be empty');
            if( !$naam ) throw new Exception('naam cannot be empty');

            $stmt = $link->prepare( "insert into `resultaten` (`klas`, `naam`) values ( ?, ? )" );
            if( $stmt ){

                $stmt->bind_param( 'ss', $klas, $naam );
                $result=$stmt->execute();
                if( !$result )throw new Exception('SQL insert failed');

            } else {
                throw new Exception('Failed to prepare SQL query');
            }


            $stmt->free_result();
            $stmt->close();
            $link->close();

        }catch( Exception $e ){
            echo $e->getMessage();
        }

    }
?>
<!doctype html>
<html>
    <head>
        <title>Bereken</title>
        <script>

            function bereken() {
                var e = 0;
                var c = 0;

                if(document.getElementById('v1_1').checked)  {
                  c = c - 0
                }else if(document.getElementById('v1_2').checked)  {
                  c += 1;
                }else if(document.getElementById('v1_3').checked)  {
                  c += 2;
                }else if(document.getElementById('v1_4').checked)  {
                  c += 3;
                }else if(document.getElementById('v1_5').checked){
                  c += 4;
                }
                var li=document.querySelector('select[name="klasDown"]')
                var klas = li.options[li.selectedIndex].value;
                var naam = document.getElementById('naamTxt').value;

                alert( klas + ", " + naam );
                document.forms['myForm'].submit();
            }

        </script>
    </head>
    <body>
        <form id="myForm" method="post">
            Naam: <input name="naamTxt" type="text" maxlength="512" id="naamTxt" class="searchField"/> <br>

            Klas: <select name="klasDown">
              <option value="H4A" selected="selected">H4A</option>
              <option value="H4B" >H4B</option>
              <option value="H4C">H4C</option>
              <option value="H4C">H4D</option>
              <option value="H4C">V4A</option>
              <option value="H4C">V4B</option>
              <option value="H4C">V4C</option>
              <option value="H4C">V4D</option> 
            </select> 


            <div class="row">
                <div class="col-4">1. Kies 1/5 </div>
                <div class="col text-center">
                    <input type="radio" name="v1" id="v1_1" value="Helemaal mee oneens">
                </div>
                <div class="col text-center">
                    <input type="radio" name="v1" id="v1_2" value="Deels oneens">
                </div> 
                <div class="col text-center">
                    <input type="radio" name="v1" id="v1_3" value="Neutraal">
                </div> 
                <div class="col text-center">
                    <input type="radio" name="v1" id="v1_4" value="Deels mee eens">
                </div> 
                <div class="col text-center">
                    <input type="radio" name="v1" id="v1_5" value="Helemaal mee eens">
                </div>
            </div>
            <button type="button" style="text-align: center" onclick="bereken()">Verstuur</button>
        </form>
    </body>
</html>

2 Comments

How is the php file now engaged?
You could copy the php to a new file script.php and set the form action as you had originally. I only put it all in one document like this for convenience when writing the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.