Jump to content

how to update mysql in php in table


wesley

Recommended Posts

Hello,

 

I have written php and a database in mysql. The meaning is that you see a table to edit/update the data of the form.

But how can I make this? I have a attach file, that is a example.

 

My code is:

<!DOCTYPE HTML>
<html lang="en">
<head>
	<title></title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<link href="style.css" type="text/css" rel="stylesheet">
	
</head>

<body>
<div id="main_container">

<header></header>

<div id="box2"></div>

<div id="content">
<h1>Vriendenboek</h1>
<?php
// 1. make connection:
require_once ("connect.php");
// 2. Query 
$query = "SELECT * FROM vrienden ORDER BY achternaam";


// 3. Query make
$result = mysqli_query($link, $query)    or    die(mysqli_error());

// 4. Result
echo "<table border='1'>";
echo "<tr>";
      echo "<th>vriendnummer</th>";
      echo "<th>voornaam</th>";
      echo "<th>achternaam</th>";
      echo "<th>adres</th>";
      echo "<th>woonplaats</th>";
      echo "<th>mobiel</th>";
      echo "<th>beschrijving</th>";
    echo "</tr>";

while ($record = mysqli_fetch_assoc($result)) {
		
    $id = $record['id'];
    $voornaam = $record['voornaam'];
    $achternaam = $record['achternaam'];
    $adres = $record['adres'];
    $woonplaats = $record['woonplaats'];
    $mobiel = $record['mobiel'];
    $beschrijving = $record['beschrijving'];
    
    echo "<tr>";
    echo "<td> $id </td> <td>$voornaam</td> <td>$achternaam</td> <td>$adres</td> <td>$woonplaats</td>  <td>$mobiel</td> <td>$beschrijving</td><td><INPUT TYPE='Submit' VALUE='Update the Record' NAME='Submit'></td>";
    echo "</tr>";
}
echo "</table>";

if(isset($_POST['Submit'])){//if the submit button is clicked
	
	$id = $_POST['id'];
	$voor_naam = $_POST['voornaam'];
	$achter_naam = $_POST['achternaam'];
	$adres = $_POST['adres'];
	$woonplaats = $_POST['woonplaats'];
	$mobiel = $_POST['mobiel'];
	$beschrijving = $_POST['beschrijving'];

	
	$query=""UPDATE vrienden SET voornaam = '$voor_naam', achternaam = '$achter_naam', adres = '$adres', woonplaats = '$woonplaats', mobiel = '$mobiel', beschrijving = '$beschrijving' WHERE id = '$id'";
	mysql_query($query) or die("Cannot update");//update or error
	}



//close connection
mysqli_close($link);
?>
	
</body>
</html>

And I have make this, this works: but without table: where can I make the table?

<form method="POST">
		<input type="text" name="id"  placeholder="vriendnummer">
			<br /><br />
		<input type="text" name="voornaam"  placeholder="voornaam">
			<br /><br />
    	<input type="text" name="achternaam"  placeholder="achternaam">
    		<br /><br />
    	<input type="text" name="adres"  placeholder="adres + nummer">
    		<br /><br />
    	<input type="text" name="woonplaats"  placeholder="woonplaats">
    		<br /><br />
    	<input type="text" name="mobiel"  placeholder="mobiel">
    		<br /><br />
    	<textarea  rows="4" cols="50" placeholder="beschrijving" name="beschrijving"></textarea>
    		<br /><br />
    	<input type="submit" name="update" value="update">

</form>

<?php

	//make connection
require_once ("connect.php");


 $id = $_POST['id'];
 $voor_naam = $_POST['voornaam'];
 $achter_naam = $_POST['achternaam'];
 $adres = $_POST['adres'];
 $woonplaats = $_POST['woonplaats'];
 $mobiel = $_POST['mobiel'];
 $beschrijving = $_POST['beschrijving'];
 
 
$sql = "UPDATE vrienden SET voornaam = '$voor_naam', achternaam = '$achter_naam', adres = '$adres', woonplaats = '$woonplaats', mobiel = '$mobiel', beschrijving = '$beschrijving' WHERE id = '$id'";

if (mysqli_query($link, $sql)) {
    echo "Data met succes gewijzigd";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($link);
}

mysqli_close($link);


?>

my question is: how can i make a edit table (like attch file) in php with the code above?

 

thanks in advance.

post-88022-0-16940000-1448629993_thumb.png

Link to comment
Share on other sites

In the first table listing make the id reference a button input with value equalling record id ref.

 

When this is submitted, the id ref will be passed to next page, use this to list single record whose id matches. Also note the Id ref should not be listed for update as it will or should remain the same, you only use id to match record to edit.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...