How to display multiple rows from database to multiple textbox? - php

Data in 'lcAmount' in the database is '15' for 4 rows and I want to call it to multiple textbox per row.
Database:
HTML CODE:
<div class="form-group">
<label class="control-label col-lg-4">Sick leave unconsumed</label>
<div class="col-lg-3">
<input type="text" id="sickLeave" name="sickLeave" class="form-control" <?php echo $sickUnconsumed; ?> />
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-4">Vacation uncomsumed</label>
<div class="col-lg-3">
<input type="text" id="vacation" name="vacation" class="form-control" <? php echo $vacationUnconsumed ?>/>
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-4">Maternity uncomsumed</label>
<div class="col-lg-3">
<input type="text" id="inpUN" name="inpUN" class="form-control" <? php echo $maternityUnconsumed ?>/>
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-4">Emergency uncomsumed</label>
<div class="col-lg-3">
<input type="text" id="inpUN" name="inpUN" class="form-control" <? php echo $emergencyUnconsumed ?>/>
</div>
</div>
Controller PHP
$query = "SELECT lcAmount FROM leavecounts WHERE accountID = ?";
while($row = mysql_fetch_array($query)){
$sickUnconsumed = $row['lcAmount'];
$vacationUnconsumed = $row['lcAmount'];
$maternityUnconsumed = $row['lcAmount'];
$emergencyUnconsumed = $row['lcAmount'];
}

Answer
$query = "SELECT lcAmount FROM leavecounts WHERE accountID = ? and ltypeID =1";
$params1 = array($accID);
$stmt = sqlsrv_query($con, $query, $params1);
while($row1 = sqlsrv_fetch_array($stmt)){
$sickUnconsumed = $row1['lcAmount'];
}
$query1 = "SELECT lcAmount FROM leavecounts WHERE accountID = ? and ltypeID =2";
$params2 = array($accID);
$stmt1 = sqlsrv_query($con, $query1, $params2);
while($row2 = sqlsrv_fetch_array($stmt1)){
$vacationUnconsumed = $row2['lcAmount'];
}
$query2 = "SELECT lcAmount FROM leavecounts WHERE accountID = ? and ltypeID =3";
$params3 = array($accID);
$stmt2 = sqlsrv_query($con, $query2, $params3);
while($row3 = sqlsrv_fetch_array($stmt2)){
$maternityUnconsumed = $row3['lcAmount'];
}
$query3 = "SELECT lcAmount FROM leavecounts WHERE accountID = ? and ltypeID =4";
$params4 = array($accID);
$stmt3 = sqlsrv_query($con, $query3, $params4);
while($row4 = sqlsrv_fetch_array($stmt3)){
$emergencyUnconsumed = $row4['lcAmount'];

This will display multiple values for multiple textboxes you just have to take html in the loop or you can copy the values returned by result set and store them in array and separate the html.
<?php
$query = "SELECT lcAmount FROM leavecounts WHERE accountID = ?";
while($row = mysql_fetch_array($query))
{
$sickUnconsumed = $row['lcAmount'];
$vacationUnconsumed = $row['lcAmount'];
$maternityUnconsumed = $row['lcAmount'];
$emergencyUnconsumed = $row['lcAmount'];
?>
<div class="form-group">
<label class="control-label col-lg-4">Sick leave unconsumed</label>
<div class="col-lg-3">
<input type="text" id="sickLeave" name="sickLeave" class="form-control" value="<?php if(isset($sickUnconsumed)) echo $sickUnconsumed; ?>" >
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-4">Vacation uncomsumed</label>
<div class="col-lg-3">
<input type="text" id="vacation" name="vacation" class="form-control" value="<?php if(isset($vacationUnconsumed)) echo $vacationUnconsumed; ?>">
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-4">Maternity uncomsumed</label>
<div class="col-lg-3">
<input type="text" id="inpUN" name="inpUN" class="form-control" value="<?php if(isset($maternityUnconsumed)) echo $maternityUnconsumed; ?>">
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-4">Emergency uncomsumed</label>
<div class="col-lg-3">
<input type="text" id="inpUN" name="inpUN" class="form-control" value="<?php if(isset($emergencyUnconsumed)) echo $emergencyUnconsumed; ?>">
</div>
</div>
<?php
}//while
?>

Related

Getting the same data for all the records for editing and updating a record in php

I am having two different database tables questions and choices where i am inserting questions in one table and multiple choices in another table where questions table id is foreign key in choices table.
Questions:
Questions_number Text
1 What is HTML?
2 What is PHP?
Choices:
id question_number is_correct text
1 1 1 markup
2 1 0 Hyext
3 1 0 Hyper text markup language
4 2 0 hsdfd
5 2 0 frfwer
6 2 1 Hypertext Preprocessor
If i am trying to edit question number 1 then i need to fetch all the details of questions,Choices and correct option as well.But when i am trying to edit the record for choices as well i am getting the same data which i am getting for question.
HTML:
<?php session_start();
include 'includes/db.php';
$id = (int)$_GET['id'];
$sql = "SELECT * FROM questions q WHERE q.question_number = $id ";
$oppointArr =array();
$result = mysqli_query($mysqli,$sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result))
{
$oppointArr = $row;
echo "Text: " . $row["text"]. "<br>";
}
} else {
echo "0 results";
}
?>
<form class="form-horizontal" action="updatequestions.php" method="post" role="form">
<?php if(isset($msg)) {?>
<div class="<?php echo $msgclass; ?>" id="mydiv" style="padding:5px;"><?php echo $msg; ?></div>
<?php } ?>
<input type='hidden' value='<?=$id;?>' name='question_number'>
<h2>Edit A Question</h1>
<div class="form-group">
<label for="questionno" class="col-sm-2 control-label">Question Number</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['question_number'];?>"
name="question_number" id="question_number" readonly>
</div>
</div>
<div class="form-group">
<label for="question" class="col-sm-2 control-label">Question</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['text'];?>" name="question_text" id="question_text">
</div>
</div>
<input type='hidden' value='<?=$id;?>' name='id'>
<h2>Edit A Choice</h1>
<div class="form-group">
<label for="choice #1" class="col-sm-2 control-label">Choice #1</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['choice1'];?>" name="choice1" id="choice1">
</div>
</div>
<div class="form-group">
<label for="choice #2" class="col-sm-2 control-label">Choice #2</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['choice2'];?>" name="choice2" id="choice2">
</div>
</div>
<div class="form-group">
<label for="choice #3" class="col-sm-2 control-label">Choice #3</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['choice3'];?>" name="choice3" id="choice3">
</div>
</div>
<div class="form-group">
<label for="Correct Choice Number:" class="col-sm-2 control-label">Correct Choice Number:</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['is_correct'];?>" name="is_correct" id="is_correct">
</div>
</div>
<div class="col-sm-offset-2">
<button type="submit" class="btn btn-default" name="submit_user" id="subject">Submit</button>
<button type="cancel" class="btn btn-raised">Cancel</button>
</div>
</form>
Updatequestions:
<?php
include 'includes/db.php';
if(isset($_POST['submit_user']))
{
$questiontext = $_POST['question_text'];
$id=$_POST['question_number'];
$correct_choice = $_POST['correct_choice'];
$choices = array();
$choices[1] = $_POST['choice1'];
$choices[2] = $_POST['choice2'];
$choices[3] = $_POST['choice3'];
$choices[4] = $_POST['choice4'];
$choices[5] = $_POST['choice5'];
$query = "UPDATE questions SET text='$questiontext' WHERE question_number = $id";
$insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__);
if($insert_row) {
foreach($choices as $choice => $value){
if($value != ''){
if($correct_choice == $choice){
$is_correct = 1;
} else {
$is_correct = 0;
}
$query = "UPDATE choices SET is_correct='$is_correct', text='$value' WHERE question_number=$id";
$insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__);
if($insert_row){
continue;
} else {
die('Error : ('.$mysqli->errno . ') '. $mysqli->error);
}
}
}
$msg = 'Question has been added';
}
}
?>
If i try to update the record all the fields are updating with the same data.
WARNING: Do not create SQL statements by concatenating the data with SQL. Use prepared statements.
As for your problem, you use the foreign key of the question to update choices. The key is not the primary key of choices and is not unique. Try using the unique primary key for your SQL.
Instead of this:
$query = "UPDATE choices SET is_correct='$is_correct', text='$value' WHERE question_number=$id";
try this:
$query = "UPDATE choices SET is_correct='$is_correct', text='$value' WHERE id=$choice ";
But of course you should really try to do it all over again using prepared statements instead!
<form class="form-horizontal" action="updatequestions.php" method="post" role="form">
<?php if(isset($msg)) {?>
<div class="<?php echo $msgclass; ?>" id="mydiv" style="padding:5px;"><?php echo $msg; ?></div>
<?php } ?>
<input type='hidden' value='<?=$id;?>' name='question_number'>
<h2>Edit A Question</h1>
<div class="form-group">
<label for="questionno" class="col-sm-2 control-label">Question Number</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['question_number'];?>"
name="question_number" id="question_number" readonly>
</div>
</div>
<div class="form-group">
<label for="question" class="col-sm-2 control-label">Question</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['text'];?>" name="question_text" id="question_text">
</div>
</div>
<input type='hidden' value='<?=$id;?>' name='id'>
<h2>Edit A Choice</h1>
<?php
$choicesql = "SELECT * FROM `choices` WHERE question_number = $id ";
$ChoicetArr =array();
$choiceresult = mysqli_query($mysqli,$choicesql);
$inc=1;
$correctAns ="";
if (mysqli_num_rows($choiceresult) > 0)
{
while($rows = mysqli_fetch_array($choiceresult))
{
$ChoicetArr[] = $rows;
?>
<div class="form-group">
<label for="choice #<?php echo $inc;?>" class="col-sm-2 control-label">Choice #<?php echo $inc;?></label>
<div class="col-sm-5">
<input type="hidden" name="choice_id<?php echo $inc;?>" value="<?php echo $rows['id'];?>">
<input type="text" class="form-control" value="<?php echo $rows['text'];?>" name="choice<?php echo $inc;?>" id="choice<?php echo $inc;?>">
</div>
</div>
<?php
//print_r($rows);
if($rows['is_correct']=="1"){
$correctAns = '<input type="hidden" name="choice_id'.$inc.'" value="'.$rows['id'].'"><div class="form-group">
<label for="Correct Choice Number:" class="col-sm-2 control-label">Correct Choice Number:</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="'.$inc.'" name="is_correct" id="is_correct">
</div>
</div>';
}
$inc++;
}
}
echo $correctAns;
?>
<div class="col-sm-offset-2">
<button type="submit" class="btn btn-default" name="submit_user" id="subject">Submit</button>
<button type="cancel" class="btn btn-raised">Cancel</button>
</div>
</form>
updatequestions.php
<?php
include 'includes/db.php';
if(isset($_POST['submit_user']))
{
$questiontext = $_POST['question_text'];
$id=$_POST['question_number'];
$correct_choice = $_POST['is_correct'];
$choices = array();
$choices[] = array("question"=>$_POST['choice1'], "answer"=>$_POST['choice_id1']);
$choices[] = array("question"=>$_POST['choice2'], "answer"=>$_POST['choice_id2']);
$choices[] = array("question"=>$_POST['choice3'], "answer"=>$_POST['choice_id3']);
$choices[] = array("question"=>$_POST['choice4'], "answer"=>$_POST['choice_id4']);
$choices[] = array("question"=>$_POST['choice5'], "answer"=>$_POST['choice_id5']);
$query = "UPDATE questions SET text='$questiontext' WHERE question_number = $id";
$insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__);
if($insert_row)
{
$inc= 0;
foreach($choices as $choice => $value){
if(count($value)>0){
$answerInc = $choice+1;
if($correct_choice == $answerInc){
$is_correct = 1;
} else {
$is_correct = 0;
}
$text= $value['question'];
$answer = $value['answer'];
//echo "<br>".$text;
//print_r($value);
echo $answerInc;
echo "<br>";
echo $query = "UPDATE choices SET is_correct='$is_correct', text='$text' WHERE id=$answer";
$insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__);
if($insert_row){
continue;
} else {
die('Error : ('.$mysqli->errno . ') '. $mysqli->error);
}
}
$inc++;
}
$msg = 'Question has been Updated Successfully';
header("location:searchquestions.php");
exit;
}
}
?>

Update query not working PHP MySQL

Can anybody help me understand why this update query isn't updating the fields in db.
<?php
$id= $_GET['id'];
$query1 = mysql_query("SELECT * FROM invoice WHERE id=$id limit 1");
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="card">
<div class="card-header">
<strong>Create Tax</strong>
</div> <?php
while($query2=mysql_fetch_array($query1)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
?>
<div class="card-block">
<div class="col-sm-6">
<div class="form-group">
<label for="company">Coustomer Name</label>
<input type="text" class="form-control" id="company" name="cname" value="<?php echo $query2['CustomerName']?>" placeholder="Coustomer Name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="company">Coustomer Mobile </label>
<input type="text" class="form-control" id="company" name="mobile" value="<?php echo $query2['CustomerMobile']?>" placeholder="Coustomer Mobile">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="company">Email</label>
<input type="text" class="form-control" id="company" name="email" value="<?php echo $query2['Email']?>" placeholder="Email">
</div>
</div> <div class="col-sm-6">
<div class="form-group">
<label for="company">Coustomer Streat </label>
<input type="text" class="form-control" id="company" name="streat" value="<?php echo $query2['CustomerStreat']?>" placeholder="Coustomer Streat">
</div>
</div> <div class="col-sm-6">
<div class="form-group">
<label for="company">Coustomer City</label>
<input type="text" class="form-control" id="company" name="city" value="<?php echo $query2['CustomerCity']?>" placeholder="Coustomer City">
</div>
</div> <div class="col-sm-6">
<div class="form-group">
<label for="company">Coustomer State</label>
<input type="text" class="form-control" id="company" name="state" value="<?php echo $query2['CustomerState']?>" placeholder="Coustomer State">
</div>
</div> <div class="col-sm-6">
<div class="form-group">
<label for="company">Coustomer Country</label>
<input type="text" class="form-control" id="company" name="country" value="<?php echo $query2['CustomerCountry']?>" placeholder="Coustomer Country">
</div>
</div> <div class="col-sm-6">
<div class="form-group">
<label for="company">Zip/Pin Code</label>
<input type="text" class="form-control" id="company" name="pin" value="<?php echo $query2['ZipCode']?>" placeholder="ZipCode">
</div>
</div>
<?php } ?>
<div class="col-sm-6">
<div class="form-group">
<label for="company">Tax Name </label><?php
$query1 = mysql_query("SELECT * FROM Tax");
?>
<select id="select" name="tax" class="form-control" size="1"><?php
while($query2=mysql_fetch_array($query1)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
?>
<option value="<?php echo $query2['NameOfTax']?> , <?php echo $query2['TaxPercentage']?>"><?php echo $query2['NameOfTax']?> , <?php echo $query2['TaxPercentage']?></option>
<?php $taxvalue = $query2['TaxPercentage']; ?> <?php } ?>
</select>
</div>
</div> <div class="col-sm-6">
<div class="form-group">
<label for="company">Iteam Name </label>
<input type="text" class="form-control" id="company" name="item" placeholder="Iteam Name" value="<?php echo $query2['ItemName']?>">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="company">Iteam Price </label>
<input type="text" class="form-control" id="company" name="itemprice" placeholder="Iteam price" value="<?php echo $query2['ItemPrice']?>">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="company">Paid Ammount </label>
<input type="text" class="form-control" id="company" name="pammount" placeholder="Paid Ammount" value="<?php echo $query2['PaidAmmount']?>">
</div>
</div> <div class="col-sm-6">
<div class="form-group">
<label for="company">Date</label>
<input type="text" class="form-control" id="company" name="date" placeholder="Date" value="<?php echo $query2['Date']?>">
</div>
</div>
<div class="col-sm-12">
<input type="submit" class="btn btn-outline-success" name="submit" value="Processe Invoice"/>
</div>
</div>
</div>
</div>
</div>
<!--/col-->
</form>
<?php
$cname = $_POST['cname'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$streat = $_POST['streat'];
$city = $_POST['city'];
$state = $_POST['state'];
$country = $_POST['country'];
$pin = $_POST['pin'];
$tax = $_POST['tax'];
$item = $_POST['item'];
$itemprice = $_POST['itemprice'];
$percent = ($taxvalue / 100) * $itemprice ;
$tammount = $percent + $itemprice;
$pammount = $_POST['pammount'];
$uammount = $tammount - $pammount;
$date = $_POST['date'];
$ip = $_SERVER['REMOTE_ADDR'];
$date1 = date('h,i,s');
if(isset($_POST['submit']))
{
$sql = "UPDATE `ero`.`invoice` SET `CustomerName` = '$cname', `CustomerMobile` = '$mobile', `Email` = '$email', `CustomerStreat` = '$streat', `CustomerCity` = '$city', `CustomerState` = '$state', `CustomerCountry` = '$country', `ZipCode` = '$pin', `TaxName` = '$tax', `ItemName` = '$item', `ItemPrice` = '$itemprice', `TotalAmmount` = '$tammount', `PaidAmmount` = '$pammount', `UnpaidAmmount` = '$uammount', `Date` = '$date', `IP` = '$ip', `DateTime` = '$date1' WHERE `invoice`.`id` = '$id'";
$result = mysql_query($sql);
if($result)
$url='customer.php';
echo '<script>window.location = "'.$url.'";</script>';
die;
}
?>
invoice.php
<?php
$query1 = mysql_query("SELECT * FROM invoice");
?>
<table class="table table-bordered table-striped table-condensed">
<thead>
<tr>
<th>Customer Name</th>
<th>Mobile</th>
<th>Email</th>
<th>City</th>
<th>Item Name</th>
<th>Item Price</th>
<th>Tax</th>
<th>Total Ammount</th>
<th>Paid Ammount</th>
<th>Unpaid Ammount</th>
<th>Action</th>
</tr>
</thead>
<tbody><?php
while($query2=mysql_fetch_array($query1)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
?>
<tr>
<td><?php echo $query2['CustomerName']?></td>
<td><?php echo $query2['CustomerMobile']?></td>
<td><?php echo $query2['Email']?></td>
<td><?php echo $query2['CustomerCity']?></td>
<td><?php echo $query2['ItemName']?></td>
<td><?php echo $query2['ItemPrice']?></td>
<td><?php echo $query2['TaxName']?></td>
<td><?php echo $query2['TotalAmmount']?></td>
<td><?php echo $query2['PaidAmmount']?></td>
<td <?php if ($query2['UnpaidAmmount'] > 1) echo 'style="background-color:#FF0000"' ?>><?php if ($query2['UnpaidAmmount'] < 1) echo "Ammount Paid"; else echo $query2['UnpaidAmmount'];?></td>
<td>
<button type="button" class="btn btn-outline-danger btn-sm"><a href="DeleteInvoice.php?id='<?php echo $query2['id'] ?>'" >Delete</a>
<button type="button" class="btn btn-outline-success btn-sm"><a href="ViewInvoice.php?id='<?php echo $query2['id'] ?>'" >View</a></button>
<button type="button" class="btn btn-outline-success btn-sm"><a href="updatecheck.php?id='<?php echo $query2['id'] ?>'" >Update</a></button>
</div> </td> </tr> <?php } ?>
<tr>
this scripts redirects customer.php.if i place a manual id like id = 38. its wroking. but there is no update in MySql Table.i am new to programming please explain with some code examples.
Thanks
Replace single quote in the query with double quotes and since you are already using the double quotes for the query, escape the double quotes for the variables or use string concatenation.
<?php
$id = $_GET['id'];
$cname = $_POST['cname'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$streat = $_POST['streat'];
$city = $_POST['city'];
$state = $_POST['state'];
$country = $_POST['country'];
$pin = $_POST['pin'];
$tax = $_POST['tax'];
$item = $_POST['item'];
$itemprice = $_POST['itemprice'];
$percent = ($taxvalue / 100) * $itemprice;
$tammount = $percent + $itemprice;
$pammount = $_POST['pammount'];
$uammount = $tammount - $pammount;
$date = $_POST['date'];
$ip = $_SERVER['REMOTE_ADDR'];
$date1 = date('h,i,s');
if (isset($_POST['submit']))
{
$sql = "UPDATE `ero`.`invoice` SET `CustomerName` = \"$cname\", `CustomerMobile` = \"$mobile\", `Email` = \"$email\", `CustomerStreat` = \"$streat\", `CustomerCity` = \"$city\", `CustomerState` = \"$state\", `CustomerCountry` = \"$country\", `ZipCode` = \"$pin\", `TaxName` = \"$tax\", `ItemName` = \"$item\", `ItemPrice` = \"$itemprice\", `TotalAmmount` = \"$tammount\", `PaidAmmount` = \"$pammount\", `UnpaidAmmount` = \"$uammount\", `Date` = \"$date\", `IP` = \"$ip\", `DateTime` = \"$date1\" WHERE `invoice`.`id` = \"$id\"";
$result = mysql_query($sql);
if ($result) $url = 'customer.php';
echo '<script>window.location = "' . $url . '";</script>';
die;
?>
UPDATE table
SET col_name = 'new value'
WHERE condition
this is the syntax for updating in sql, try deleting the ``

Multiple Insert Queries in PHP [duplicate]

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);

How to insert multiple rows of data [duplicate]

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);

PHP undefined index id error [duplicate]

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

Categories