I'm having a small college project about discussion room service. I'm stuck at updating the database of the rooms.
I already used mysqli_error() function, and that didn't return any error, I wonder why.
Here's my form code:
<?php
//Tahap 1. Buat koneksi Database
$host = "localhost";
$user = "root";
$pass = "";
$name = "pinjamruang";
$koneksi = mysqli_connect($host, $user, $pass, $name);
//Periksa apakah koneksi berhasil
if(mysqli_connect_errno()){
echo "Error: ";
echo mysqli_connect_error();
echo "<br /> Error Code: ";
echo mysqli_connect_errno();
die();
}
$sql = "SELECT * FROM ruangan";
$keranjang = mysqli_query($koneksi, $sql);
$row = mysqli_fetch_assoc($keranjang);
?>
<h1 class="page-header">Edit Karyawan</h1><br>
<form class="form-horizontal" action="process/process-ruangan-edit.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="inputKodeRuangan" class="col-sm-2 control-label">Kode Ruangan</label>
<div class="col-sm-10">
<input type="text" name="kode" class="form-control" id="inputKodeRuangan" value="<?php echo $row['kode'];?>" placeholder="Kode Ruangan">
</div>
</div>
<div class="form-group">
<label for="inputJumlahMeja" class="col-sm-2 control-label">Jumlah Meja</label>
<div class="col-sm-10">
<input type="number" name="meja" class="form-control" id="inputJumlahMeja" value="<?php echo $row['meja'];?>"placeholder="Jumlah Meja">
</div>
</div>
<div class="form-group">
<label for="inputJumlahKursi" class="col-sm-2 control-label">Jumlah Kursi</label>
<div class="col-sm-10">
<input type="number" name="kursi" class="form-control" id="inputJumlahKursi" value="<?php echo $row['kursi'];?>"placeholder="Jumlah Kursi">
</div>
</div>
<div class="form-group">
<label for="inputStatus" class="col-sm-2 control-label">Status</label>
<div class="col-sm-10">
<select name="status" class="form-control" id="inputStatus">
<option value="available">Tersedia</option>
<option value="unavailable">Tidak Tersedia</option>
</select>
</div>
</div>
<div class="form-group">
<label for="inputNote" class="col-sm-2 control-label">Catatan Khusus</label>
<div class="col-sm-10">
<input type="text" name="note" class="form-control" id="inputNote" value="<?php echo $row['note'];?>"placeholder="Catatan Khusus">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="hidden" name="id" value="<?php echo $row2['id']; ?>" />
<button type="submit" class="btn btn-primary">Update</button>
</div>
</div>
</form>
And here's my process code:
<?php
// Tahap 1. Buat koneksi database
$host = "localhost";
$user = "root";
$pass = "";
$name = "pinjamruang";
$koneksi = mysqli_connect($host, $user, $pass, $name);
//Periksa apakah koneksi berhasil
if(mysqli_connect_errno()){
echo "Error: ";
echo mysqli_connect_error();
echo "<br />Error Code: ";
echo mysqli_connect_errno();
die();
}
//Tahap 2. Lakukan Query SQL
// Dapatkan data dari form dan dibersihkan
$kode = mysqli_real_escape_string($koneksi, $_POST['kode']);
$meja = mysqli_real_escape_string($koneksi, $_POST['meja']);
$kursi = mysqli_real_escape_string($koneksi, $_POST['kursi']);
$status = mysqli_real_escape_string($koneksi, $_POST['status']);
$note = mysqli_real_escape_string($koneksi, $_POST['note']);
$sql = "UPDATE ruangan
SET kode = '$kode',
kursi = $kursi,
meja = $meja,
status = '$status',
note = '$note'
WHERE id = $_POST[id]";
mysqli_query($koneksi,$sql);
echo mysqli_error($koneksi);
//header('Location: ../index.php?page=ruangan');
?>
Any help would be much appreciated, I'm still really new at PHP and basically programming so, thanks a lot!
In your form code you are referencing $row2 which hasn't been defined yet.
<input type="hidden" name="id" value="<?php echo $row2['id']; ?>" />
You should change it to
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
Related
This is index.php
<?php
session_start();
include_once('process.php');
if(isset($_POST['submit']))
{
$fullname = isset($_POST['fullname']);
$email = isset($_POST['email']);
$mobile = isset($_POST['mobile']);
$location = isset($_POST['location']);
$program = isset($_POST['program']);
$dateoofbirth = $_POST['day']."-".$_POST['month']."-".$_POST['year'];
}
?>
<?php
$fullname = isset($_POST['fullname']);
$email = isset($_POST['email']);
$mobile = isset($_POST['mobile']);
$location = isset($_POST['location']);
$program = isset($_POST['program']);
$dateofbirth = isset($_POST['day'])."-". isset($_POST['month'])."-".isset($_POST['year']);
?>
<div class="container-fluid">
<div class="row" id="banner">
<div class="col-md-8">
<img src="images/images.png" style="height:500px; width:671px;">
</div>
<div class="col-md-4">
<div class="container">
<form>
<div class="form-row">
<div class="form-group">
<label for="name"></label>
<input type="text" class="form-control" id="fullname" name="fullname" value="<?php echo
isset($_POST['fullname']); ?>" placeholder="ENTER NAME">
</div>
<div class="form-group">
<label for="email"></label>
<input type="email" class="form-control" id="email" name="email" value="<?php echo
isset($_POST['email']); ?>" placeholder="ENTER EMAIL">
</div>
</div>
<div class="form-group">
<label for="mobile"></label>
<input type="text" class="form-control" id="mobile" name="mobile" value="<?php echo
isset($_POST['mobile']); ?>" placeholder="ENTER MOBILE NUMBER">
</div>
<div class="form-group">
<label for="location"></label>
<input type="text" class="form-control" id="location" name="location"
value="<?php echo isset($_POST['location']); ?>" placeholder="ENTER LOCATION">
</div>
<div class="form-group">
<select class="form-control" id="program" name="program" required>
<option value="program"><?php echo isset($_POST['program']); ?></option>
<option value="PG Diploma Programs">PG Diploma Programs</option>
<option value="Diploma Programs">Diploma Programs</option>
<option value="Certificate Programs">Certificate Programs</option>
</select>
</div>
<div class="wrapper">
<!---<input type="submit" id= "submit" name="submit" value="Submit"/>-->
<input type="submit" id="submit" name="submit" class="btn btn-primary" value="SEND ENQUIRY">
</div>
This is process.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
<?php
if(isset($_POST['submit']))
{
$fullname = addslashes($_POST['fullname']);
$email = addslashes($_POST['email']);
$mobile = addslashes($_POST['mobile']);
$location = addslashes($_POST['location']);
$program = addslashes($_POST['program']);
$dobArr = array($_POST['day']."-". $_POST['month']."-".$_POST['year']);
$dateofbirth = implode('-',$doArr);
date_default_timezone_set('Asia/Calcutta');
$today = date('d-m-Y H:i:s');
}
$sql = "INSERT INTO enquiry(id,fullname,email,mobile,location,program,dateofbirth)
VALUES ('', '".isset($_POST["fullname"])."', '".isset($_POST["email"])."','".isset($_POST["mobile"])."','".isset($_POST["location"])."','".isset($_POST["program"])."','".isset($_POST["dateofbirth"])."')";
?>
<?php
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
inserting table data name,email,mobile ,location as value 1 .
it should be some name as richard ,richard#gmail.com,54234523452,and new york as location.
[here is my table ][1]
[1]: https://i.stack.imgur.com/M2xZS.png
what is the issue in the code
i have try everyything from m side
if required more file will help
how is the data is shown as 1 instead it show name and email ,location
i have try everyything from m side
if required more file will help
how is the data is shown as 1 instead it show name and email ,location
i have try everyything from m side
if required more file will help
how is the data is shown as 1 instead it show name and email ,location
i have try everyything from m side
if required more file will help
how is the data is shown as 1 instead it show name and email ,location
if required more file will help
how is the data is shown as 1 instead it show name and email ,location
your insert is wrong,
$sql = "INSERT INTO enquiry(id,fullname,email,mobile,location,program,dateofbirth)
VALUES ('', '".isset($_POST["fullname"])."', '".isset($_POST["email"])."','".isset($_POST["mobile"])."','".isset($_POST["location"])."','".isset($_POST["program"])."','".isset($_POST["dateofbirth"])."')";
it should be
$sql = "INSERT INTO enquiry(id,fullname,email,mobile,location,program,dateofbirth)
VALUES ('', '".$_POST["fullname"]."', '".$_POST["email"])."','".$_POST["mobile"])."','".$_POST["location"])."','".$_POST["program"])."','".$_POST["dateofbirth"]."')";
isset — Determine if a variable is set and is not NULL http://php.net/manual/en/function.isset.php
UPDATE:
No need to pass id if its Auto increment https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html
And you use $fullname $email $mobile $location $program and dateofbirth directly in INSERT, no need to call $_POST variables again
The form is meant to capture a new user and store user data in the database, except that it does not store any data though the form still returns a successful message.
Form page:
<?php
$servername = "*****";
$username = "*****";
$password = "*****";
$database = "*****";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
<div class="row">
<div class="col-xs-12">
<section class="panel">
<header class="panel-heading">
<h2 class="panel-title">Laai Nuwe Lid</h2>
</header>
<div class="panel-body">
<form class="form-horizontal form-bordered" action=""
method="post">
<p><strong>ID:</strong> Nuwe lid</p>
<div class="form-group">
<label class="col-md-3 control-label"
for="FirstName">Naam:</label>
<div class="col-md-6">
<input type="text" class="form-control"
name="FirstName" id="FirstName" value="<?php echo $firstname; ?>">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"
for="LastName">Van:</label>
<div class="col-md-6">
<input type="text" class="form-control"
name="LastName" id="LastName" value="<?php echo $lastname; ?>"'>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"
for="Cell">Selfoon:</label>
<div class="col-md-6">
<input type="text" class="form-control"
name="Cell" id="Cell" value="<?php echo $cell; ?>">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"
for="Address">Addres:</label>
<div class="col-md-6">
<input type="text" class="form-control"
name="Address" id="Address" value="<?php echo $adress; ?>">
</div>
</div>
<div class="row">
<div class="col-sm-9 col-sm-offset-3">
<button value="submit" type="submit"
name="submit" class="btn btn-primary">Stoor nuwe lid</button>
<button type="reset" class="btn btn-
default">Kanselleer</button>
</div>
</div>
</form>
</div>
</section>
</div>
</div>
<?php
// check if the form has been submitted. If it has, start to process the
form and save it to the database
if (isset($_POST['submit'])) {
// get form data, making sure it is valid
$firstname =
mysql_real_escape_string(htmlspecialchars($_POST['firstname']));
$lastname =
mysql_real_escape_string(htmlspecialchars($_POST['lastname']));
$cell = mysql_real_escape_string(htmlspecialchars($_POST['cell']));
$address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
$sql = "INSERT INTO `tblusers` (FirstName, LastName, Cell, Address) VALUES
('$firstname','$lastname', '$cell','$address')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// once saved, redirect back to the view page
header("Location: index.php");
}
?>
I am not sure if the problem is with PHP or the SQL code as I get no error messages.
The database connects fine. The query works in mysql directly, but when I combine the PHP with the HTML form it stores blank rows.
The code does not work because when you tried to comment out the validation part 'if condition' you forgot to comment out its 'else condition'.
I am talking about this line:
//if ($firstname == '' || $lastname == '' || $cell == '' || $address == '') {
The reason it was not working is because the variables were not the same, PHP is case sensitive. E.G lastname while HTML was LastName.
$firstname =
mysql_real_escape_string(htmlspecialchars($_REQUEST['FirstNameirstname']));
$lastname = mysql_real_escape_string(htmlspecialchars($_REQUEST['LastName']));
$cell = mysql_real_escape_string(htmlspecialchars($_REQUEST['Cell']));
$address = mysql_real_escape_string(htmlspecialchars($_REQUEST['Address']));
Kindly use the following function :
$con->mysqli_real_escape_string ( $_POST['...'])
in place of
mysql_real_escape_string(htmlspecialchars($_POST['...']))
<?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 5 years ago.
The connection is correct fields are set correctly but can't locate the problem from the code.After an hour ago it was work fine but after putting more fields it goes stuck.Form submitting correctly but the fields in xampp have nothing.Please help me to submit data.See the variables below, in fact, all the code below.
<?php
$product = "";
$cost = "";
$other = "";
$otherOne = "";
$code = "";
$price = "";
$qty = "";
$kg = "";
$value = "";
$id = 0;
Here is my config.php file code and its connect properly with my database as it cant show any die() error.
<?php
//Database Connection
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'waqar';
$connection = mysqli_connect($host, $user,$pass,$db) or die("<h2>Database Connection Failed. Contact Your Administrator</h2>");
?>
//When Form Submits
if(isset($_POST['submit'])) {
$product = $_POST['product'];
$cost = $_POST['cost'];
$other = $_POST['other'];
$otherOne = $_POST['otherOne'];
$code = $_POST['code'];
$price = $_POST['price'];
$qty = $_POST['qty'];
$kg = $_POST['kg'];
$value = $_POST['value'];
//Query
$query = "INSERT INTO managment(product, cost, other, otherOne, code, price, value) VALUES ('$product','$cost','$other', '$otherOne', '$code', '$price', '$qty', '$kg', 'value')";
mysqli_query($connection, $query);
header("location: home.php");
}
?>
<?php include("header.php"); ?>
<div class="content">
<br/>
<div class="row">
<div class="col-lg-10 col-md-10 col-sm-2"></div>
<div class="col-lg-10 col-md-10 col-sm-8">
<div class="login-form">
<form method="post">
<div class="row">
<div class="col-lg-6">
<input class="form-control" type="text" name="product" placeholder="Product..."><br>
</div>
<div class="col-lg-6">
<input class="form-control" type="text" name="code" placeholder="Code..."><br>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<input class="form-control" type="text" name="cost" placeholder="Cost..."><br>
</div>
<div class="col-lg-6">
<input class="form-control" type="text" name="price" placeholder="Price..."><br>
</div>
</div>
<input class="form-control" type="text" name="other" placeholder="Other..."><br>
<input class="form-control" type="text" name="otherOne" placeholder="Other..."><br>
<label class="radio-inline">
<input type="radio" name="qty" id="qty" value="qty"> Quantity
</label>
<label class="radio-inline">
<input type="radio" name="kg" id="kg" value="kg"> Kilograms
</label>
<br><br>
<input class="form-control" type="number" step="1.00" name="value" placeholder="Quantity / Kilograms"><br>
<input class="btn btn-primary pull-right" type="submit" name="submit" value="Add Record">
</form>
<br/>
<br/>
</div>
</div>
<div class="col-lg-10 col-md-10 col-sm-2"></div>
</div>
</div>
<?php include("footer.php"); ?>
You are missing the $ from value
$query = "INSERT INTO managment(product, cost, other, otherOne, code, price, value) VALUES ('$product','$cost','$other', '$otherOne', '$code', '$price', '$qty', '$kg', '$value')";
My problem is :When I press the button Delete Or Update ,they delete the information of the text boxes, and do nothing else,they do not show any error.I am new to programming, if someone can help me, I would be grateful.
P.S: Sorry for my bad English.
<?php
$servername = "localhost";
$username = "estgv15592";
$password = "estgv155922016";
$dbname = "estgv15592";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST["bt_registaraparelho"]))
{
$id=$_POST["id"];
$nome=$_POST["nome"];
$anomalias=$_POST["anomalias"];
$datarecepcao=$_POST["datarecepcao"];
$datareparacao=$_POST["datareparacao"];
$dataentrega=$_POST["dataentrega"];
$preco=$_POST["preco"];
$estado=$_POST["estado"];
$sql = ("UPDATE FICHA_DE_OBRA SET NOME = '$nome',ANOMALIAS = '$anomalias',CUSTO_ESTIMADO= '$preco',DATA_RECECAO= '$datarecepcao'
,DATA_REPARACA= '$datareparacao',DATA_ENTREGA= '$dataentrega' ,ESTADO= '$estado' WHERE ID_FICHAOBRA = $id");
mysqli_select_db('estgv15592');
$retval = mysqli_query( $conn, $sql);
if(! $retval ) {
die('Could not update data: ' . mysqli_error());
}
echo "Alterado com Sucesso\n";
mysqli_close($conn);
}
/////////////////////////////////////////////////////////////////
if(isset($_POST["btn_eliminar"]))
{
$id=$_POST["id"];
$sql = "DELETE FROM FICHA_DE_OBRA WHERE id='$id' ";
$result = mysqli_query($conn, $sql) or die(mysqli_error());
if(mysqli_affected_rows($result) > 0) echo 'Selected data rows Deleted';
mysqli_select_db('estgv15592');
$retval = mysqli_query( $conn, $sql);
if(! $retval ) {
die('Could not delete data: ' . mysqli_error());
}
echo "Deleted data successfully\n";
$result = mysqli_query($conn, $sql);
}
////////////////////////////////////////////////////////////////
if(isset($_POST["loadbtn"]))
{
$id = (integer) $_POST["id"];
$query = "SELECT NOME, TELEMOVEL,ANOMALIAS,CUSTO_ESTIMADO,DATA_RECECAO, DATA_REPARACA,DATA_ENTREGA, ESTADO,
PRECO FROM FICHA_DE_OBRA WHERE ID_FICHAOBRA = '$id' ";
$result = mysqli_query($conn, $query);
$details = mysqli_fetch_array($result);
$nome = $details["NOME"];
$telemovel = $details["TELEMOVEL"];
$anomalias = $details["ANOMALIAS"];
$custoestimado = $details["CUSTO_ESTIMADO"];
$datarececao = $details["DATA_RECECAO"];
$datareparacao = $details["DATA_REPARACA"];
$dataentrega = $details["DATA_ENTREGA"];
$estado = $details["ESTADO"];
$preco = $details["PRECO"];
}
$sql = "SELECT * FROM FICHA_DE_OBRA";
$result = mysqli_query($conn, $sql);
echo '<div class="absolute">';
echo '<form id="form" method="post">';
echo "<select name ='id'>";
echo "<option value=''>Selecione Número ficha Obra</option>";
echo '</div>';
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['ID_FICHAOBRA'] . "'>" . $row['ID_FICHAOBRA'] . "</option>";
}
echo "</select>";
$conn->close();
?>
<input type="submit" value="Load" name="loadbtn">
</div>
<br/>
</form>
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<hr>
<div class="jumbotron">
<form method="post" autocomplete="disable" enctype="multipart/form-data">
<div class="input-group">
<span class="input-group-addon">ID:</span>
<input type="text" class="form-control" placeholder="" name="id" value="<?php echo $id;?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Nome:</span>
<input type="text" class="form-control" placeholder="" name="nome" value="<?php echo $nome;?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Telemovel:</span>
<input type="text" class="form-control" placeholder="" name="telemovel" value="<?php echo $telemovel?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Anomalias:</span>
<input type="text" class="form-control" placeholder="" name="anomalias" value="<?php echo $anomalias;?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Data Recepção:</span>
<input type="date" class="form-control" placeholder="" name="datarecepcao" value="<?php echo $datarececao?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Data Reparação:</span>
<input type="date" class="form-control" placeholder="" name="datareparacao" value="<?php echo $datareparacao;?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Data Entrega:</span>
<input type="date" class="form-control" placeholder="" name="dataentrega" value="<?php echo $dataentrega;?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Custo Estimado:</span>
<input type="number" step="0.01" class="form-control" placeholder="" name="precoestimado" value="<?php echo $custoestimado;?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Estado:</span>
<input type="text" class="form-control" placeholder="" name="estado" value="<?php echo $estado;?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Preço:</span>
<input type="number" class="form-control" placeholder="" name="preco" value="<?php echo $preco;?>" />
</div>
<br>
<p>
<input align="left" type="submit" class="btn btn-primary btn-lg" name="bt_alterar" value="Update">
<input align="right" type="submit" class="btn btn-primary btn-lg" name="bt_eliminar" value="Delete">
</p>
<p
</p>
</div>
</div>
You have mixed up everything like mysql, mysqli, Multiple connections. Please use either mysql or mysqli for whole script. There is no need to select db so many time.
Your code need some modification like
$retval = mysqli_query( $sql, $conn ); Wrong
$retval = mysqli_query( $conn, $sql ); Right
Here is modified version of your code:
<?php
$servername = "localhost";
$username = "estgv15592";
$password = "estgv155922016";
$dbname = "estgv15592";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST["bt_registaraparelho"]))
{
$id=$_POST["id"];
$nome=$_POST["nome"];
$anomalias=$_POST["anomalias"];
$datarecepcao=$_POST["datarecepcao"];
$datareparacao=$_POST["datareparacao"];
$dataentrega=$_POST["dataentrega"];
$preco=$_POST["preco"];
$estado=$_POST["estado"];
$sql = ("UPDATE FICHA_DE_OBRA SET NOME = '$nome',ANOMALIAS = '$anomalias', CUSTO_ESTIMADO= '$preco', DATA_RECECAO= '$datarecepcao' , DATA_REPARACA= '$datareparacao', DATA_ENTREGA= '$dataentrega' , ESTADO= '$estado' WHERE ID_FICHAOBRA = $id");
/**Not needed. You have already have a active connection */
//mysql_select_db('estgv15592');
$retval = $conn->query($sql);
/**An integer greater than zero indicates the number of rows affected or retrieved. Zero indicates that no records were updated for an UPDATE statement, no rows matched the WHERE clause in the query or that no query has yet been executed. -1 indicates that the query returned an error.*/
if(!$retval && $conn->affected_rows != -1 ) {
die('Could not update data: ' . $mysqli->error);
}
echo "Alterado com Sucesso\n";
/**Not needed. Close the connection at the end*/
mysql_close($conn);
}
/////////////////////////////////////////////////////////////////
if(isset($_POST["btn_eliminar"]))
{
$id=$_POST["id"];
$sql = "DELETE FROM FICHA_DE_OBRA WHERE id='$id' ";
/*$result = $conn->query($sql) or die($mysqli->error);
if(mysql_affected_rows($result) > 0) echo 'Selected data rows Deleted';
*/
/**Not needed. You have already have a active connection */
//mysql_select_db('estgv15592');
$retval = $conn->query($sql);
if(!$retval && $conn->affected_rows != -1 ) {
die('Could not delete data: ' .$mysqli->error);
}
echo "Deleted data successfully\n";
//$result = mysqli_query($conn, $query);
}
////////////////////////////////////////////////////////////////
if(isset($_POST["loadbtn"]))
{
$id = (integer) $_POST["id"];
$query = "SELECT NOME, TELEMOVEL,ANOMALIAS,CUSTO_ESTIMADO,DATA_RECECAO, DATA_REPARACA,DATA_ENTREGA, ESTADO,
PRECO FROM FICHA_DE_OBRA WHERE ID_FICHAOBRA = '$id' ";
$result = $conn->query($conn, $query);
$details = $result->fetch_array(MYSQLI_ASSOC);
$nome = $details["NOME"];
$telemovel = $details["TELEMOVEL"];
$anomalias = $details["ANOMALIAS"];
$custoestimado = $details["CUSTO_ESTIMADO"];
$datarececao = $details["DATA_RECECAO"];
$datareparacao = $details["DATA_REPARACA"];
$dataentrega = $details["DATA_ENTREGA"];
$estado = $details["ESTADO"];
$preco = $details["PRECO"];
/* free result set */
$result->free();
}
$sql = "SELECT * FROM FICHA_DE_OBRA";
$result = $conn->query($conn, $sql);
echo '<div class="absolute">';
echo '<form id="form" method="post">';
echo "<select name ='id'>";
echo "<option value=''>Selecione Número ficha Obra</option>";
echo '</div>';
while($row = $result->fetch_array(MYSQLI_ASSOC))
{
echo "<option value='" . $row['ID_FICHAOBRA'] . "'>" . $row['ID_FICHAOBRA'] . "</option>";
}
echo "</select>";
/* free result set */
$result->free();
$conn->close();
?>
<input type="submit" value="Load" name="loadbtn">
</div>
<br/>
</form>
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<hr>
<div class="jumbotron">
<form method="post" autocomplete="disable" enctype="multipart/form-data">
<div class="input-group"> <span class="input-group-addon">ID:</span>
<input type="text" class="form-control" placeholder="" name="id" value="<?php echo $id;?>" />
</div>
<div class="input-group"> <span class="input-group-addon">Nome:</span>
<input type="text" class="form-control" placeholder="" name="nome" value="<?php echo $nome;?>" />
</div>
<div class="input-group"> <span class="input-group-addon">Telemovel:</span>
<input type="text" class="form-control" placeholder="" name="telemovel" value="<?php echo $telemovel?>" />
</div>
<div class="input-group"> <span class="input-group-addon">Anomalias:</span>
<input type="text" class="form-control" placeholder="" name="anomalias" value="<?php echo $anomalias;?>" />
</div>
<div class="input-group"> <span class="input-group-addon">Data Recepção:</span>
<input type="date" class="form-control" placeholder="" name="datarecepcao" value="<?php echo $datarececao?>" />
</div>
<div class="input-group"> <span class="input-group-addon">Data Reparação:</span>
<input type="date" class="form-control" placeholder="" name="datareparacao" value="<?php echo $datareparacao;?>" />
</div>
<div class="input-group"> <span class="input-group-addon">Data Entrega:</span>
<input type="date" class="form-control" placeholder="" name="dataentrega" value="<?php echo $dataentrega;?>" />
</div>
<div class="input-group"> <span class="input-group-addon">Custo Estimado:</span>
<input type="number" step="0.01" class="form-control" placeholder="" name="precoestimado" value="<?php echo $custoestimado;?>" />
</div>
<div class="input-group"> <span class="input-group-addon">Estado:</span>
<input type="text" class="form-control" placeholder="" name="estado" value="<?php echo $estado;?>" />
</div>
<div class="input-group"> <span class="input-group-addon">Preço:</span>
<input type="number" class="form-control" placeholder="" name="preco" value="<?php echo $preco;?>" />
</div>
<br>
<p>
<input align="left" type="submit" class="btn btn-primary btn-lg" name="bt_alterar" value="Update">
<input align="right" type="submit" class="btn btn-primary btn-lg" name="bt_eliminar" value="Delete">
</p>
<p
</p>
</div>
</div>