我的数据没有插入我的数据库

时间:2018-06-14 07:06:07

标签: php html database forms mysqli

当我尝试将数据插入我的数据库时,它无法正常工作,当我尝试发送数据时也没有显示任何错误。 这是HTML表单

<form method="post" action="check_inschrijven.php" id="inschrijf_form">
    <label>Naam</label><br>
    <input type="text" name="naam" class="inputs_text"><br>
    <label>Cursus</label><br>
    <input type="text" name="cursus" class="inputs_text"><br>
    <label>E-Mail</label><br>
    <input type="text" name="email" class="inputs_text"><br><br>
    <input type="submit" value="Registreren" name="reg_button" class="reg_button">
  </form>

至于PHP代码,它就在这里:

<?php

$link = mysql_connect("localhost", "uname", "password", "mkb");

if(!$link)
{
    die('Kon niet verbinden met de database: ' . mysql_error());
}

$naam = $_POST['naam'];
$cursus = $_POST['cursus'];
$email = $_POST['email'];

$query = "INSERT INTO cursist (naam, cursus, email) VALUES ('$naam', '$cursus', '$email')";
if(!mysql_query($query))
{
    die("Gefaald om gegevens op te slaan");
    header("location: inschrijven.php");
} else { 
  echo "Gegevens succesvol toegevoegd aan de database";
  header("location: index.php");
}

mysql_close();

?>

3 个答案:

答案 0 :(得分:0)

您可以修改mysql_query。您可以提供Db参考并尝试它。尝试:

if (!mysql_query($query, $link)) {
    // logic
}

希望这有帮助。

答案 1 :(得分:0)

试试这段代码

$query = mysql_query("INSERT INTO cursist (naam, cursus, email) VALUES ('$naam', '$cursus', '$email')");
if(!($query))
{
    die("Gefaald om gegevens op te slaan");
    header("location: inschrijven.php");
} else { 
  echo "Gegevens succesvol toegevoegd aan de database";
  header("location: index.php");
}

答案 2 :(得分:0)

找到解决方案,这有效:

<?php
$link = mysqli_connect("localhost", "root", "usbw", "mkb");

if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$naam = mysqli_real_escape_string($link, $_REQUEST['naam']);
$cursus = mysqli_real_escape_string($link, $_REQUEST['cursus']);
$email = mysqli_real_escape_string($link, $_REQUEST['email']);


$sql = "INSERT INTO cursist (naam, cursus, email) VALUES ('$naam', '$cursus', '$email')";
if(mysqli_query($link, $sql)){
    echo "Succesvol ingeschreven";
    header("location: index.php");
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

mysqli_close($link);
?>