Notice: Undefined index: ArLebel in E:\xampp\htdocs\proj\example\insert.php on line 15.
Here is my html code for Dropdown.
<tr>
<td>Level</td>
<td><select name="ArLevel">
<option value="beginner">Beginner</option>
<option value="intermeted">Intermeted</option>
<option value="advance">Advance</option>
</select>
</td>
</tr>
<tr>
<td>Subject</td>
<td><select name="ArSubject">
<option value="politic">Politic</option>
<option value="islamic law">Islamic Law</option>
</select>
</td>
</tr>
Here is my PHP Code:
<?php
if (isset($_POST['submit'])) {
$con = mysql_connect("localhost","root","");
if(!$con){
die("Can not connect:" . mysql_error());
}
//Database Connection
mysql_select_db("example",$con);
$sql = "INSERT INTO datainsert(ArticleDate,ArticleAuthor,ArticleSource,ArLevel,ArSubject)
VALUES ('$_POST[ArticleDate]','$_POST[ArticleAuthor]','$_POST[ArticleSource]','$_POST[ArLebel]','$_POST[ArSubject]')";
mysql_query($sql,$con);
mysql_close($con);
}
?>
the array keys are given as string, and strings should be enclosed in quotes, even when you are already in a string (or heredocs/nowdocs, but that's not relevant here):
It it $variableName["keyName"]
, not $variableName[keyName]
, unless keyName is a defined constant.
change
'$_POST[ArLebel]','$_POST[ArSubject]'
into
'{$_POST["ArLebel"]}','{$_POST["ArSubject"]}'
The {} allow for using indexed variables inside other strings.
Failing to do this correctly might yield you the notice that a string is assumed in stead of a constant.
But seen as you have a different notice message (undefined index) , it is also telling you the entire index is not found (string nor constant)... So you need to check why $_POST["ArSubject"]
is not existing.
Have you seen Tun Zarni Kyaw's comment about probably misspelling ArLevel
as ArLebel
somewhere?
warning:
Your code is vulnerable to SQL injection! Please sanitize your variables before putting them inside your query!
Use isset()
to check the POST variables are set. Also its a good practice to escape the POST variables before inserting to database.
<?php
if (isset($_POST['submit'])) {
$con = mysql_connect("localhost","root","");
if(!$con){
die("Can not connect:" . mysql_error());
}
//Database Connection
mysql_select_db("example",$con);
$ArticleDate = isset($_POST['ArticleDate']) ? $_POST['ArticleDate'] : '';
$ArticleAuthor = isset($_POST['ArticleAuthor']) ? $_POST['ArticleAuthor'] : '';
$ArticleSource = isset($_POST['ArticleSource']) ? $_POST['ArticleSource'] : '';
$ArLebel = isset($_POST['ArLebel']) ? $_POST['ArLebel'] : '';
$ArSubject = isset($_POST['ArSubject']) ? $_POST['ArSubject'] : '';
$sql = "INSERT INTO datainsert(ArticleDate,ArticleAuthor,ArticleSource,ArLevel,ArSubject)
VALUES ('$ArticleDate','$ArticleAuthor','$ArticleSource','$ArLebel','$ArSubject')";
mysql_query($sql,$con);
mysql_close($con);
}
?>
There is only spelling mismatch mistack in your code:--
You are using "ArLevel"
in the Select option ehere you are selecting a level
And you are using $_POST["ArLebel"]
in the php code at place of $_POST["ArLevel"]
There is the only mistack of b & v