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);
Related
<?php
if(isset($_POST["submit"]))
if (!empty($_FILES["uploadImage"]["name"])) {
//Including dbconfig file.
require 'config.php';
$ImageSavefolder = "images/student/";
move_uploaded_file($_FILES["uploadImage"]["tmp_name"] ,
"$ImageSavefolder".$_FILES["uploadImage"]["name"]);
$name = mysqli_real_escape_string($conn, $_POST['name']);
$fathername = mysqli_real_escape_string($conn, $_POST['fathername']);
$htno = mysqli_real_escape_string($conn, $_POST['htno']);
$phoneno = mysqli_real_escape_string($conn, $_POST['phoneno']);
$department = mysqli_real_escape_string($conn, $_POST['department']);
$class = mysqli_real_escape_string($conn, $_POST['class']);
$address = mysqli_real_escape_string($conn, $_POST['address1']);
$address2 = mysqli_real_escape_string($conn, $_POST['address2']);
$city = mysqli_real_escape_string($conn, $_POST['city']);
$state = mysqli_real_escape_string($conn, $_POST['state']);
$zip = mysqli_real_escape_string($conn, $_POST['zip']);
$sql= "INSERT INTO student_detail(name,fathername,htno,phoneno,department,class,address1,address2,city,state,zip) VALUES ('$name','$fathername','$htno','$phoneno','$department','$class','$address','$address2','$city','$state','$zip','".$_FILES['uploadImage']['name']."')";
if(!mysqli_query($conn,$sql))
{
echo "Not Updated";
}
else
{
echo "<br><div class='alert alert-success' role='alert'>Added Sucessfully !</div>";
}
}
?>
This code is not working for adding the following data into database. Did I do anything wrong? Please help me sort the problem.
I already created database with config.php
Form Data
<form method="post" action="" enctype="multipart/form-data">
<div class="form-row">
<div class="form-group col-md-6">
<label for="name">Name</label>
<input type="text" class="form-control" placeholder="Please Enter Name" name="name">
</div>
<div class="form-group col-md-6">
<label for="fathername">Father's Name</label>
<input type="text" class="form-control" placeholder="Please Enter Father's Name" name="fathername">
</div>
<div class="form-group col-md-6">
<label for="htno">Hall Ticket/ Roll No.</label>
<input type="text" class="form-control" placeholder="Please Enter Hall Ticket/ Roll No." name="htno">
</div>
<div class="form-group col-md-6">
<label for="phoneno">Phone Number</label>
<input type="text" class="form-control" placeholder="Please Enter Phone No." name="phoneno">
</div>
<div class="form-group col-md-6">
<label for="department">Department</label>
<select class="form-control" name="department">
<option selected="selected">Choose your Department</option>
<?php
require('config.php');
$result = mysqli_query($conn,"SELECT * FROM department");
while($test= mysqli_fetch_array($result))
{
echo "<option value='".$test['department_name']."'>".$test['department_name']."</option>";
}
?>
</select>
</div>
<div class="form-group col-md-6">
<label for="class">Class</label>
<select class="form-control" name="class">
<option selected="selected">Choose your Class</option>
<?php
require('config.php');
$result = mysqli_query($conn,"SELECT * FROM class");
while($test= mysqli_fetch_array($result))
{
echo "<option value='".$test['class_name']."'>".$test['class_name']."
</option>";
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="address">Address</label>
<input type="text" class="form-control" placeholder="House No./Flat No." name="address1">
</div>
<div class="form-group">
<label for="address2">Address 2 (Optional)</label>
<input type="text" class="form-control" placeholder="Locality/Area/Street" name="address2">
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="city">City</label>
<input type="text" class="form-control" name="city">
</div>
<div class="form-group col-md-4">
<label for="state">State</label>
<select class="form-control" name="state">
<option selected="selected"name="bihar">bihar</option>
<option>...</option>
</select>
</div>
<div class="form-group col-md-2">
<label for="zip">Zip</label>
<input type="text" class="form-control" name="zip">
</div>
<div class="form-group col-md-6">
<label for="profile">Profile Pic</label><br>
<input type="file" accept="image/*" onchange="loadFile(event)" name="uploadImage" id="uploadImage">
<img id="output" style="width:20%;"/>
<script>
var loadFile = function(event) {
var reader = new FileReader();
reader.onload = function(){
var output = document.getElementById('output');
output.src = reader.result;
};
reader.readAsDataURL(event.target.files[0]);
};
</script>
</div>
</div>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
you are trying to insert extra value which is not exist in query see here.
your field
(name,fathername,htno,phoneno,department,class,address1,address2,city,state,zip)
and your values
('$name','$fathername','$htno','$phoneno','$department','$class','$address','$address2','$city','$state','$zip','".$_FILES['uploadImage']['name']."')
you need to do add image field also
your full query
$sql= "INSERT INTO student_detail(name,fathername,htno,phoneno,department,class,address1,address2,city,state,zip,imageFieldName) VALUES ('$name','$fathername','$htno','$phoneno','$department','$class','$address','$address2','$city','$state','$zip','".$_FILES['uploadImage']['name']."')";
<?php
/* dbconnection.php file
$conn = mysqli_connect("localhost","root","12345") or die (mysqli_error());
mysqli_select_db($conn,"student") or die (mysqli_error());
*/
//Including dbconnection file here
include('dbconnection.php');
if(isset($_POST["submit"]))
{
if (!empty($_FILES["uploadImage"]["name"]))
{
$ImageSavefolder = "images/student/";
$name = $_FILES["uploadImage"]["name"];
$tmp_name = $_FILES["uploadImage"]["tmp_name"];
move_uploaded_file(tmp_name, $ImageSavefolder.$name);
$sql = "INSERT INTO students (name,fathername,htno,phoneno,department,class,address1,address2,city,state,zip,image) VALUES ('".$_POST["name"]."','".$_POST["fathername"]."','".$_POST["htno"]."','".$_POST["phoneno"]."','".$_POST["department"]."','".$_POST["class"]."','".$_POST["address1"]."','".$_POST["address2"]."','".$_POST["city"]."','".$_POST["state"]."','".$_POST["zip"]."','".$name."')";
if ($conn->query($sql) === TRUE)
{
echo "<script type= 'text/javascript'>alert('Record Inserted Successfully');</script>";
}
else
{
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
}
}
}
?>
This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 6 years ago.
I checked source code ten times but can't find anymistake,for some reason i remember it was working now it doesn't work,i didn't change anything elsewhere to blame everything wors except update,when iclick Edit button it gives error,meaning that there was something wrong with query.
<?php
$editId = $_GET['id'];
$event = mysqli_query($con, "SELECT * FROM events WHERE id = '$editId'")->fetch_assoc();
if(isset($_POST['submit'])){
$name = $_POST['name'];
$description = $_POST['description'];
$date = $_POST['date'];
$artists = $_POST['artists'];
$tickets = $_POST['tickets'];
$updateQuery = mysqli_query($con, "UPDATE events SET name='$name',description='$description',date='$date', artists='$artists', ticket='$tickets' WHERE id = '$editId'");
}
?>
<?php if(isset($updateQuery) && $updateQuery): ?>
<div class="alert alert-success">
<strong>Successfully Edited</strong>
</div>
<?php endif; ?>
<?php if(isset($updateQuery) && !$updateQuery): ?>
<div class="alert alert-danger">
<strong>Error</strong>
</div>
<?php endif; ?>
<form action="<?php echo $app_host; ?>/admin/?page=editevent&id=<?php echo $editId; ?>" method="post">
<div class="form-group">
<label for="name">Name</label>
<input value="<?php echo $event['name']; ?>" required="true" type="text" class="form-control" id="name" name="name">
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea class="form-control" id="description" name="description"><?php echo $event['description'];; ?></textarea>
</div>
<div class="form-group">
<label for="date">Date</label>
<input value="<?php echo $event['date']; ?>" required="true" type="text" class="form-control" id="date" name="date">
</div>
<div class="form-group">
<label for="artists">Artists</label>
<input value="<?php echo $event['artists']; ?>" required="true" type="text" class="form-control" id="artists" name="artists" data-role="tagsinput">
</div>
<div class="form-group">
<label for="tickets">Ticket Link</label>
<input value="<?php echo $event['tickets']; ?>" required="true" type="text" class="form-control" id="tickets" name="tickets" data-role="tagsinput">
</div>
<input name="submit" type="submit" class="btn btn-default" value="Edit Event" />
</form>
Okay,so you declare a variable ,
$tickets = $_POST['tickets'];
and than you say
$updateQuery = mysqli_query($con, "UPDATE events SET name='$name',description='$description',date='$date', artists='$artists', ticket='$tickets' WHERE id = '$editId'");
}
and solution is simple:
$updateQuery = mysqli_query($con, "UPDATE events SET name='$name',description='$description',date='$date', artists='$artists', tickets='$tickets' WHERE id = '$editId'");
}
simply changed 'ticket' in query to 'tickets'.
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 am trying to build a multiple choice exam portal. It's working fine but when I am adding a question in my add.php file it get inserted but the choices of the question is not inserted in database
Here is my database:
Choices table:
Questions table:
Here is my code for add.php
<?php include 'includes/header.php'; ?>
<?php include 'config/config.php'; ?>
<?php include 'lib/Database.php'; ?>
<?php
$db = new Database();
if (isset($_POST['submit'])) {
//Grab Post Data
$question_number = $_POST['question_number'];
$question_text = $_POST['question_text'];
$correct_choice = $_POST['correct_choice'];
$choices = array();
$choices[1] = $_POST['choice1'];
$choices[2] = $_POST['choice2'];
$choices[3] = $_POST['choice3'];
$choices[4] = $_POST['choice4'];
//Insert question into database
$query = "INSERT INTO `questions`(question_number, text) VALUES('$question_number','$question_text')";
$insert_row = $db->insert($query);
//validate
if ($insert_row) {
foreach ($choices as $choice => $value) {
if ($value != '') {
if ($correct_choice == $choice) {
$is_correct = 1;
} else {
$is_correct = 0;
}
//Choice Query
$query = "INSERT INTO `choices`(question_number,is_correct,text) VALUES('$question_number','$is_correct',
'$value')";
//insert row
$insert_row = $db->insert($query);
if ($insert_row) {
continue;
} else {
die($mysqli->error);
}
}
}
}
}
$query1 = "SELECT * FROM `questions`";
$result = $db->select($query1);
$total = $result->num_rows;
$next = $total + 1;
?>
<form class="form-horizontal" action="signup.php" method="POST">
<fieldset>
<div id="legend">
<legend class="text-center">Add Questions</legend>
</div>
<div class="control-group">
<label class="control-label" for="username">Question Number</label>
<div class="controls">
<input name="question_number" value="<?php echo $next; ?>" placeholder="" class="form-control input-lg" type="number"/>
</div>
</div>
<div class="control-group">
<label class="control-label" for="text">Question Text</label>
<div class="controls">
<input name="text" placeholder="" class="form-control input-lg" type="text">
</div>
</div>
<div class="control-group">
<label class="control-label" for="choice1">#Choice 1</label>
<div class="controls">
<input name="choice1" placeholder="" class="form-control input-lg" type="text">
</div>
</div>
<div class="control-group">
<label class="control-label" for="username">#Choice 2</label>
<div class="controls">
<input id="choice2" name="choice2" placeholder="" class="form-control input-lg" type="text">
</div>
</div>
<div class="control-group">
<label class="control-label" for="username">#Choice 3</label>
<div class="controls">
<input id="choice3" name="choice3" placeholder="" class="form-control input-lg" type="text">
</div>
</div>
<div class="control-group">
<label class="control-label" for="username">#Choice 4</label>
<div class="controls">
<input id="username" name="choice4" placeholder="" class="form-control input-lg" type="text">
</div>
</div>
<div class="control-group">
<label class="control-label" for="username">Correct Choice Number</label>
<div class="controls">
<input id="username" name="correct_choice" placeholder="" class="form-control input-lg" type="number"/>
</div>
</div>
<input type="submit" name="submit" class="btn btn-block btn-primary" value="Submit" class="submit"/>
</fieldset>
</form>
<?php include 'includes/footer.php';?>
Now only the question is adding in the database but not the choices.
The syntax appears to be correct, but you can delete simple quotes around numbers. In addition, it's better to make one request to insert all choices.
//validate
if ($insert_row) {
$values = [];
foreach ($choices as $choice => $value) {
if ($value == '')
continue;
$is_correct = $correct_choice == $choice ? 1 : 0;
$values[] = "($question_number, $is_correct, '$value')";
}
if (count($values) > 0)
{
$query = "INSERT INTO choices (question_number, is_correct, text) VALUES ".implode(',', $values);
$insert_row = $db->insert($query);
if (!$insert_row)
die($mysqli->error);
}
}
And your variables $value and $question_number come from $_POST variable, you have to use prepared statement to protect your query from SQL injection.
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>