Jump to content

[PHP/MYSQL] Update error


Go to solution Solved by OuisVN,

Recommended Posts

principal php (editar_producto.php)

<?php
	//Incluir Encabezado 
	require_once('layout/header.php');
	$id = $_GET['id'];
	//Incluir Conexión
	require_once('base_datos/conexion.php');

	session_start(); 
	if($_SESSION['tipo_usuario'] == 'admin')
	{
		//QUERY SELECT
		$consulta = mysqli_query($conexion, "SELECT usuarios.correo, pesos.nombres, productos.fecha_registro, productos.cantidad, productos.nombrep, productos.precio, productos.descripcion, productos.id, categorias.nombre FROM categorias INNER JOIN productos ON productos.categoria_id = categorias.id INNER JOIN pesos ON pesos.id = productos.pesos_id INNER JOIN usuarios ON usuarios.id = productos.usuario_id WHERE productos.id = $id");
		$usuario = mysqli_fetch_array($consulta);
		if(mysqli_num_rows($consulta)):
		?>
		<div class="container">
			<h2 class="text-center"><b>EDITAR PRODUCTO (<?php echo $usuario['id']; ?>)</b></h2>
			<?php require_once('layout/insertar_prod.php') ?>
			<form action="editar_producto_post.php" method="POST" role="form">
				<div class="form-group">
					<label for="">Nombre Juego</label>
					<input type="text" class="form-control" name="nombrep" required="" value="<?php echo $usuario['nombrep']; ?>">
				</div>
				<div class="form-group">
					<label for="">Categoria</label>
					<select class="form-control" name="nombre" required="">
							<option value="1" <?php if($usuario['nombre'] =="FPS Games") echo "selected";?>>FPS Games</option>
							<option value="2" <?php if($usuario['nombre'] =="Aventure Games") echo "selected";?>>Aventure Games</option>
						</select>
				</div>
				<div class="form-group">
					<label for="">Descripción</label>
					<input type="text" class="form-control" name="descripcion" required="" value="<?php echo $usuario['descripcion']; ?>">
				</div>
				<div class="form-group">
					<label for="">Precio</label>
			       	<input type="text" class="form-control" name="precio" required="" value="<?php echo $usuario['precio']; ?>">
				</div>
				
				<div class="form-group">
						<label for="">Tipo de Moneda</label>
						<select class="form-control" name="nombres" required="">
							<option value="1" <?php if($usuario['nombres'] =="EUROS") echo "selected";?>>EUROS</option>
							<option value="2" <?php if($usuario['nombres'] =="DOLARES") echo "selected";?>>DOLARES</option>
						</select>
					</div>
				<div class="form-group">
					<label for="">Cantidad</label>
					<input type="text" class="form-control" name="cantidad" required="" value="<?php echo $usuario['cantidad']; ?>">
				</div>
				<div class="modal-footer">
					<button type="submit" class="btn btn-primary">Editar Producto</button>
					<a href="administracion2.php" class="btn btn-success">Volver Atrás</a>
				</div>
			</form>
		</div>
<?php endif; } else { ?>
		<div class="alert alert-danger">
			<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
			<strong>Alerta!</strong> No tienes los suficientes permisos para acceder a esta página...
		</div>
<?php }
	//Incluir Pie de Pagina
	require_once('layout/footer.php');
?>

editar_producto_post.php

<?php
	$nombrep = $_POST['nomprep'];
	$nombre = $_POST['nombre'];
	$descripcion = $_POST['descripcion'];
	$precio = $_POST['precio'];
	$nombres = $_POST['nombres'];
	$cantidad = $_POST['cantidad'];
	$id = $_POST['id'];
	
	session_start();
	$loginid = $_SESSION['id'];
	//Incluir Conexión
	require_once('base_datos/conexion.php');
	$editar = mysqli_query($conexion, "UPDATE productos SET nombrep = '$nombrep', descripcion = '$descripcion', precio = '$precio', categoria_id = '$nombre', usuario_id = '$loginid', cantidad = '$cantidad', pesos_id = '$nombres' WHERE id = '$id'");
	$consulta = mysqli_affected_rows($conexion);
	header("Location: administracion2.php?consulta=".$consulta); ?>

When i tried to use update in the differents fields i got the next message: Alerta! El producto no pudo ser actualizado, intentelo de nuevo. Alert: The product could'n update, try again...

But the update query is correct. :/

Link to comment
https://forums.phpfreaks.com/topic/302356-phpmysql-update-error/
Share on other sites

But the update query is correct.

 

Have you tried using mysqli_error() just to make sure? More information can be found here:

http://php.net/manual/en/mysqli.error.php

 

Side note: In case you are not aware, the query is susceptible to SQL injection attacks. To protect the query, look into using prepare statements or mysqli_real_escape_string().

Have you tried using mysqli_error() just to make sure? More information can be found here:

http://php.net/manual/en/mysqli.error.php

 

Side note: In case you are not aware, the query is susceptible to SQL injection attacks. To protect the query, look into using prepare statements or mysqli_real_escape_string().

Thx but is only to get a grade in my university.

I use mysqli_error in the connection

<?php
	$config['servidor'] = 'xxx:xxx';
	$config['usuario'] = 'xxx';
	$config['contrasena'] = 'xxx';
	$config['base_datos'] = 'xxx';

	$conexion = mysqli_connect($config['servidor'], $config['usuario'], $config['contrasena'], $config['base_datos']);

	if(!$conexion){
		die("Error de Conexion a la base de datos: ". mysqli_connect_error());
	}
?>

your form doesn't have any field named 'id', so, $_POST['id'] doesn't exist. this would be throwing php errors if you had php's error_reporting set to E_ALL and display_errors set to ON (preferably in the php.ini on your development system.) with no id value, the WHERE clause in the query is false, so the affected rows will always be zero.

  • Solution

your form doesn't have any field named 'id', so, $_POST['id'] doesn't exist. this would be throwing php errors if you had php's error_reporting set to E_ALL and display_errors set to ON (preferably in the php.ini on your development system.) with no id value, the WHERE clause in the query is false, so the affected rows will always be zero.

It's not neccesary because id is unsignned but thx, I fix it. The fields are differents in db. Solved.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.