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>
Related
I am working on a web form that includes a select drop down with two options: "Cedula" (in English, "Identification") and "Pasaporte" (in English, "Passport").
Here is an image of my web form so far.
Please help me achieve the following goal: when the user selects "Cedula", they are limited to 10 digits, but when they select "Pasaporte, they are not limited to 10 digits.
Here is my code so far:
<?php
if ($_GET['id']) {
$cliente = $clienteNegocio->recuperar($_GET['id']);
$txtAction = 'Editar';
}else{
$cliente = new cliente();
$txtAction = 'Agregar';
}
?>
<div class="container">
<div class="page-header">
<h1><?php echo $txtAction; ?> Cliente</h1>
</div>
<form role="form" method="post" id="principal">
<input type="hidden" name="id" value="<?php echo $cliente->getId();?>" >
<div class="form-group">
<label for="nombre">Nombre</label>
<input type="text" class="form-control" id="nombre" name="nombre" placeholder="Nombre" value="<?php echo $cliente->getNombre();?>" required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="apellido">Apellidos</label>
<input type="text" class="form-control" id="apellido" name="apellido" placeholder="Apellido" value="<?php echo $cliente->getApellido();?>" required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="tipoDoc">Tipo de Documento</label>
<select class="form-control" id="tipoDoc" name="tipoDoc">
<option value="Cedula" <?php if($cliente->getTipoDoc() == 'Cedula') {echo "selected";} ?> >Cedula</option>
<option value="Pasaporte" <?php if($cliente->getTipoDoc() == 'Pasaporte') {echo "selected";} ?> >Pasaporte</option>
</select>
</div>
<div class="form-group">
<label for="nroDoc">Numero de Documento</label>
<input type="number" class="form-control" id="nroDoc" maxlength=10 oninput="if(this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);"
name="nroDoc" placeholder="Numero de Documento" value="<?php echo $cliente ->getNroDoc();?>" required>
<div class="help-block with-errors"></div>
</div>
There you go.
I have written a javascript function to check the length with you select Pasaporte/Cedula.
Secondly, in <input type = "number"/> you cannot set maxLength. Hence you have to set <input type = "text" />. Also, an onKeyPress event to verify the input as number
<?php
if ($_GET['id']) {
$cliente = $clienteNegocio->recuperar($_GET['id']);
$txtAction = 'Editar';
}else{
$cliente = new cliente();
$txtAction = 'Agregar';
}
?>
<script>
function setMaxLength(){
var inputVal = document.getElementById("tipoDoc")
var selIndex = inputVal.options[inputVal.selectedIndex].value
var inputNum = document.getElementById("nroDoc");
if( selIndex === "Cedula"){
inputNum.maxLength = 10
selIndex.substr(0, 9);
inputNum.value = inputNum.value.substr(0, 9);
} else{
// Set your own limit here
// if selIndex === "Pasaporte"
inputNum.maxLength = 20
}
}
</script>
<div class="container">
<div class="page-header">
<h1><?php echo $txtAction; ?> Cliente</h1>
</div>
<form role="form" method="post" id="principal">
<input type="hidden" name="id" value="<?php echo $cliente->getId();?>" >
<div class="form-group">
<label for="nombre">Nombre</label>
<input type="text" class="form-control" id="nombre" name="nombre" placeholder="Nombre" value="<?php echo $cliente->getNombre();?>" required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="apellido">Apellidos</label>
<input type="text" class="form-control" id="apellido" name="apellido" placeholder="Apellido" value="<?php echo $cliente->getApellido();?>" required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="tipoDoc">Tipo de Documento</label>
<select class="form-control" id="tipoDoc" name="tipoDoc" onChange="setMaxLength()">
<option value="Cedula" <?php if($cliente->getTipoDoc() == 'Cedula') {echo "selected";} ?> >Cedula</option>
<option value="Pasaporte" <?php if($cliente->getTipoDoc() == 'Pasaporte') {echo "selected";} ?> >Pasaporte</option>
</select>
</div>
<div class="form-group">
<label for="nroDoc">Numero de Documento</label>
<!--
Here onKeyPress method is used to check if the input is a number
in <input type = "number"/> you cannot set maxLength
hence you have to set <input type = "text" />
-->
<input type="text" class="form-control" id="nroDoc" maxlength=10 onkeypress="if ( isNaN(this.value + String.fromCharCode(event.keyCode) )) return false;"
name="nroDoc" placeholder="Numero de Documento" value="<?php echo $cliente ->getNroDoc();?>" required>
<div class="help-block with-errors"></div>
</div>
</form>
</div>
Good day. I just wanna ask how can I insert the data from the forms generated from a while loop. This is what I tried so far. I added a loop where they will have different id or names but when I tried on clicking the button the first form is the only one working. Thank you so much in advance.
<?php
include "../config/dbconfig.php";
$data['productCode'] = "1"; // sample data
$stmt = $conn->prepare("SELECT * FROM tbl_category");
//$stmt->bind_param("i", $data['productCode']);
$stmt->execute();
$result = $stmt->get_result();
$i = 1;
while ($stuff = $result->fetch_assoc()) {
?>
<div class="col-sm-6" style="margin-top:20px;">
<div class="card">
<div class="card-header"><?php echo $stuff['categoryname']; ?>
</div>
<div class="card-body outermydiv">
<div class="myDIV">
<form method="POST" name="itemform" action="">
<div class="form-row">
<div class="col-5">
<input type="text" class="form-control" name="name[<?php echo $i; ?>]" id="itemname[<?php echo $i; ?>]" placeholder="Item name" required autocomplete="off">
</div>
<div class="col">
<input type="number" class="form-control" name="cost[<?php echo $i; ?>]" id="itemcost[<?php echo $i; ?>]" placeholder="Cost" required>
</div>
<div class="col">
<input type="number" class="form-control" name="price[<?php echo $i; ?>]" id="itemprice[<?php echo $i; ?>]" placeholder="Price" required>
<input type="hidden" class="form-control" name="code[<?php echo $i; ?>]" id="forcatcode[<?php echo $i; ?>]" value="<?php echo $stuff['categorycode'] ?>">
</div>
<div class="col">
<button type="submit" class="btn btn-success" name="btnsaveitem" id="btnsaveitem">Save</button>
</div>
<?php $i++; ?>
<input type="hidden" name="count" value="<?php echo $i; ?>" />
</div>
</form>
</div>
<br>
</div>
</div>
</div>
<?php
}
?>
and here is my insert code
<?php
if (isset($_POST['btnsaveitem'])) {
$count = $_POST['count'];
for ($i = 1; $i < $count; $i++) {
//$code = $_POST['code'][$i]; // check empty and check if interger
$foritemname = $_POST['name'][$i]; // check empty and strip tags
//$qty = $_POST['qty'][$i]; // check empty and check if interger
$stmt = $conn->prepare("INSERT INTO tbl_items(`itemname`) VALUES ('".$foritemname."')");
//$stmt->bind_param("iss",$name);
$stmt->execute();
}
}
?>
Starting by rewriting your form...
You don't need $i for anything, but I'll leave the declaration in case you want it for something else.
Don't submit array type data, each form will submit its own set of fields.
It probably makes more sense to add $stuff['categorycode'] as the value of each submit to avoid needing the hidden field. I'll leave it your way for now.
Form:
foreach ($stmt->get_result() as $i => $stuff) { ?>
<div class="col-sm-6" style="margin-top:20px;">
<div class="card">
<div class="card-header"><?php echo $stuff['categoryname']; ?></div>
<div class="card-body outermydiv">
<div class="myDIV">
<form method="POST">
<div class="form-row">
<div class="col-5">
<input type="text" class="form-control" name="name" placeholder="Item name" required autocomplete="off">
</div>
<div class="col">
<input type="number" class="form-control" name="cost" placeholder="Cost" required>
</div>
<div class="col">
<input type="number" class="form-control" name="price" placeholder="Price" required>
</div>
<div class="col">
<button type="submit" class="btn btn-success" name="btnsaveitem">Save</button>
</div>
</div>
<input type="hidden" class="form-control" name="code" value="<?php echo $stuff['categorycode']; ?>">
</form>
</div>
<br>
</div>
</div>
</div>
<?php
}
Receiving script: (extend with additional fields as desired)
if (isset($_POST['btnsaveitem'])) {
$stmt = $conn->prepare("INSERT INTO tbl_items(`itemname`) VALUES (?)");
$stmt->bind_param("s",$_POST['name']);
$stmt->execute();
}
This is all untested code.
Hi Guys could please check my code I have written, the problem is it won't update the data in DB, it just reloading and not giving any error.
Here is my update.php
if (isset($_POST['update']) && isset($_POST['update']) != "") {
$name = $_POST['c_name'];
$mobile = $_POST['c_mob'];
$dDate = $_POST['d_date'];
$frame = $_POST['frame'];
$size = $_POST['size'];
$lense = $_POST['lense'];
$descr = $_POST['descr'];
$paid = $_POST['paid'];
$remains = $_POST['remains'];
$refer = $_POST['c_refer'];
}
$setquery = "UPDATE `dep_sale` SET c_name='$name', c_mob='$mobile', d_date='$dDate', frame='$frame', size='$size', lense='$lense', descr='$descr',c_refer='$refer', paid='$paid', remains='$remains' WHERE id='".$_POST["id"]."'";
mysqli_query($link, $setquery);
header("location: reports.php");
And here is the HTML
<form method="POST" action="update.php">
<div class="row">
<div class="col-lg-4 col-md-4">
<input type="hidden" name="id" />
<input type="text" class="form-control" value="<?php echo $row['c_name']; ?>" required id="c_name" name="c_name" placeholder="Name..."/>
</div>
<div class="col-lg-4 col-md-4">
<input type="phone" class="form-control" value="<?php echo $row['c_mob']; ?>" required id="c_mob" name="c_mob" placeholder="Mobile..."/>
</div>
<div class="col-lg-4 col-md-4">
<input type="text" class="form-control" value="<?php echo $row['d_date']; ?>" required id="d_date" name="d_date" onfocus="(this.type='date')" placeholder="Delivery Date..."/>
</div>
</div>
<br>
<div class="row">
<div class="col-lg-4 col-md-4">
<input type="text" class="form-control" value="<?php echo $row['frame']; ?>" required id="frame" name="frame" placeholder="Frame..."/>
</div>
<div class="col-lg-4 col-md-4">
<input type="text" class="form-control" value="<?php echo $row['size']; ?>" required id="size" name="size" placeholder="Size..."/>
</div>
<div class="col-lg-4 col-md-4">
<input type="text" class="form-control" value="<?php echo $row['lense']; ?>" required id="lense" name="lense" placeholder="Lense..."/>
</div>
</div>
<br>
</form>
There is no problem in connection because i am fetching and inserting data perfectly.Please help me guys.Thanks
To see the errors set the following at first lines:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
Your if is wrong. Try this:
if (isset($_POST['update']) && $_POST['update'] != "") {
Or more shorter version
if (!empty($_POST['update'])) {
Hope this helped you!
<?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>";
}
}
}
?>
first of all i would like to appolagise on the amount of code i am about to paste, i didn't want to snippet any more incase its a bit that's giving me the errors
i have a table named contacts and want to update the table by a form.
i am not sure if its the form or if its the code as the delete user isn't working
i have just started to learn this (a few days ago)so the code might be messy or not 100% secure as it should this is for a offline database so i would improve it as i learn.
<?php include("header.php");
//include database connection
include 'db_connect.php';
$action = isset( $_POST['action'] ) ? $_POST['action'] : "";
if($action == "update"){
//write query
$query = "update contacts
set
name = '".$mysqli->real_escape_string($_POST['name'])."',
surname = '".$mysqli->real_escape_string($_POST['surname'])."',
email = '".$mysqli->real_escape_string($_POST['email'])."',
pcode = '".$mysqli->real_escape_string($_POST['pcode'])."',
website = '".$mysqli->real_escape_string($_POST['website'])."',
gender = '".$mysqli->real_escape_string($_POST['gender'])."'
mobile = '".$mysqli->real_escape_string($_POST['mobile'])."'
phone = '".$mysqli->real_escape_string($_POST['phone'])."'
county = '".$mysqli->real_escape_string($_POST['county'])."'
town = '".$mysqli->real_escape_string($_POST['town'])."'
address = '".$mysqli->real_escape_string($_POST['address'])."'
notes = '".$mysqli->real_escape_string($_POST['notes'])."'
business = '".$mysqli->real_escape_string($_POST['business'])."'
where id='".$mysqli->real_escape_string($_REQUEST['id'])."'";
if( $mysqli->query($query) ) {
echo "User was updated.";
}else{
echo "Database Error: Unable to update record.";
}
}
if($action=='delete'){ //if the user clicked ok, run our delete query
$query = "DELETE FROM users WHERE id = ".$mysqli->real_escape_string($_GET['id'])."";
if( $mysqli->query($query) ){
echo "User was deleted.";
}else{
echo "Database Error: Unable to delete record.";
}}
$query = "select id, name, pcode, website, email, surname, mobile, phone, business, gender, address, town, county, notes
from contacts
where id='".$mysqli->real_escape_string($_REQUEST['id'])."'
limit 0,1";
$result = $mysqli->query( $query );
$row = $result->fetch_assoc();
$id = $row['id'];
$name = $row['name'];
$surname = $row['surname'];
$pcode = $row['pcode'];
$email = $row['email'];
$business = $row['business'];
$phone = $row['phone'];
$mobile = $row['mobile'];
$gender = $row['gender'];
$address = $row['address'];
$county = $row['county'];
$notes = $row['notes'];
$town = $row['town'];
$website = $row['website']; ?>
<?php echo "<a href='#' onclick='delete_user( {$id} );'>Delete</a>";
?>
<body>
<div class="div-middle-big">
<!--we have our html form here where new user information will be entered-->
<a href='index.php'>Back to index</a>
</td>
</tr>
</table>
</form>
<div id="loader_cont"><img src="img/loaders/page_loader.gif"></div>
<?php include'topnav.php' ?>
<div class="container">
<div class="main_content row-fluid">
<div class="span3">
<?php include'menu.php' ?>
<!--/.well -->
</div>
<!--/span-->
<div class="span9">
<div class="row-fluid">
<div class="span12">
<ul class="breadcrumb br_styled no_space">
<li> Dashboard <span class="divider">/</span> </li>
<li class="active">Profile</li>
</ul>
<div class="widget profile_cont">
<header>
<h3>Profile: <span class="profile_title"><?php echo$name; ?> <?php echo$surname; ?></span></h3>
<ul class="toggle_content">
<li class="arrow">Toggle Content</li>
</ul>
</header>
<section class="group">
<div class="info"> <img src="http://api.thumbalizr.com/?url=http://<?php echo$website; ?>&width=250" alt="Profile picture">
<h4>Profile Picture</h4>
<div class="profile_picture">
<input type="file" />
<!-- <input type="submit" /> -->
visit website
<!-- UPLOAD -->
</div>
<ul>
<li><i class="sweet-user"></i> Profile</li>
<li><i class="sweet-settings"></i> Settings</li>
<li><i class="sweet-mail"></i> Email <?php echo$name; ?></li>
<li><i class="sweet-cog-4"></i> Widgets</li>
<li><i class="sweet-exit"></i> Logout</li>
</ul>
<div class="span3">
<div class="widget">
<header>
<h3>Grid 3</h3>
<ul class="toggle_content" style="display: none;">
<li class="arrow">Toggle Content</li>
</ul>
</header>
<section class="code_align"> <code>class="span3"</code> </section>
</div>
</div>
</div>
<div class="details">
<form action='#' method='post' border='0' class="well form-horizontal">
<fieldset>
<h4 class="group"> <span>Personal details</span> </h4>
<div class="control-group">
<div class="controls"> </div>
</div>
<div class="control-group">
<label class="control-label" for="name">First name</label>
<div class="controls">
<input id="name" type="text" name="name" value="<?php echo$name; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="surname">Last name</label>
<div class="controls">
<input id="surname" type="text" name="surname" value="<?php echo$surname; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="business">Company Name</label>
<div class="controls">
<input id="business" type="text" name="business" value="<?php echo$business; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="phone">Phone number</label>
<div class="controls">
<input id="phone" type="text" name="phone" value="<?php echo$phone; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="mobile">Mobile number</label>
<div class="controls">
<input id="mobile" type="text" name="mobile" value="<?php echo$mobile; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="gender">Sex</label>
<div class="controls">
<select class="gender" style="width:210px;" tabindex="2">
<option value="<?php echo$gender; ?>"><?php echo$gender; ?></option>
<option value="female">Female</option>
<option value="male">Male</option>
</select>
</div>
</div>
<h4>Contact details</h4>
<div class="control-group">
<label class="control-label" for="email">E-mail</label>
<div class="controls">
<input id="email" type="text" name="email" value="<?php echo$email; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="website">Website</label>
<div class="controls">
<input id="website" type="text" name="website" value="<?php echo$website; ?>" data-original-title="Without the http://">
</div>
</div>
<div class="control-group">
<label class="control-label" for="address">Address</label>
<div class="controls">
<textarea id="address" rows="3" name="address" ><?php echo$address; ?></textarea>
</div>
</div>
<div class="control-group">
<label class="control-label" for="skypeid">Town</label>
<div class="controls">
<input id="town" type="text" name="town" value="<?php echo$town; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="county">County</label>
<div class="controls">
<input id="county" type="text" name="county" value="<?php echo$county; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="pcode">Post code</label>
<div class="controls">
<input id="pcode" type="text" name="pcode" value="<?php echo$pcode; ?>">
</div>
</div>
<h4>Notes about <?php echo$name; ?> <?php echo$surname; ?></h4>
<p>
<textarea id="notes" rows="5" name="notes" ><?php echo$notes; ?></textarea>
</p>
<div class="form-actions">
<!-- so that we could identify what record is to be updated -->
<input type='hidden' name='id' value='<?php echo $id ?>' />
<!-- we will set the action to edit -->
<input type='hidden' name='action' value='update' />
<input type='submit' value='Edit' />
</div>
</fieldset>
</form>
The problem with the above code is thats its not updating my database and i am getting
Database Error: Unable to update record
UPDATE
i have gone back to my old files and now this dosent work
ok i gone right back to the basic files i had....
<meta http-equiv="refresh" content="0; url=../contacts.php"> <?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "DELETE FROM contacts
WHERE created='$_GET[id]'";
mysql_select_db('pcrepairs');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not delete data: ' . mysql_error());
}
echo "Deleted data successfully\n";
mysql_close($conn);
?>
i am now getting this error
Could not delete data: Unknown column 'created' in 'where clause'
You seems to be using users table in your delete query.Does the users table exist?,if not please change it to contacts.Please let me know
Thanks
Forgetting PHP for a moment if you were to issue a SQL query, say in the command-line, you would need to use single quotes to signify the search string.
So it would like this:
DELETE FROM users WHERE id = '100';
The above has to remain true when you construct the query via PHP:
$query = "DELETE FROM users WHERE id='".$mysqli->real_escape_string($_GET['id'])."'";
If your code's failing, you really need to get into the mindset of debugging your code. Approach it in smaller chunks and work your way back up. So for instance, you can try executing the above query with a hard-coded id value in the console and confirm it works.
Can you try echo'ing the $query value before running it through mysqli? Get that sql statement and try manually running it through the database. You may also want to double check your data types. You can get an error if you try, for example, setting an NUMBER/INT field with a string value.
You forgot the commas in your SQL UPDATE statement:
$query = "update contacts
set
name = '".$mysqli->real_escape_string($_POST['name'])."',
surname = '".$mysqli->real_escape_string($_POST['surname'])."',
email = '".$mysqli->real_escape_string($_POST['email'])."',
pcode = '".$mysqli->real_escape_string($_POST['pcode'])."',
website = '".$mysqli->real_escape_string($_POST['website'])."',
gender = '".$mysqli->real_escape_string($_POST['gender'])."',
mobile = '".$mysqli->real_escape_string($_POST['mobile'])."',
phone = '".$mysqli->real_escape_string($_POST['phone'])."',
county = '".$mysqli->real_escape_string($_POST['county'])."',
town = '".$mysqli->real_escape_string($_POST['town'])."',
address = '".$mysqli->real_escape_string($_POST['address'])."',
notes = '".$mysqli->real_escape_string($_POST['notes'])."',
business = '".$mysqli->real_escape_string($_POST['business'])."'
where id='".$mysqli->real_escape_string($_REQUEST['id'])."'";
You also need to review your HTML code.
EDIT
The SQL syntax for an update statement is:
UPDATE my_table_name SET col1='value1', col2='value2', ... WHERE conditions
And this should work for the delete query:
$query = "DELETE FROM users WHERE id='".$mysqli->real_escape_string($_GET['id'])."'";
If you are using PHP5+ I recommend you to use PDO instead of the old sqlite functions.
You also need to verify your data before saving into the DB.