-2

I have this select

 <select name="tipi">
          <option value="gp">Gradinita Privata</option>
          <option value="gs">Gradinita de stat</option>
          <option value="cr">Cresa</option>
 </select>

I want to add the information in my database depending on what is selected. I tried

$tip="";
//here is more information
$tip=$_POST["tipi"];

    
   if( $tip === 'gp'){
$sql="INSERT INTO gradiniteprivate (denumire,email,adresa,telefon,pret) VALUES 
('$denumire','$email','$adresa','$telefon','$pret')";
echo $sql;
echo "</br>";}
else if( $tip === 'gs'){
    $sql="INSERT INTO gradinitestat (denumire,email,adresa,telefon,pret) VALUES 
('$denumire','$email','$adresa','$telefon','$pret')";
echo $sql;
echo "</br>";
}
else if( $tip === 'cr'){
      $sql="INSERT INTO crese (denumire,email,adresa,telefon,pret) VALUES 
('$denumire','$email','$adresa','$telefon','$pret')";
echo $sql;
echo "</br>";
}

I have these errors: Warning: Undefined array key "tipi" in C:\xampp\htdocs\Proiect\adaugag-d.php on line 31

Warning: Undefined variable $sql in C:\xampp\htdocs\Proiect\adaugag-d.php on line 49

Fatal error: Uncaught ValueError: mysqli_query(): Argument #2 ($query) cannot be empty in C:\xampp\htdocs\Proiect\adaugag-d.php:49 Stack trace: #0 C:\xampp\htdocs\Proiect\adaugag-d.php(49): mysqli_query(Object(mysqli), '') #1 {main} thrown in C:\xampp\htdocs\Proiect\adaugag-d.php on line 49

  • There is no particular problem with the code you entered, The error messages you posted are from another section, This question needs to be told in more detail – Erfan Bahramali May 08 '21 at 20:18
  • Does the html form and the photos code are in the same file? If yes, you should test that $_POST is initialized with [isset](https://www.php.net/manual/en/function.isset.php) function – DonKnacki May 08 '21 at 21:04
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman May 08 '21 at 21:09

1 Answers1

1

I have these errors: Warning: Undefined array key "tipi" in C:\xampp\htdocs\Proiect\adaugag-d.php on line 31

Your either accessing over GET request and POST is not set or your form is not actually posting the value, <select name="tipi"> outside </form>?

Warning: Undefined variable $sql in C:\xampp\htdocs\Proiect\adaugag-d.php on line 49

It's a cascading error, you are not defining $sql because it's only defined inside an if statement which is not truthy due to previous issue, not POST'ing value, or GET request.

Fatal error: Uncaught ValueError: mysqli_query(): Argument #2 ($query) cannot be empty in C:\xampp\htdocs\Proiect\adaugag-d.php:49 Stack trace: #0 C:\xampp\htdocs\Proiect\adaugag-d.php(49): mysqli_query(Object(mysqli), '') #1 {main} thrown in C:\xampp\htdocs\Proiect\adaugag-d.php on line 49

Again, it's expecting $sql to be set.

You should check for POST request, validate user input and use prepared queries.

<?php
$errors = [];

// its a POST!
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

   // validate form inputs
   if (!isset($_POST["tipi"])) {
     $errors['tipi'] = 'Tipi is a required field';
   } elseif(!in_array($_POST["tipi"], ['gp', 'gs', 'cr'])) {
     $errors['tipi'] = 'Invalid tipi value';
   }

   // @todo: add validation for $denumire, $email, $adresa, $telefon, $pret

   // $errors is empty so must be no errors, continue to do query..
   if (empty($errors)) {

     // determine table, could use an if statement, or not do it as its already been validated as either gp, gs  or cr
     $table = '';
     switch ($_POST["tipi"]) {
      case: "gp": $table = 'gradiniteprivate'; break;
      case: "gs": $table = 'gradinitestat'; break;
      case: "cr": $table = 'crese'; break;
     }
   
     // run prepared query
     $stmt = $mysqli->prepare("
       INSERT INTO $table (
         denumire,
         email,
         adresa,
         telefon,
         pret
       ) VALUES (?,?,?,?,?)");
     $stmt->bind_param("sssss", $denumire, $email, $adresa, $telefon, $pret);
     $stmt->execute();
     
   }
}
?>

// if form is after this
<form>
  ...

  <select name="tipi">
      <option value="gp">Gradinita Privata</option>
      <option value="gs">Gradinita de stat</option>
      <option value="cr">Cresa</option>
  </select>
  <?= !empty($errors['tipi']) ? '<div class="invalid-feedback">'.$errors['tipi'].'</div>' : '' ?>

  ...
</form>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106