-2

I'm trying to make a very simple php form that inserts values in a database. First I have created a form :

<html>
  <head>
    <title>insertion de données en PHP :: partie 1</title>
  </head>
<body>
<form name="insertion" action="insertion2.php" method="POST">
  <table border="0" align="center" cellspacing="2" cellpadding="2">
    <tr align="center">
      <td>nom</td>
      <td><input type="text" name="nom"></td>
    </tr>
    <tr align="center">
      <td>prenom</td>
      <td><input type="text" name="prenom"></td>
    </tr>
    <tr align="center">
      <td>adresse</td>
      <td><input type="text" name="adresse"></td>
    </tr>
    <tr align="center">
      <td>code postal</td>
      <td><input type="text" name="codePostal"></td>
    </tr>
    <tr align="center">
      <td>numéro de téléphone</td>
      <td><input type="text" name="telephone"></td>
    </tr>

    <tr align="center">
      <td colspan="2"><input type="submit" value="insérer"></td>
    </tr>
  </table>
</form>
</body>
</html>

Then I created a second php file with the mysqli database connection and a SQL query to insert the values in the database :

<?php
    $mysqli = new mysqli("localhost", "root", "", "info");
    if ($mysqli->connect_errno) {
        echo "Echec lors de la connexion à MySQL : (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }

    $nom     = $_POST["nom"] ;
    $prenom = $_POST["prenom"] ;
    $adresse = $_POST["adresse"] ;
    $cp        = $_POST["codePostal"] ;
    $tel       = $_POST["telephone"] ;

    $sql = "INSERT  INTO personnes (nom, prenom, adresse, cp, telephone)
                VALUES ( '$nom', '$prenom', '$adresse', '$cp', '$tel') " ;

    $requete = mysql_query($sql, $base) or die( mysql_error() ) ;

    if($requete)
    {
       echo("L'insertion a été correctement effectuée") ;
    }
    else
    {
       echo("L'insertion à échouée") ;
    }
?>

But now I have errors :

Warning: mysql_query() expects parameter 2 to be resource, null given in C:\wamp\www\Inform\insertion2.php on line 20 Notice: Undefined variable: base in C:\wamp\www\Inform\insertion2.php on line 20

and I don't know what to do. Can you help me?

Tchami
  • 4,647
  • 1
  • 32
  • 45
Maxime
  • 131
  • 2
  • 16

2 Answers2

2

You are mixing it up with mysql. Try with -

$requete = mysqli_query($mysqli, $sql) or die( mysqli_error());

Instead of -

$requete = mysql_query($sql, $base) or die( mysql_error() ) ;
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
0

As mentioned in the comments you need to define your $base variable. You also call both mysqli and mysql functions.

Since you write it is a Mysqli database, you should use the mysqli functions at all times in your code.

But you really also should sanitize your parameters. This code is very much open to SQL Injections

You migth want to look into using mysqli->Prepare and execute with parameterized queries.

Community
  • 1
  • 1
Kaspar Kjeldsen
  • 936
  • 1
  • 13
  • 30