简体   繁体   中英

How I add the information in my database depending on what is selected.?

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM