0
Hello,
I have this code
<?php
// Include config file
require_once "../extras/config.php";
// Define variables and initialize with empty values
$grupo = $cliente = "";
$grupo_err = $cliente_err = "";
// Processing form data when form is submitted
if(isset($_POST["id"]) && !empty($_POST["id"])){
// Get hidden input value
$id = $_POST["id"];
// Valida grupo
$input_grupo = trim($_POST["grupo"]);
if(empty($input_grupo)){
$grupo_err = "Por favor introduza um grupo";
} elseif(!ctype_digit($input_grupo)){
$grupo_err = "Por favor introduza um cliente válido";
} else{
$grupo = $input_grupo;
}
// Valida cliente
$input_cliente = trim($_POST["cliente"]);
if(empty($input_cliente)){
$cliente_err = "Por favor introduza um cliente";
} elseif(!filter_var($input_cliente, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
$cliente_err = "Por favor introduza um cliente válido";
} else{
$cliente = $input_cliente;
}
// Check input errors before inserting in database
if(empty($grupo_err) && empty($cliente_err)){
// Prepare an update statement
$sql = "UPDATE clientes SET id_grupo=?, cliente=?, WHERE id=?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ssi", $param_grupo, $param_cliente, $param_id);
// Set parameters
$param_grupo = $grupo;
$param_cliente = $cliente;
$param_id = $id;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Records updated successfully. Redirect to landing page
header("location: clientes.php");
exit();
} else{
echo "Oops! Algo está errado. Por favor tente novamente mais tarde.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
} else{
// Check existence of id parameter before processing further
if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
// Get URL parameter
$id = trim($_GET["id"]);
// Prepare a select statement
$sql = "SELECT * FROM clientes WHERE id_cliente = ?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "i", $param_id);
// Set parameters
$param_id = $id;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
$result = mysqli_stmt_get_result($stmt);
if(mysqli_num_rows($result) == 1){
/* Fetch result row as an associative array. Since the result set
contains only one row, we don't need to use while loop */
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
// Retrieve individual field value
$grupo = $row["id_grupo"];
$cliente = $row["cliente"];
} else{
// URL doesn't contain valid id. Redirect to error page
header("location: erro.php");
exit();
}
} else{
echo "Oops! Algo está errado. Por favor tente novamente mais tarde.";
}
}
// Close statement
mysqli_stmt_close($stmt);
// Close connection
mysqli_close($link);
} else{
// URL doesn't contain id parameter. Redirect to error page
header("location: erro.php");
exit();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Actualizar Registo</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
.wrapper{
width: 500px;
margin: 0 auto;
}
</style>
<link rel="stylesheet" href="../css/style.css">
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h2>Actualizar Registo</h2>
</div>
<p>Por favor actualize os registos e registe para actualizar.</p>
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div class="form-group <?php echo (!empty($grupo_err)) ? 'has-error' : ''; ?>">
<div class="form-group <?php echo (!empty($grupo_err)) ? 'has-error' : ''; ?>">
<label>Grupo</label>
 
<?php
$host="localhost";
$user="root";
$pass="";
$dbname="adminam";
$conn=mysqli_connect($host,$user,$pass,$dbname);
$query="SELECT * FROM grupos";
$result1=mysqli_query($conn,$query);
?>
<select name="grupo" style="width: 100%">
<?php while ($row= mysqli_fetch_array($result1)): ?>
<option name="grupo" value="<?php echo $row[0]; ?>"><?php echo $row[1];?></option>option>
<?php endwhile; ?>
</select>
<span class="help-block"><?php echo $grupo_err;?></span>
</div>
<div class="form-group <?php echo (!empty($cliente_err)) ? 'has-error' : ''; ?>">
<label>Cliente</label>
<input type="text" name="cliente" class="form-control" value="<?php echo $cliente; ?>">
<span class="help-block"><?php echo $cliente_err;?></span>
</div>
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<input type="submit" class="btn btn-primary" value="Actualizar">
<a href="clientes.php" class="btn btn-default">Cancelar</a>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
I keep getting the message
Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, boolean given in C:\wamp64\www\final\ges_clientes\update.php on line 59
What’s wrong? This code worked only yesterday... Thanks for the help
Thanks, the comma really made a difference! As for the remaining notes I will take them into account and make the respective changes, thanks for the tips!
– xela05
Regarding the error, consider all the tips I mentioned in the reply. Do not limit yourself to just this comma. ;)
– LipESprY