azhpo
azhpo

Reputation: 135

How to allow user update data Database SQL on html page

I have a mySQL database and which stores information that the user enters by the next form:

<form action="demo.php" method="POST"/>
<p><label>title:</label> <input type="text" name="titre" required/></p>
<p><label>disp: </label>
    Oui&nbsp;<input type="radio" name="disponible" value="1" required/>&nbsp;&nbsp;
    Non&nbsp;<input type="radio" name="disponible" value="0" required/>
</p>
<p><label>Corrected: </label>
    Oui&nbsp;<input type="radio" name="corrige" value="1" required/>&nbsp;&nbsp;
    Non&nbsp;<input type="radio" name="corrige" value="0"required/></p>
<p><label>Notes A:</label> <input type="text" name="notes" /></p>
<p><label>Notes B </label> <input type="text" name="notes2" /></p>
<p><label>N pages </label> <input type="text" name="pages" /></p>
<p><label>Mont: </label>
    Oui&nbsp;<input type="radio" name="monte" value="1" required/>&nbsp;&nbsp;
    Non&nbsp;<input type="radio" name="monte" value="0" required/></p></p>
<p><label>File:</label> <input type="text" name="image" /></p>

$value = $_POST['titre'] ;
$value2 = $_POST['disponible'] ;
$value3 = $_POST['corrige'] ;
$value4 = $_POST['notes'] ;
$value5 = $_POST['notes2'] ;
$value6 = $_POST['pages'] ;
$value7 = $_POST['monte'] ;
$image = $_POST['image'] ;


$sql = "INSERT INTO mode (Titre, Disponible, Corrige, NotesCorr, NotesRed, NPages, Monte, image) VALUES ('$value', '$value2', '$value3', '$value4', '$value5', '$value6', '$value7', '$image')";

if (!mysql_query($sql)) {
    die('Error: ' . mysql_error());
} else {
    header('Location: index.php');
    echo "done";
}

The information is stored and is displayed in a table. (If code needed I'll post here)

What I don't have any idea how to do is to let the user EDIT data that is already stored in the table.

For example, the user selects the ID and edits whatever he wants.

Any idea how can I do this?

Best regards.

Upvotes: 1

Views: 4304

Answers (2)

sameer kumar
sameer kumar

Reputation: 149

Try this It may Help you

create one more column in your table where the data is displaying in the webpage name it as "Edit"

<tr>Edit</tr>

In the edit column <td> value Use loop Like

while($result = mysql_fetch_array($sql))

here $sqlis to fetch all data from your table

now redirect <a> link to the update page where you need to see the data and update it.

<td><a href = "update.php?id=<?php echo $result[0]?>">Edit</td>

here $result[0] indicate the primary key column of the table which redirect the key to the next page and you have now the primary key to Update your data inside your table.

Upvotes: 1

Logan Wayne
Logan Wayne

Reputation: 5991

Note:

  • Should be using MySQLi, instead of deprecated MySQL.
  • Why use input type="text" on a file? Let me show you how to use input type="file"

Replace your:

<p><label>File:</label> <input type="text" name="image" /></p>

with:

<p><label>File:</label> <input type="file" name="image" accept=".jpg, .png, .jpeg"/></p>

And add enctype='multipart/form-data' to your <form>:

<form action="demo.php" method="POST" enctype="multipart/form-data"/>

PHP Code:

$allowedExts=array("jpg","jpeg","png");
$temp=explode(".",$_FILES["image"]["name"]);
$extension=end($temp);

    /* START OF FILE UPLOAD */

    if(($_FILES["image"]["size"]<8388608) && in_array($extension,$allowedExts)){

        $image=$_FILES['image']['name']; /* STORE THE FILE NAME TO $image VARIABLE */
        $fullpath="ImageDirectory/".$image; /* CHANGE THE NECESSARY FILE DIRECTORY */
        move_uploaded_file ($_FILES['image']['tmp_name'], $fullpath); /* MOVE FILE TO THE GIVEN DIRECTORY */

    }

$value = $_POST['titre'] ;
$value2 = $_POST['disponible'] ;
$value3 = $_POST['corrige'] ;
$value4 = $_POST['notes'] ;
$value5 = $_POST['notes2'] ;
$value6 = $_POST['pages'] ;
$value7 = $_POST['monte'] ;

$stmt = $mysqli->prepare("INSERT INTO mode (Titre, Disponible, Corrige, Notescorr, NotesRed, Npages, Monte, image) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param('ssssssss', $value, $value2, $value3, $value4, $value5, $value6, $value7, $image);

More Note:

  • For editing, you need to provide more information about your database and table.
  • We need to know the structure of your mode table.
  • Your table needs to have a unique ID in order to update the table in ease.

I'll update my answer as soon as you have provided the needed information.

Upvotes: 1

Related Questions