简体   繁体   中英

Sql query suddenly doesnt work through php

I have a HTML-form and want the output of it inserted in my (local) mysql. However, after it succeeded once, it suddenly doesnt work anymore when I submit the form and I havent changed the code. Now I just see a blank page with the Post-request in Network tab. How does it come nothing is inserted anymore?

script.php:

<?php
$postdata = file_get_contents("php://input");
$link = mysqli_connect("127.0.0.1", "root", "", "izandb");

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

$stmt = $link->prepare('INSERT INTO resultaten (Klas, Naam, Percentages, 
Antwoorden)  VALUES (?, ?, ?, ?)');
$stmt->bind_param('ssss', $klas, $naam, $klas, $klas);
$stmt->execute();
echo "Insertion succeed";
} else {
echo "Insertion not succeed";
}
?>

html form:

<form id="myForm" method="post" action="script.php">
Volledige 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>


<div class="row">
<div class="col-4">1. Ik ben spontaan. </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>

<br> <input type="submit" name="submit" value="submit" onsubmit="bereken()"></input>

Please make sure to close the in your code.

    if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
   }
   echo "<pre>";
   print_r($_POST);

insert this code and find the error

Okay, so after talking to you and reproducing the scenario, I was able to modify the script a bit and come up with something which worked fine for me.

<?php
$postdata = file_get_contents("php://input");

mysqli_report(MYSQLI_REPORT_ALL);

try
{
    $link = new mysqli( 'localhost', 'test_account', 'test_password', 'database');
}
catch(Exception $e)
{
    echo 'Error: ' . $e->getMessage();
}


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

    $query = 'INSERT INTO resultaten (Klas, Naam, Percentages, Antwoorden)  VALUES (?, ?, ?, ?)';

    if ($stmt = $link->prepare( $query ))
    {
        $stmt->bind_param('ssss', $klas, $naam, $klas, $klas);
        $stmt->execute();
        $stmt->close();
        echo "Insertion succeed";
    } else {
        echo "Insertion not succeed";
    }
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM