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'.
Related
I want only 4 columns from database in below
I have to get only 4 column from database for displaying in textfield please give suggestion for that. The table is shown below thank you
$sql_getnm = "SELECT * FROM util WHERE util_head IN ('wc_email', 'wc_contact_us', 'wc_mobile', 'wc_google_map');";
$result_getnm = $connect->query($sql_getnm);
while($row_getnm = $result_getnm->fetch_array())
$util_value_email_data=?
$util_value_mobile_data=?;
$util_value_map_data=?;
$util_value_data=?;
html form
<form id="submitForm" method="post" role="form" name="hl_form" method="post" enctype="multipart/form-data">
<div class="box-body">
<div class="form-group">
<label for="mobile_number">Email*</label>
<input class="form-control" id="util_value_email" name="util_value_email" value="<?Php echo $util_value_email_data; ?>" maxlength="250" placeholder="Enter Email Address" type="text">
</div>
<div class="form-group">
<label for="mobile_number">Mobile*</label>
<input class="form-control" id="util_value_mobile" name="util_value_mobile" value="<?Php echo $util_value_mobile_data; ?>" maxlength="250" placeholder="Enter Mobile Number" type="text">
</div>
<div class="form-group">
<label for="mobile_number">Map*</label>
<input class="form-control" id="util_value_map" name="util_value_map" value="<?Php echo $util_value_map_data; ?>" maxlength="250" placeholder="Enter Map Address" type="text">
</div>
<div class="form-group">
<label for="description" class="required">Description*</label>
<textarea class="form-control" style="resize: none;" id="util_value" name="util_value" rows="3" placeholder="Enter Description"><?Php echo $util_value_data; ?></textarea>
</div>
<div class="box-footer">
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</form>
Below is the code from which you can get all the four data that you want.
$sql_getnm = "SELECT * FROM util WHERE util_head IN ('wc_email', 'wc_contact_us', 'wc_mobile', 'wc_google_map');";
$result_getnm = $connect->query($sql_getnm);
while($row_getnm = $result_getnm->fetch_array()) {
if($row_getnm['util_head'] == 'wc_email'){
$util_value_email_data = $row_getnm['util_value'];
}
if($row_getnm['util_head'] == 'wc_contact_us'){
$util_value_contact_data = $row_getnm['util_value'];
}
if($row_getnm['util_head'] == 'wc_mobile'){
$util_value_mobile_data = $row_getnm['util_value'];
}
if($row_getnm['util_head'] == 'wc_google_map'){
$util_value_map_data = $row_getnm['util_value'];
}
}
You need to replace * with columns name with comma separation.
$sql_getnm = "SELECT Columname1,Columname2,Columname3,Columname4 FROM util WHERE util_head IN ('wc_email', 'wc_contact_us', 'wc_mobile', 'wc_google_map');";
$result_getnm = $connect->query($sql_getnm);
while($row = mysql_fetch_array($result_getnm, MYSQL_ASSOC)
$util_value_email_data= $row['Columname1'];
$util_value_mobile_data= $row['Columname2'];
$util_value_map_data= $row['Columname3'];
$util_value_data= $row['Columname4'];
I am working on a PHP form where the user can either add information or update previous information.
When Building I entered some information manually into the database to get things working. Now I am find out that the form will not show up for new users because there is no user_id in the database yet.(I tested and manually added the user_ids and the the form then showed up.)
I believe its because my form is wrapped in a while loop.
How can I change my form(INPUTS) to populate if there is no data for the user yet?
<?php
$user_id = $_SESSION['User']['logged_in'];
$businessProfileQuery = $db->query("SELECT * FROM sellerBusinessProfile
WHERE user_id = $user_id;") or die(mysqli_error($db));
while( $row = $businessProfileQuery->fetch_assoc() ) {
$savedBusinessName = $row['businessName'];
$$savedBusinessAddress = $row['businessAddress'];
?>
<form>
<div class="form-group row">
<div class="col-lg-12">
<p class="content-header">BUSINESS ADDRESS</p>
<p class="seller-profile-text"></p>
<input name="businessName" type="name" class="form-control
seller-input" value="<?php echo $savedBusinessName; ?>">
<input name="businessAddress" type="name" class="form-control"
value="<?php echo $savedBusinessAddress; ?>" >
</div>
</form>
<?php
}
?>
You can count number of rows.
If number of rows less then or equal 0 then there is no data set for this user.
If number of rows greater then 0 then data already available for this user.
<?php
$user_id = $_SESSION['User']['logged_in'];
$businessProfileQuery = $db->query("SELECT * FROM sellerBusinessProfile
WHERE user_id = $user_id;") or die(mysqli_error($db));
if ($businessProfileQuery->num_rows <= 0) {
?>
<form>
<div class="form-group row">
<div class="col-lg-12">
<p class="content-header">BUSINESS ADDRESS</p>
<p class="seller-profile-text"></p>
<input name="businessName`enter code here`" type="name" class="form-control
seller-input" value="">
<input name="businessAddress" type="name" class="form-control"
value="" >
</div>
</form>
<?php
}
?>
Hope to help you!
<?php
$user_id = $_SESSION['User']['logged_in'];
$businessProfileQuery = $db->query("SELECT * FROM sellerBusinessProfile
WHERE user_id = $user_id;") or die(mysqli_error($db));
$savedBusinessName = "";
$savedBusinessAddress = "";
while( $row = $businessProfileQuery->fetch_assoc() ) {
$savedBusinessName = $row['businessName'];
$savedBusinessAddress = $row['businessAddress'];
}
?>
<form>
<div class="form-group row">
<div class="col-lg-12">
<p class="content-header">BUSINESS ADDRESS</p>
<p class="seller-profile-text"></p>
<input name="businessName" type="name" class="form-control
seller-input" value="<?php echo $savedBusinessName; ?>">
<input name="businessAddress" type="name" class="form-control"
value="<?php echo $savedBusinessAddress; ?>" >
</div>
</form>
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 currently trying to make a type blog site and I'm having trouble uploading the post in the edition of a post already created .
I'm currently using the Bootstrap and do not know if this will influence my code or not but the php error is in the lines of the form table.
<?php
include("includes/connect.php");
if (isset($_GET['edit'])) {
$edit_id = $_GET['edit'];
$edit_query = "select * from posts where post_id='$edit_id'";
$run_edit = mysql_query($edit_query);
while ($edit_row=mysql_fetch_array($run_edit)) {
$post_id = $edit_row['post_id'];
$post_title = $edit_row['post_title'];
$post_author = $edit_row['post_author'];
$post_keywords = $edit_row['post_keywords'];
$post_image = $edit_row['post_image'];
$post_content = $edit_row['post_content'];
}
}
?>
<div class="col-md-10" id="content-area">
<div class="container">
<div class="row">
<h2>Edit Post</h2>
<form method="post" action="edit_post.php?edit_form=<?php echo $edit_id ?>" enctype="multipart/form-data">
<fieldset class="form-group">
<label for="title">Title</label>
<input type="text" name="title" class="form-control" id="title" placeholder="title" value="<?php echo $post_title; ?>">
</fieldset>
<fieldset class="form-group">
<label for="author">Author</label>
<input type="text" name="author" class="form-control" id="author" placeholder="author" value="<?php echo $post_author; ?>">
</fieldset>
<fieldset class="form-group">
<label for="keywords">Keywords</label>
<input type="text" name="keywords" class="form-control" id="keywords" placeholder="keywords" value="<?php echo $post_keywords; ?>">
</fieldset>
<fieldset class="form-group">
<label for="image">Image</label>
<input type="file" name="image" class="form-control" id="image" placeholder="image">
<img src="../images/<?php echo $post_image; ?>" width="100" height="100">
</fieldset>
<fieldset class="form-group">
<label for="content">Content</label>
<textarea name="content" cols="20" rows="20" class="form-control" id="content" placeholder="content"><?php echo $post_content; ?></textarea>
</fieldset>
<input class="btn btn-primary" type="submit" name="submit" value="Update Post"></input>
</form>
</div>
</div>
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
</html>
<?php
if (isset($_POST['update'])) {
$update_id = $_GET['edit_form'];
$post_title1 = $_POST['title'];
$post_date1 = date('m-d-y');
$post_author1 = $_POST['author'];
$post_keywords1 = $_POST['keywords'];
$post_content1 = $_POST['content'];
$post_image1 = $_FILES['image']['name'];
$image_tmp = $_FILES['image']['tmp_name'];
if($post_title1=='' or $post_author1=='' or $post_keywords1=='' or $post_content1=='' or $post_image1=='') {
echo "<script>alert('Preencha todos os campos')</script>";
exit();
}
else {
move_uploaded_file($image_tmp, "../images/$post_image1");
$update_query = "update posts set post_title='$post_title1',post_date='$post_date1',post_author='$post_author1',post_image='$post_image1',post_keywords='$post_keywords1',post_content='$post_content1' where post_id='update_id'";
if (mysql_query($update_query)) {
echo "<script>alert('O seu post foi atualizado')</script>";
echo "<script>window.open('view_posts.php','_self')</script>";
}
}
}
?>
First of all, you should prevent SQL Injection.
Change this line:
$edit_id = $_GET['edit'];
To this:
$edit_id = (int)$_GET['edit'];
This way, php will always assume that the variable is an int.
The same on update code:
$update_id = $_GET['edit_form'];
To:
$update_id = (int)$_GET['edit_form'];
And then, you forgot to put $ inside SQL query, on update_id variable to run as a PHP variable:
$update_query = (...) where post_id='$update_id'";
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>