I am creating a multiple choice quiz and I don't know how to insert multiple arrays to database. I can insert the first 2 arrays using array_combine but cannot on the 3rd, 4th, 5th, and 6th array. I can insert questions and answers but not the options A, B, C, and D.
I'm trying to do something like
foreach(array_combine($_POST['inQuestion'], $_POST['inAnswer'], $_POST['inA'], $_POST['inB'], $_POST['inC'], $_POST['inD']) as $question => $answer => $A => $B => $C => $D) {
Please help. Here is my code.
<?php
if(isset($_POST['btnCreate'])) {
$inQuestion = array($_POST['inQuestion']);
$inAnswer = array($_POST['inAnswer']);
$inA = array($_POST['inA']);
$inB = array($_POST['inB']);
$inC = array($_POST['inC']);
$inD = array($_POST['inD']);
$inLesson = $_POST['inLesson'];
$inQuizNo = $_POST['inQuizNo'];
$sql = "SELECT * FROM lessons WHERE title='$inLesson'";
$query = mysql_query ($sql);
$row = mysql_fetch_assoc($query);
$lessonID = $row['lessonID'];
foreach(array_combine($_POST['inQuestion'], $_POST['inAnswer']) as $question => $answer) {
$sql = "INSERT INTO `test` (question, answer, A, B, C, D, lessonID, quizNo) VALUES ('$question', '$answer', '$A', '$B', '$C', '$D', '$lessonID', $inQuizNo)";
$query = mysql_query( $sql );
}
}
?>
<div class="panel panel-info">
<div class="panel-heading">
<h4>Create Assessment - Multiple Choice</h4>
</div>
<div class="panel-body">
<form method="post">
<br>
<div>
<div class="col-md-4">
<strong>Lesson</strong><br>
<select class="form-control" name="inLesson">
<option></option>
<option disabled></option>
<?php
$sql2 = "SELECT * FROM lessons WHERE courseID='$_GET[courseID]'";
$query2 = mysql_query ($sql2);
while ($row2 = mysql_fetch_assoc($query2)) {
?>
<option><?php echo $row2['title']; ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-4">
<strong>Quiz No.</strong><br>
<input required type="number" class="form-control" name="inQuizNo" min="1" max="100">
</div>
<div class="col-md-4">
<div class="pull-right">
<br>
<input type="button" value="Add" id="addButton" class="btn btn-info">
<input type="button" value="Remove" id="removeButton" class="btn btn-warning">
</div>
</div>
</div>
<br><br><br><br><br>
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1" class="form-group">
<h2><span class="label label-primary">No. 1</span></h2>
<br><br>
<div class="col-md-10">
<label>Question</label><input type='text' id='textbox1' name='inQuestion[]' class="form-control">
</div>
<div class="col-md-2">
<label>Answer</label><input type='text' id='textbox1' name='inAnswer[]' class="form-control">
<br>
</div>
<div class="col-md-3">
<label>A</label><input type='text' id='textbox1' name='inA[]' class="form-control">
</div>
<div class="col-md-3">
<label>B</label><input type='text' id='textbox1' name='inB[]' class="form-control">
</div>
<div class="col-md-3">
<label>C</label><input type='text' id='textbox1' name='inC[]' class="form-control">
</div>
<div class="col-md-3">
<label>D</label><input type='text' id='textbox1' name='inD[]' class="form-control">
<br><br>
</div>
</div>
</div>
<div class="col-md-12">
<button type="submit" name="btnCreate" class="btn btn-success pull-right">Create Quiz
</div>
</form>
</div>
From what I understood, you are trying to insert multible rows INTO your database.
First, when you use the [] in HTML forms you don't have to declare it as an array in php
Second, each question will have a question name, answer, A,B,C,D
When you trying to create the HTML form you could use this format to keep track the question_id or number.
<label>Question</label><input type='text' id='textbox1' name='inQuestion[1]' class="form-control">
Then you will be able to retrieve this item as $_POST['inQuestion'][1]
For Example, if you need 10 questions in your test page you can do it like this:
<?php for($i = 0; $i <= 10; $i++) : ?>
<form method="post">
<div class="col-md-10">
<label>Question</label><input type='text' id='textbox1' name='inQuestion[<?=$i;?>]' class="form-control">
</div>
<div class="col-md-2">
<label>Answer</label><input type='text' id='textbox1' name='inAnswer[<?=$i;?>]' class="form-control">
<br>
</div>
<div class="col-md-3">
<label>A</label><input type='text' id='textbox1' name='inA[<?=$i;?>]' class="form-control">
</div>
<div class="col-md-3">
<label>B</label><input type='text' id='textbox1' name='inB[<?=$i;?>]' class="form-control">
</div>
<div class="col-md-3">
<label>C</label><input type='text' id='textbox1' name='inC[<?=$i;?>]' class="form-control">
</div>
<div class="col-md-3">
<label>D</label><input type='text' id='textbox1' name='inD[<?=$i;?>]' class="form-control">
<br><br>
</div>
<input type="submit" value="go">
</form>
<?php endfor; ?>
Also, why do you put the content of the Answers A,B,C,D to an array while you have them is columns in your database. Also use these variables in your query too.
$inA = $_POST['inA'];
$inB = $_POST['inB'];
$inC = $_POST['inC'];
$inD = $_POST['inD'];
Finally Let's try to insert your values now.
if($_POST['btnCreate']){
// The form has been posted!
$inLesson = $_POST['inLesson'];
$inQuizNo = $_POST['inQuizNo'];
$questionsCount = count($_POST['inQuestion']);
$items = array();
// Since we should have the same size of arrays.
// Also the empty validation has been handled before this step!
for($i = 0; $i < $questionsCount; $i++){
$temp = array();
$temp['inQuestion'] = $_POST['inQuestion'][$i];
$temp['inAnswer'] = $_POST['inAnswer'][$i];
$temp['inA'] = $_POST['inA'][$i];
$temp['inB'] = $_POST['inB'][$i];
$temp['inC'] = $_POST['inC'][$i];
$temp['inD'] = $_POST['inD'][$i];
$items[] = $temp;
}
// Now items should have an array of all questions.
foreach($items as $item){
$SQL = 'INSERT INTO `test` (question, answer, A, B, C, D, lessonID, quizNo) VALUES (
"'.$item['inQuestion'].'",
"'.$item['inAnswer'].'",
"'.$item['inA'].'",
"'.$item['inB'].'",
"'.$item['inC'].'",
"'.$item['inD'].'",
"'.$inLesson.'",
"'.$inQuizNo.'")';
$query = mysql_query( $SQL );
} // End Foreach
}
Try This
<?php
if(isset($_POST['btnCreate'])) {
$inQuestion = array($_POST['inQuestion']);
$inAnswer = array($_POST['inAnswer']);
$inA = $_POST['inA'] ? $_POST['inA'] : null;
$inB = $_POST['inB'] ? $_POST['inB'] : null;
$inC = $_POST['inC'] ? $_POST['inC'] : null;
$inD = $_POST['inD'] ? $_POST['inD'] : null;
$inLesson = $_POST['inLesson'];
$inQuizNo = $_POST['inQuizNo'];
$sql = "SELECT * FROM lessons WHERE title='$inLesson'";
$query = mysql_query ($sql);
$row = mysql_fetch_assoc($query);
$lessonID = $row['lessonID'];
$sql = "INSERT INTO `test` (question, answer, A, B, C, D, lessonID, quizNo) VALUES ('$question', '$answer', '$inA', '$inB', '$inC', '$inD', '$lessonID', $inQuizNo)";
$query = mysql_query( $sql );
}
?>
<div class="panel panel-info">
<div class="panel-heading">
<h4>Create Assessment - Multiple Choice</h4>
</div>
<div class="panel-body">
<form method="post">
<br>
<div>
<div class="col-md-4">
<strong>Lesson</strong><br>
<select class="form-control" name="inLesson">
<option></option>
<option disabled></option>
<?php
$sql2 = "SELECT * FROM lessons WHERE courseID='$_GET[courseID]'";
$query2 = mysql_query ($sql2);
while ($row2 = mysql_fetch_assoc($query2)) {
?>
<option><?php echo $row2['title']; ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-4">
<strong>Quiz No.</strong><br>
<input required type="number" class="form-control" name="inQuizNo" min="1" max="100">
</div>
<div class="col-md-4">
<div class="pull-right">
<br>
<input type="button" value="Add" id="addButton" class="btn btn-info">
<input type="button" value="Remove" id="removeButton" class="btn btn-warning">
</div>
</div>
</div>
<br><br><br><br><br>
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1" class="form-group">
<h2><span class="label label-primary">No. 1</span></h2>
<br><br>
<div class="col-md-10">
<label>Question</label><input type='text' id='textbox1' name='inQuestion[]' class="form-control">
</div>
<div class="col-md-2">
<label>Answer</label><input type='text' id='textbox1' name='inAnswer[]' class="form-control">
<br>
</div>
<div class="col-md-3">
<label>A</label><input type='text' id='textbox1' name='inA' class="form-control">
</div>
<div class="col-md-3">
<label>B</label><input type='text' id='textbox1' name='inB' class="form-control">
</div>
<div class="col-md-3">
<label>C</label><input type='text' id='textbox1' name='inC' class="form-control">
</div>
<div class="col-md-3">
<label>D</label><input type='text' id='textbox1' name='inD' class="form-control">
<br><br>
</div>
</div>
</div>
<div class="col-md-12">
<button type="submit" name="btnCreate" class="btn btn-success pull-right">Create Quiz
</div>
</form>
</div>
rather than posting a detailed solution, play with how to insert multiple rows to get some ideas
Something like:
$_POST['inQuestion'] = array(
'q1','q2','q3' // from input array name 'inQuestion[]'
);
$_POST['inAnswer'] = array(
'a1','a2','a3' // from input array name 'inAnswer[]'
);
$_POST['other'] = 'sunshine';
$_POST['yet_another'] = 'nice weather';
foreach($_POST['inQuestion'] as $k => $v){
$inserts[] = "(".$v.",".$_POST['inAnswer'][$k].",".$_POST['other'].",".$_POST['yet_another'].")";
}
echo "INSERT INTO table col_1, col_2, col_3, col_4 VALUES ".implode(',',$inserts)."";
// INSERT INTO table col_1, col_2, col_3, col_4 VALUES (q1,a1,sunshine,nice weather),(q2,a2,sunshine,nice weather),(q3,a3,sunshine,nice weather)
Related
I have a dynamic form which allows to add multiple textboxes. While saving the data in the database, it also saves an additional empty extra row. Could you help to find the issue?
HTML:
<div class="col-xs-12 col-sm-6">
<div class="form-group">
<input name="skill[]" type="text" class="form-control" placeholder="Skill name, e.g. HTML">
</div>
</div>
<div class="col-xs-12 col-sm-6">
<div class="form-group">
<div class="input-group">
<input name="percent[]" type="text" class="form-control" placeholder="Skill proficiency, e.g. 90">
<span class="input-group-addon">%</span>
</div>
</div>
</div>
<div class="col-xs-12 duplicateable-content">
<div class="item-block">
<div class="item-form">
<button class="btn btn-danger btn-float btn-remove"><i class="ti-close"></i></button>
<div class="row">
<div class="col-xs-12 col-sm-6">
<div class="form-group">
<input name="skill[]" type="text" class="form-control" placeholder="Skill name, e.g. HTML">
</div>
</div>
<div class="col-xs-12 col-sm-6">
<div class="form-group">
<div class="input-group">
<input name="percent[]" type="text" class="form-control" placeholder="Skill proficiency, e.g. 90">
<span class="input-group-addon">%</span>
</div>
</div>
</div>
</div>
</div>
</div>
Here is my php to save the data input:
for ($i = 0; $i < count($_POST["skill"]); $i++) {
$skill = $_POST["skill"][$i];
$percent = $_POST["percent"][$i];
$sql = "insert into tb_skill (skills,percent,user_id) values
('$skill', '$percent', '$_SESSION[id]')";
mysqli_query($con, $sql);
}
This might help to you.
$skill = $_POST['skill];
$percent = $_POST['percent'];
for ($i = 0; $i < = count($skill); $i++) {
$sql = "insert into tb_skill (skills,percent,user_id) values
('$skill[$i]', '$percent[$i]', '$_SESSION[id]')";
mysqli_query($con, $sql);
}
Add these lines inside of your for loop, before you submit the variables to database.
$skill = trim($skill);
$percent = trim($percent);
This question already has answers here:
Why can't I run two mysqli queries? The second one fails [duplicate]
(2 answers)
Closed 6 years ago.
I am trying to create a php/html form which will insert results into a dog show database. The problem no matter what I do I get this error:
QUERY FAILED .You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO.
Here is the code for the page any help appreciated.
<?php
if(isset($_POST['create_show'])) {
//Insert Judges
$show_title = escape($_POST['show_title']);
$show_user = escape($_POST['show_user']);
$show_category_id = escape($_POST['show_category_id']);
$show_status = escape($_POST['show_status']);
// $show_image = escape($_FILES['show_image']['name']);
//$show_image_temp = escape($_FILES['image']['tmp_name']);
$show_tags = escape($_POST['show_tags']);
$show_content = escape($_POST['show_content']);
//$show_date = escape(date('d-m-y'));
//INSERT Judges
$judge_affix = escape($_POST['judge_affix']);
$judge_name = escape($_POST['judge_name']);
$judge_show = escape($_POST['show_idj']);
//Insert Dogs
$dog_name = escape($_POST['dog_name']);
$resultIDD = escape($_POST['resultIDD']);
//Insert Into Results
$class_name = escape($_POST['class_name']);
$placement = escape($_POST['placement']);
$award = escape($_POST['award']);
//move_uploaded_file($show_image_temp, "../images/$show_image" );
//Insert Shows
$query = "INSERT INTO shows (show_category_id, show_title, show_user, show_content, show_tags, show_status) VALUES ('$show_category_id','$show_title','$show_user','$show_content','$show_tags','$show_status');";
$query .= "INSERT INTO judges (judge_affix, judge_name) VALUES ('$judge_affix','$judge_name');";
$query .= "INSERT INTO dogs (dog_name, resultIDD) VALUES ('$dog_name','$resultIDD');";
$query .= "INSERT INTO result(class_name, placement,) VALUES ('$class_name','$placement')";
$create_show_query = mysqli_query($connection, $query);
confirmQuery($create_show_query);
$the_show_id = mysqli_insert_id($connection);
echo "<p class='bg-success'>Show Created. <a href='../show.php?s_id={$the_show_id}'>View Post </a> or <a href='shows.php'>Edit More Shows</a></p>";
}
?>
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="show_title">Show Title</label>
<input type="text" class="form-control" name="show_title">
</div>
<div class="form-group">
<label for="category">Category</label>
<select name="show_category" id="">
<?php
$query = "SELECT * FROM categories";
$select_categories = mysqli_query($connection,$query);
confirmQuery($select_categories);
while($row = mysqli_fetch_assoc($select_categories )) {
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
echo "<option value='$cat_id'>{$cat_title}</option>";
}
?>
</select>
</div>
<div class="form-group">
<label for="users">Users</label>
<select name="post_user" id="">
<?php
$users_query = "SELECT * FROM users";
$select_users = mysqli_query($connection,$users_query);
confirmQuery($select_users);
while($row = mysqli_fetch_assoc($select_users)) {
$user_id = $row['user_id'];
$username = $row['username'];
echo "<option value='{$username}'>{$username}</option>";
}
?>
</select>
</div>
<!-- <div class="form-group">
<label for="title">Post Author</label>
<input type="text" class="form-control" name="author">
</div> -->
<div class="form-group">
<select name="show_status" id="">
<option value="draft">Show Status</option>
<option value="published">Published</option>
<option value="draft">Draft</option>
</select>
</div>
<div class="form-group">
<label for="show_tags">Show Tags</label>
<input type="text" class="form-control" name="show_tags">
</div>
<div class="form-group">
<label for="judge_name">Show Tags</label>
<input type="text" class="form-control" name="judge_name">
</div>
<div class="form-group">
<label for="judge_affix">Show Tags</label>
<input type="text" class="form-control" name="judge_affix">
</div>
<div class="form-group">
<label for="show_content">Show Content</label>
<textarea class="form-control " name="show_content" id="" cols="30" rows="5">
</textarea>
</div>
<div class="form-group">
<p>Minor Puppy Dog</p>
</div>
<div class="form-group">
<label for="dog_name">1st Dog Name</label>
<input type="text" class="form-control" name="dog_name">
</div>
<div class="form-group">
<input type="hidden" class="form-control" name="placement" value="1">
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="create_show" value="Publish Show">
</div>
</form>
The mysqli_query only executes one single query.
For executing multiple queries at once, you can use mysqli_multi_query.
Simply replace your mysqli_query with the mysqli_multi_query like so:
$create_show_query = mysqli_multi_query($connection, $query);
This question already has answers here:
Why can't I run two mysqli queries? The second one fails [duplicate]
(2 answers)
Closed 6 years ago.
I am trying to create a php/html form which will insert results into a dog show database. The problem no matter what I do I get this error:
QUERY FAILED .You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO.
Here is the code for the page any help appreciated.
<?php
if(isset($_POST['create_show'])) {
//Insert Judges
$show_title = escape($_POST['show_title']);
$show_user = escape($_POST['show_user']);
$show_category_id = escape($_POST['show_category_id']);
$show_status = escape($_POST['show_status']);
// $show_image = escape($_FILES['show_image']['name']);
//$show_image_temp = escape($_FILES['image']['tmp_name']);
$show_tags = escape($_POST['show_tags']);
$show_content = escape($_POST['show_content']);
//$show_date = escape(date('d-m-y'));
//INSERT Judges
$judge_affix = escape($_POST['judge_affix']);
$judge_name = escape($_POST['judge_name']);
$judge_show = escape($_POST['show_idj']);
//Insert Dogs
$dog_name = escape($_POST['dog_name']);
$resultIDD = escape($_POST['resultIDD']);
//Insert Into Results
$class_name = escape($_POST['class_name']);
$placement = escape($_POST['placement']);
$award = escape($_POST['award']);
//move_uploaded_file($show_image_temp, "../images/$show_image" );
//Insert Shows
$query = "INSERT INTO shows (show_category_id, show_title, show_user, show_content, show_tags, show_status) VALUES ('$show_category_id','$show_title','$show_user','$show_content','$show_tags','$show_status');";
$query .= "INSERT INTO judges (judge_affix, judge_name) VALUES ('$judge_affix','$judge_name');";
$query .= "INSERT INTO dogs (dog_name, resultIDD) VALUES ('$dog_name','$resultIDD');";
$query .= "INSERT INTO result(class_name, placement,) VALUES ('$class_name','$placement')";
$create_show_query = mysqli_query($connection, $query);
confirmQuery($create_show_query);
$the_show_id = mysqli_insert_id($connection);
echo "<p class='bg-success'>Show Created. <a href='../show.php?s_id={$the_show_id}'>View Post </a> or <a href='shows.php'>Edit More Shows</a></p>";
}
?>
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="show_title">Show Title</label>
<input type="text" class="form-control" name="show_title">
</div>
<div class="form-group">
<label for="category">Category</label>
<select name="show_category" id="">
<?php
$query = "SELECT * FROM categories";
$select_categories = mysqli_query($connection,$query);
confirmQuery($select_categories);
while($row = mysqli_fetch_assoc($select_categories )) {
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
echo "<option value='$cat_id'>{$cat_title}</option>";
}
?>
</select>
</div>
<div class="form-group">
<label for="users">Users</label>
<select name="post_user" id="">
<?php
$users_query = "SELECT * FROM users";
$select_users = mysqli_query($connection,$users_query);
confirmQuery($select_users);
while($row = mysqli_fetch_assoc($select_users)) {
$user_id = $row['user_id'];
$username = $row['username'];
echo "<option value='{$username}'>{$username}</option>";
}
?>
</select>
</div>
<!-- <div class="form-group">
<label for="title">Post Author</label>
<input type="text" class="form-control" name="author">
</div> -->
<div class="form-group">
<select name="show_status" id="">
<option value="draft">Show Status</option>
<option value="published">Published</option>
<option value="draft">Draft</option>
</select>
</div>
<div class="form-group">
<label for="show_tags">Show Tags</label>
<input type="text" class="form-control" name="show_tags">
</div>
<div class="form-group">
<label for="judge_name">Show Tags</label>
<input type="text" class="form-control" name="judge_name">
</div>
<div class="form-group">
<label for="judge_affix">Show Tags</label>
<input type="text" class="form-control" name="judge_affix">
</div>
<div class="form-group">
<label for="show_content">Show Content</label>
<textarea class="form-control " name="show_content" id="" cols="30" rows="5">
</textarea>
</div>
<div class="form-group">
<p>Minor Puppy Dog</p>
</div>
<div class="form-group">
<label for="dog_name">1st Dog Name</label>
<input type="text" class="form-control" name="dog_name">
</div>
<div class="form-group">
<input type="hidden" class="form-control" name="placement" value="1">
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="create_show" value="Publish Show">
</div>
</form>
The mysqli_query only executes one single query.
For executing multiple queries at once, you can use mysqli_multi_query.
Simply replace your mysqli_query with the mysqli_multi_query like so:
$create_show_query = mysqli_multi_query($connection, $query);
I'm trying to add the data from my form into the database. But it's only refreshing. Here is my code for my form:
<form action="" method="post">
<div id="modal1" class="modal modal-fixed-footer">
<div class="modal-content">
<h4>Bedroom</h4><br><br>
<div class="row">
<div class="input-field col s6">
<i class="material-icons prefix">account_circle</i>
<input name="txt_room_ID" id="txt_room_ID" type="text" class="validate" readonly="true"
value="<?php
$sql= "select * from tblBedroom";
$stmt = sqlsrv_query($con,$sql);
while($rs = sqlsrv_fetch_array($stmt)){
$Setting_adminID = $rs['BedRoomID'];
$adminID_new = str_replace("B","",$Setting_adminID);
$adminID = str_pad($adminID_new + 1,11,0,STR_PAD_LEFT);}
echo htmlentities('B'. $adminID);
?>">
<label for="room_ID">Bedroom ID:</label>
</div>
<div class="input-field col s6">
<i class="material-icons prefix">account_circle</i>
<input name="txt_bedroom_no" id="txt_bedroom_no" type="text" class="validate">
<label for="bedroom_no">Room Number</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<select name="txt_Room_Type">
<option value="" disabled selected>Choose Room Type</option>
<?php
$sql = "select * from tblBedRoomType";
$stmt = sqlsrv_query($con,$sql);
while($rs = sqlsrv_fetch_array($stmt)){
$BedroomTypeID = $rs['BedroomTypeID'];
$BedroomName = $rs['BedroomName'];
echo"<option value='$bedroomTypeID'>$BedroomName</option>";
}
?>
</select>
<label>Room Type</label>
</div>
</div>
</div><!--END OF MODAL CONTENT-->
<div class="modal-footer">
<a class="modal-action modal-close waves-effect waves-green btn-flat ">Cancel</a>
<input id="btn_Save" name="btn_Save" type="submit" value="Save" class="waves-effect waves-green btn-flat " onclick="return confirm('Are You Sure You Want To Save?');">
</div>
</div>
</form>
The php below the form:
<?php
if(isset($_POST['btn_Save'])){
$txt_room_ID = $_POST['txt_room_ID'];
$txt_bedroom_no = $_POST['txt_bedroom_no'];
$txt_Room_Type = $_POST['txt_Room_Type'];
$AcctStat = 1;
$sql = "insert into tblBedRoom (BedRoomID,BedroomType,RoomNo,AcctStat)
values('$txt_room_ID','$txt_Room_Type','$txt_bedroom_no','$AcctStat')";
$stmt = sqlsrv_query($con,$sql);
echo "<meta http-equiv='refresh' content='0'>";
}
?>
And my columns in my tblBedRoom are:
BedRoomID | BedroomType | RoomNo |AcctStat
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I am trying to submit this form and upon the submission it should redirect to itself back. However, the problem I am facing is that, after I submit the form it display this error:
Notice: Undefined index: id in C:\xampp\htdocs\iNetLink\configuration.php on line 9
Here is my form file:
<?php include("header.php");
if(ISSET($_POST['node'])){
$nodeID = $_POST['node'];
}else{
$nodeID = 0;
}
$requestID = $_GET['id'];
$qConfigurationForm = "SELECT * FROM configuration WHERE request_id = '$requestID' ";
$rConfigurationForm = $connection->query($qConfigurationForm);
$rowCount = $rConfigurationForm->rowCount();
$row = $rConfigurationForm->fetch();
if($rowCount > 0){
?>
<section id="content">
<section class="vbox">
<section class="scrollable padder">
<ul class="breadcrumb no-border no-radius b-b b-light pull-in">
<li></li>
</ul>
<div class="m-b-md">
<h3 class="m-b-none">Configuration</h3>
</div>
<section class="panel panel-default">
<!-- Tabs for navigation -->
<?php include("tabs.php");?>
</section>
<!-- Tabs for navigation ended -->
<!--forms-->
<section class="panel panel-default">
<header class="panel-heading font-bold">Line Configuration Information</header>
<div class="panel-body">
<form class="form-horizontal" method="post" action="configuration.php" data-validate="parsley">
<div class="col-md-6">
<div class="form-group">
<label class="col-sm-3 control-label" for="requestID"></label>
<div class="col-sm-6">
<input type="hidden" name="requestID" id="requestID" value="<?php echo $requestID;?>">
<input type="hidden" name="isUpdate" value="1" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Node</label>
<div class="col-sm-6">
<select name="node" id="node" class="form-control m-b" onchange="GetId()" data-required="true">
<option></option>
<?php
$qNode = "SELECT id,node FROM node WHERE id = '$nodeID'";
$rNode = $connection->query($qNode);
foreach($rNode as $node){
$nodeid = $node['id'];
$nodeName = $node['node'];
echo "<option value=\"$nodeid\" selected=\"selected\">$nodeName</option>";
}
echo "<optgroup></optgroup>";
$qNode = "SELECT id,node FROM node WHERE `status` = 1";
$rNode = $connection->query($qNode);
foreach($rNode as $node){
$nodeid = $node['id'];
$nodeName = $node['node'];
echo "<option value=\"$nodeid\">$nodeName</option>";
}
?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Connection Type</label>
<div class="col-sm-6">
<select name="connectionType" id="connectionType" class="form-control m-b" data-required="true">
<option></option>
<?php
$connection_type = $row['connection_type'];
$qConnectionConfigType = "SELECT * FROM `connection_type_config` WHERE id = '$connection_type'";
$rConnectionConfigType = $connection->query($qConnectionConfigType);
foreach($rConnectionConfigType as $connectionConfigType){
$connectionConfigID = $connectionConfigType['id'];
$connectionType = $connectionConfigType['connection_type'];
echo "<option value=\"$connectionConfigID\" selected=\"selected\">$connectionType</option>";
}
echo "<optgroup></optgroup>";
$qConnectionConfigType = "SELECT * FROM `connection_type_config`";
$rConnectionConfigType = $connection->query($qConnectionConfigType);
foreach($rConnectionConfigType as $connectionConfigType){
$connectionConfigID = $connectionConfigType['id'];
$connectionType = $connectionConfigType['connection_type'];
echo "<option value=\"$connectionConfigID\">$connectionType</option>";
}
?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="timeSlot">Time Slot</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="timeSlot" name="timeSlot" value="<?php echo $row['time_slot'];?>" data-required="true">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="dslUnifiNo">DSL/Unifi Username</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="dslUnifiNo" name="dslUnifiNo" value="<?php echo $row['dsl_username'];?>">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="remarksConfig">Remarks</label>
<div class="col-sm-6">
<textarea rows="4" cols="50" class="form-control" id="remarksConfig" name="remarksConfig"><?php echo $row['remarks'];?></textarea>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="col-sm-3 control-label">Port</label>
<div class="col-sm-6">
<select name="portNo" id="portNo" name="portNo" class="form-control m-b" data-required="true">
<!-- <option></option>
<?php
$qPortNo = "SELECT * FROM node_port WHERE node_id = '$nodeID' AND `status` = 1";
$rPortNo = $connection->query($qPortNo);
foreach($rPortNo as $portNo){
$portID = $portNo['id'];
$number = $portNo['port_no'];
echo "<option value=\"$portID\">$number</option>";
}
?> -->
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">E1 Circuit No.</label>
<div class="col-sm-6">
<select name="e1circuitNo" id="e1circuitNo" class="form-control m-b">
<option></option>
<?php
$e1_circuit_no = $row['e1_curcuit_no'];
$qE1Type = "SELECT * FROM `e1_type` WHERE id = '$e1_circuit_no'";
$rE1Type = $connection->query($qE1Type);
foreach($rE1Type as $e1Type){
$e1ID = $e1Type['id'];
$type = $e1Type['type'];
echo "<option value=\"$e1ID\" selected=\"selected\">$type</option>";
}
echo "<optgroup></optgroup>";
$qE1Type = "SELECT * FROM `e1_type`";
$rE1Type = $connection->query($qE1Type);
foreach($rE1Type as $e1Type){
$e1ID = $e1Type['id'];
$type = $e1Type['type'];
echo "<option value=\"$e1ID\">$type</option>";
}
?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Changed Date</label>
<div class="col-sm-6">
<input class="input-m input-m form-control" type="date"
data-date-format="dd-mm-yyyy" id="changedDate" name="changedDate" value="<?php echo $row['changed_date'];?>" data-required="true">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="dslUnifiIp">DSL/Unifi IP</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="dslUnifiIp" name="dslUnifiIp" value="<?php echo $row['dsl_ip'];?>">
</div>
</div>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<div class="btn-toolbar pull-right">
<input type="hidden" name="id" value="<?=$requestID;?>">
<input class="btn btn-warning " type="reset" value="Reset" name="reset" >
<input class="btn btn-info " type="submit" value="Save Draft" name="saveDraft">
<input class="btn btn-success " type="submit" value="Submit" name="Submit">
</div>
</div>
</form>
</div>
</section>
Line 9 is the $requestID = $_GET['id'];
Here is the executer for the form:
<?php
include("dbconnection.php");
session_start();
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
$requesterID = $_SESSION['id'];
print_r($_POST);
$node = $_POST['node'];
$connectionType = $_POST['connectionType'];
$timeSlot = $_POST['timeSlot'];
$dslUnifiNo = $_POST['dslUnifiNo'];
$remarksConfig = $_POST['remarksConfig'];
$port = $_POST['port'];
$e1circuitNo = $_POST['e1circuitNo'];
$changedDate = $_POST['changedDate'];
$dslUnifiIp = $_POST['dslUnifiIp'];
$requestID = $_POST['requestID'];
if(ISSET($_POST['isUpdate'])){
$qinsert = "UPDATE configuration SET
node = '$node',
connection_type = '$connectionType',
time_slot = '$timeSlot',
dsl_username = '$dslUnifiNo',
remarks = '$remarksConfig',
port = '$port',
e1_curcuit_no = '$e1circuitNo',
changed_date = '$changedDate',
dsl_ip = '$dslUnifiIp'
WHERE request_id = '$requestID'";
}else
{
$qinsert = "REPLACE INTO configuration SET
node = '$node',
connection_type = '$connectionType',
time_slot = '$timeSlot',
dsl_username = '$dslUnifiNo',
remarks = '$remarksConfig',
port = '$port',
e1_curcuit_no = '$e1circuitNo',
changed_date = '$changedDate',
dsl_ip = '$dslUnifiIp',
request_id = '$requestID'";
$requestID = $connection->lastInsertId();
if($status == 1){
$action = "lineApplication.php";
$dateCreated = date("Y-m-d H:i:s");
$userTypeID = 5;
$qinsertTss = "INSERT INTO notifications SET
user_id = '$requesterID',
action = '$action',
item_id = '$requestID',
created_at = '$dateCreated',
receive_user_type_id = '$userTypeID'";
$rinsertTss = $connection->query($qinsertTss);
}
}
$rinsert = $connection->query($qinsert);
header("location:configuration.php?id=$requestID");
Help! :)
You are sending you're POST to configuration.php not to your executer?
<form class="form-horizontal" method="post" action="configuration.php" data-validate="parsley">
Also...
$requestID = $_GET['id'];
this will error because $_GET['id'] is not available during POST. to fix your code try this:
$requestID = (isset($_GET['id'])) ? $_GET['id'] : 'add ID for POST';
or make sure your form is sending in the right PHP file