Reputation: 3
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.
Upvotes: 0
Views: 52
Reputation: 1741
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>
Upvotes: 3