INSERT INTO 语法问题

INSERT INTO syntax issue

即使我尝试解决它,我仍然遇到语法错误问题。这是错误消息:

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'rue de vergennes, 06xxxxxxxx, mail@mail.com)' at line 2"

这是我的代码:

1.html

<form method="post" action="2.php">
      <p>
          <label>Nom</label> : <input type="text" name="nom"><br>
          <label>Prénom</label> : <input type="text" name="prenom"><br>
          <label>Adresse</label> : <input type="text" name="adresse"><br>
          <label>Numéro de téléphone</label> : <input type="tel" name="tel" pattern="^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$"><br>
          <label>Adresse e-mail</label> : <input type="email" name="mail"><br>
          <input type="submit" name="submit" value="Poursuivre">
      </p>
  </form>

2.php

<?php
  $servername = "localhost";
  $username = "root";
  $password = "root";
  $dbname = "kitcasino";

$nom = $_POST['nom'];
$prenom = $_POST['prenom'];
$adresse = $_POST['adresse'];
$tel = $_POST['tel'];
$mail = $_POST['mail'];

  $conn = mysqli_connect($servername, $username, $password, $dbname);
  if (!$conn) {
      die("Connection failed: " . mysqli_connect_error());
  }

  $sql = "INSERT INTO donnees (nom, prenom, adresse, tel, email)
  VALUES ($nom, $prenom, $adresse, $tel, $mail)";

  if (mysqli_query($conn, $sql)) {
      echo "Done!";
  } else {
      echo "Error: " . $sql . "<br>" . mysqli_error($conn);
  }

  mysqli_close($conn);
  ?>

您必须将变量放在引号之间

$sql = "INSERT INTO donnees (nom, prenom, adresse, tel, email)
  VALUES ($nom, '$prenom', '$adresse', $tel, '$mail')";

你必须这样做:

 $sql = "INSERT INTO donnees (nom, prenom, adresse, tel, email)
  VALUES ('".$nom."', '".$prenom."', '".$adresse."', '".$tel."', '".$mail."')";

你好来自法国,因为我看到一个讲法语的人:)

试试你是不是下面的代码

      $servername = "localhost";
      $username = "root";
      $password = "root";
      $dbname = "kitcasino";

  $con = mysqli_connect($servername, $username, $password, $dbname);
$nom = mysqli_real_escape_string($con, $_POST['nom']);
$prenom = mysqli_real_escape_string($con, $_POST['prenom']);
$adresse = mysqli_real_escape_string($con, $_POST['adresse']);
$tel = $_POST['tel'];
$mail = $_POST['mail'];
    if (!$conn) {
          die("Connection failed: " . mysqli_connect_error());
      }

      $sql = "INSERT INTO donnees (nom, prenom, adresse, tel, email)
      VALUES ('".$nom."', '".$prenom."', '".$adresse."', '".$tel."', '".$mail."')";

      if (mysqli_query($conn, $sql)) {
          echo "Done!";
      } else {
          echo "Error: " . $sql . "<br>" . mysqli_error($conn);
      }

      mysqli_close($con);

使用 mysqli 准备好的语句,这对您的应用程序来说更安全、更快速。

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "kitcasino";
$nom = $_POST['nom'];
$prenom = $_POST['prenom'];
$adresse = $_POST['adresse'];
$tel = $_POST['tel'];
$mail = $_POST['mail'];
$mysqli = new mysqli($servername, $username, $password, $dbname);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$stmt = $mysqli->prepare("INSERT INTO donnees (`nom`, `prenom`, `adresse`, `tel`, `email`) VALUES (?,?,?,?,?)");
$stmt->bind_param("sssss", $nom, $prenom, $adresse, $tel, $mail);
if (!$stmt->execute()) {
trigger_error('Error executing MySQL query: ' . $stmt->error);
} else {
echo "Done!";
}
$stmt->close();
$mysqli->close();