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
Related
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;
}
}
?>
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
?>
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 really hope you can help me.
I've done a form, where I can opt if a client I'm adding to the database is "Active or "Inactive", using a dropdown select box.
My code saves all the data correctly to the datbase, but when I want to edit the client, the option displays always as "Active", ignoring the value from the database.
I have 2 files:
edita_clientes.php - the form where I can edit the clients values
salvar_edicao.php - the file that saves the edition.
Here are the codes:
edita_clientes.php:
<?php
#ini_set('display_errors', '1');
error_reporting(E_ALL);
$id = $_GET["id_cliente"];
settype($id, "integer");
mysql_connect("localhost", "root", "");
mysql_select_db("sistema");
$resultado = mysql_query("select * from tabela where id_cliente = $id");
$dados = mysql_fetch_array($resultado);
mysql_close();
?>
<form id="edita_pj" name="edita_pj" method="post" action="salvar_edicao.php">
<input type="hidden" name="id_cliente" id="id_cliente" value="<?php echo $id;?>" />
<div class="box-body">
<div class="form-group">
<label>Razão Social</label>
<input type="text" name="razao" id="razao" class="form-control" value="<?php echo $dados["razao"];?>" />
</div>
<div class="form-group">
<label>Nome Fantasia</label>
<input type="text" name="fantasia" id="fantasia" class="form-control" value="<?php echo $dados["fantasia"];?>" />
</div>
</div>
<div class="box-body">
<div class="form-group">
<label>CNPJ</label>
<input type="text" name="cnpj" id="cnpj" class="form-control" data-inputmask='"mask": "999.999.999-99"' data-mask value="<?php echo $dados["cnpj"];?>">
</div>
</div>
<div class="box-body">
<div class="row">
<div class="col-xs-9">
<label>Logradouro</label>
<input type="text" name="logradouro" id="logradouro" class="form-control" value="<?php echo $dados["logradouro"];?>">
</div>
<div class="col-xs-3">
<label>Número</label>
<input type="text" name="numero" id="numero" class="form-control" value="<?php echo $dados["numero"];?>">
</div>
</div>
</div>
<div class="box-body">
<div class="row">
<div class="col-xs-9">
<label>Bairro</label>
<input type="text" name="bairro" id="bairro" class="form-control" value="<?php echo $dados["bairro"];?>">
</div>
<div class="col-xs-3">
<label>CEP</label>
<input type="text" name="cep" id="cep" class="form-control" data-inputmask='"mask": "99999-999"' data-mask value="<?php echo $dados["cep"];?>">
</div>
</div>
</div>
<div class="box-body">
<div class="row">
<div class="col-xs-10">
<label>Cidade</label>
<input type="text" name="cidade" id="cidade" class="form-control" value="<?php echo $dados["cidade"];?>">
</div>
<div class="col-xs-2">
<label>UF</label>
<input type="text" name="uf" id="uf" class="form-control" value="<?php echo $dados["uf"];?>">
</div>
</div>
</div>
<div class="box-body">
<div class="row">
<div class="col-xs-9">
<label>E-mail</label>
<input type="text" name="email" id="email" class="form-control" value="<?php echo $dados["email"];?>">
</div>
<div class="col-xs-3">
<label>Telefone</label>
<input type="text" name="telefone" id="telefone" class="form-control" data-inputmask='"mask": "(99) 9999.9999"' data-mask value="<?php echo $dados["telefone"];?>"/>
</div>
</div>
</div>
<div class="box-body">
<div class="row">
<div class="col-xs-9">
<label>Contato</label>
<input type="text" name="contato" id="contato" class="form-control" value="<?php echo $dados["contato"];?>">
</div>
<div class="col-xs-3">
<label>Estado</label>
<select class="form-control" name="estado" id="estado" value=""><?php echo $dados["estado"];?>
<option>Ativo</option>
<option>Inativo</option>
</select>
</div>
</div>
<div class="form-group">
<label>Observações</label>
<textarea class="form-control" name="obs" id="obs" rows="6" ><?php echo $dados["obs"];?>
</textarea>
</div>
</div>
<div class="box-footer">
<button type="submit" name="Submit" class="btn btn-primary">Salvar</button>
</div>
</form>
salvar_edicao.php:
<?php
#ini_set('display_errors', '1');
error_reporting(E_ALL);
$razao = $_POST["razao"];
$fantasia = $_POST["fantasia"];
$cnpj = $_POST["cnpj"];
$logradouro = $_POST["logradouro"];
$numero = $_POST["numero"];
$bairro = $_POST["bairro"];
$cep = $_POST["cep"];
$cidade = $_POST["cidade"];
$uf = $_POST["uf"];
$email = $_POST["email"];
$telefone = $_POST["telefone"];
$contato = $_POST["contato"];
$estado = $_POST["estado"];
$obs = $_POST["obs"];
$id = $_POST["id_cliente"];
mysql_connect("localhost", "root", "");
mysql_select_db("sistema");
mysql_query("UPDATE tabela SET razao = '$razao', fantasia = '$fantasia', cnpj = '$cnpj', logradouro = '$logradouro', numero='$numero', bairro='$bairro', cep='$cep', cidade = '$cidade', uf='$uf', email = '$email', telefone = '$telefone', contato = '$contato', estado = '$estado', obs = '$obs' WHERE tabela.id_cliente = $id");
mysql_close();
header("Location: consulta.php");
?>
You need to add 'selected' to the option that you want to be selected, based on a value from the form/db. Here is an example using $value as the option value that you want selected.
<select class="form-control" name="estado" id="estado">
<option <?php echo $value == 'Ativo' ? selected : '' ?>>Ativo</option>
<option <?php echo $value == 'Inativo' ? selected : '' ?>>Inativo</option>
</select>
Also, your <select> tag does not require a 'value' element..
The HTML select element does not have a value attribute. For the select to do what you want you need to add the selected attribute to the option you want selected. It's always showing as 'Active' because that's the first option and it is the default.
The resulting post-php HTML will need to look something like this stripped back example for 'Inactive' to be selected.
<select>
<option>Active</option>
<option selected>Inactive</option>
</select>
Thank's for all the help!
The solution I've found was:
<?php
$resultado = mysql_query("select * from tabela where id_cliente = $id");
$dados = mysql_fetch_array($resultado);
$query = mysql_query("SELECT * FROM estado");
?>
And the html part:
<select class="form-control" name="estado" id="estado">
<option selected="selected"><?php echo $dados["estado"];?></option>
<option value="Ativo">Ativo</option>
<option value="Inativo">Inativo</option>
</select>