INSERT INTO语法问题

时间:2015-02-12 10:10:41

标签: php mysql sql-insert

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

  

"您的SQL语法出错了;检查与您的MySQL服务器版本相对应的手册,以便在#rue de vergennes,06xxxxxxxx,mail @mail.com附近使用正确的语法。'在第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);
  ?>

4 个答案:

答案 0 :(得分:2)

你必须把变量放在引用之间作为

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

答案 1 :(得分:1)

你必须这样做:

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

Bonjour de la france,puisque je vois un francophone:)

答案 2 :(得分:0)

试试你是下面的代码

      $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);

答案 3 :(得分:0)

使用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();