简体   繁体   中英

Keep getting Notice: Undefined index: whatever I do

I am trying to fill a table in my database from a drop down menu which I populated from another table from my database. The problem is that whenever I submit my query, it gives me the same error "Notice: Undefined index:" and won't fill the table. I am new to coding, so please be gentle. This is the part for populating the drop down menu

<?php
@mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("motocikli") or die(mysql_error());

$query = "SELECT kategorija_ime FROM kategorija";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>

<select name="kateg">
<?php 
while ($row = mysql_fetch_array($result))
{
    echo "<option value='".$row['kategorija_ime']."'>'".$row['kategorija_ime']."'</option>";
}
?>        
</select>

 <form action="insert.php" method="post">
 <input type="submit">
</form>

And this is the insert.php

<?php

$dsn = 'mysql:dbname=motocikli;host=127.0.0.1';
$user = 'root';
$password = '';
$pdo = new \PDO($dsn, $user, $password);



function unesiPoruku($kateg)
{
    global $pdo;
    $upit = $pdo->prepare("INSERT INTO test (kateg) VALUES (:kateg)");
    $upit->bindParam('kateg',$kateg);

    $upit->execute();
}

$kateg = $_REQUEST['kateg'];
unesiPoruku($kateg);

?> 

The error is showing for $kateg = $_REQUEST['kateg'];, the 'kateg' tag.

Your select box needs to be inside the form so that the value is posted properly to the server

ie.

<form action="insert.php" method="post">
      <select name="kateg">
      <?php 
         while ($row = mysql_fetch_array($result))
         {
             echo "<option value='".$row['kategorija_ime']."'>'".$row['kategorija_ime']."'</option>";
         }
      ?>        
      </select>

      <input type="submit">
</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