Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a table with a list of people, and I want to have a option where you can edit the information shown of those people listed. I have a editperson.php file and a editsql.php file. I get these errors when trying to submit new values for the persons:

Click here -> https://gyazo.com/5e5e52c26b1b978d7802f58eaafba16d[^]

Here is the code for editperson.php


HTML
<!doctype html>
<html>
<body>
	Skriv inn nye verdier for denne personen
	<form action = 'editsql.php' method = 'POST'>
	<?php
		include('oppkobling.php');

		$id = $_POST['id'];

		// Lag SQL-setning
		$query = 'SELECT * FROM rikestemennesker WHERE id = '.$id;

		// Kjør spørringen
		$resultat = mysqli_query($db, $query);
		$rad = mysqli_fetch_array($resultat);

		//echo "<input type = 'hidden' name = 'editmnr' value = ".$id.">";
		echo "Plass: <input type = 'text' value = '".$rad['id']."' size='30' 
         name='editplass'><br>";
		echo "Navn: <input type = 'text' value = '".$rad['Navn']."' size='30' 
         name='editnavn'><br>";
		echo "Land: <input type = 'text' value = '".$rad['Land']."' size='30' 
         name='editland'><br>";
		echo "Nettoformue: <input type = 'text' value = 
         '".$rad['Nettoformue']."' size='50' name='editformue'><br>";
		echo "Kilde: <input type='text' value = '".$rad['Kilde']."' size ='50' 
         name ='editkilde'><br>";
		// Koble ned
		mysqli_close($db);
		mysqli_free_result($resultat);


	?>
		<input type = 'submit' value = 'Oppdater'>
	</form>
</body>
</html>



and here is the code for editsql.php

PHP
<?php
	include('oppkobling.php');
	$n = $_POST['editnavn'];
	$a = $_POST['editformue'];
  $l = $_POST['editland'];
	$m = $_POST['editplass'];
	$k = $_POST['editkilde'];

	$query = "UPDATE rikestemennesker SET
	navn = '$n',
	land = '$l',
	formue = '$a',
	kilde = '$k',
	WHERE plass = $m";

	mysqli_query($db, $query);

	mysqli_close($db);
	header("Location: index.php");
?>


What I have tried:

I have tried to look over all variables, and checked that the code is linked to the database
Posted
Updated 21-Jan-18 15:34pm
v3

PHP
$query = 'SELECT * FROM rikestemennesker WHERE id = '.$id;
$query = "UPDATE rikestemennesker SET
navn = '$n', land = '$l', formue = '$a', kilde = '$k',
WHERE plass = $m";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
There is a comma before WHERE clause in your update query.
since you are using mysqli, then use mysqli class[^] instead

and rewrite your select query as
PHP
$query = 'SELECT * FROM rikestemennesker WHERE id = ?';
$stmt = $mysqli->prepare("id", $id);
$mysqli->execute($db, $query);
$row = $result->get_result()->fetch_assoc();// add some verification


Do similar with update command too.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900