PHP buttons to delete a row in MYSQL table output - php

My goal is to display the entire table e.g SELECT * FROM athlete and include button for each row in the table output, that when pressed will remove that row from the database.
I have followed another post and shown below is what I have so far but does not work:
//if(isset($_POST['delete_id']) && (!empty($_POST['delete_id'])))
if(isset($_POST['Delete'])){
$delete_id = mysql_real_escape_string($_POST['delete_id']);
echo("deletebutton" . $delete_id);
//mysql_query("DELETE FROM KeepScores WHERE `id`=".$delete_id);
//header('Location: main.php');
}
else
{
$query = 'SELECT * FROM athlete';
$result = mysql_query($query);
$self = $_SERVER['PHP_SELF'];
$self = htmlentities($_SERVER['PHP_SELF']); // strip tags to improve security
echo("<table>
<tr>
<td> id </td>
<td>last name</td>
<td>first name</td>
<td>delete data</td>
</tr>");
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
echo("<tr>");
echo("<td>" . $row['id'] . "</td>");
echo("<td>" . $row['lastName'] . "</td>");
echo("<td>" . $row['firstName'] . "</td>");
echo("<td> <form action='$self' method='post'>
<input type='hidden' name='delete_id value='$id' />
<input type='submit' value='Delete' />
</form>
</td>
</tr>");
}
mysql_free_result($result);
echo("</table>");
}
However this does not work. Does anyone know why. NOTE: I have left my login credentials out but I am connecting to the database elsewhere in the file.

The most probable cause is that your checking for $_POST['Delete'] having not given any of the submit buttons the attribute 'name' of 'Delete'. However, the line you have commented out above should work?

I think you should look into this areas
//if(isset($_POST['delete_id']) && (!empty($_POST['delete_id'])))
if(isset($_POST['Delete'])){
and
<input type='submit' value='Delete' />
You should include: <input type='submit' name="Delete" value='Delete' />
That should solve the problem.

Related

How to create a textbox that accepts input and displays any valid query that is in my database in php

$query = "SELECT * FROM Employee";
$res = $conn->query($query);
echo("<b>Employee</b><br><br>");
if ($res->num_rows > 0) {
while ($row = $res->fetch_assoc()) {
echo "<tr><td>" . $row['empID']
. "</td><td>" . $row['empFirstName']
. "</td><td>" . $row['empLastName']
. "</td><td>" . $row['empDept']
. "</td><td>" . $row['empPhone']
. "</td><td>" . $row['empEmail']
. "</td><td>" . $row['empAddress']
. "</td></tr>";
}
echo "</table>";
} else {
echo "0 result";
}
/* close connection */
$conn->close();
?>
</body>
</html>
This is currently how each table is setup. As of right now, each table is a separate link on the homepage. At the bottom of the home page, there is a textbox with a submit and clear button. My issue is, how exactly do I go about typing in any query I desire and have it return the results of the query submitted?
Updated 8:17PM:
Textbox portion of homepage.
<b>Ad-hoc Query:</b><br><br>
<form action="querybox.php" method="post">
<table>
<tr>
<td align = right>
<strong>Please enter your query here<br></font></strong>
</td>
<td>
<input type=text size=130 maxlength=280 name="query">
</td>
</tr>
<tr>
<td align = right>
<input type=reset value="Clear">
</td>
<td>
<input type=submit value="Submit">
</td>
</tr>
</table>
</form>

How do I use foreach within a while loop

I have a script that displays all the values in a db table. I'd like to be able to update the price value of each row individually on clicking the update button but as it's in a while loop it's only updating the final value successfully. I realise this probably requires foreach but I can't figure where to code it within the loop to send the data from each row to the processing script on the next page. Can anyone help? Thank you!
Display script
<h2>Boiler Prices</h2>
<?php
$sql = "SELECT * FROM emb_prices_boilers ORDER BY id ASC";
$result = $connection->query($sql);
if ($result->num_rows > 0) {
/* Start Form and Table ---------------------------------------------------------*/
echo "<form action='../admin/actions/set-boiler-price.php' method='post'>";
echo "<table id='boiler-prices'>
<tr>
<th>ID</th>
<th>Type</th>
<th>Boiler</th>
<th>Ref</th>
<th>Price</th>
</tr>";
/* Start Loop and Variables --------------------------------------------*/
while($row = $result->fetch_assoc()) {
$id = $row["id"];
$type = $row["type"];
$manufacturer = $row["manufacturer"];
$model = $row["model"];
$boilerref = $row["boilerref"];
$price = $row["price"];
// output data of each row
echo "
<tr>
<td>" .$id. "</td>
<td>" .$type. "</td>
<td>" .$manufacturer. " " .$model. "</td>
<td>" .$boilerref. "</td>
<td>£<input type='text' name='price' value='" .$price. "' /><input type='hidden' name='boilerref' value='" .$boilerref. "' /><input type='submit' value='update' /></td>
</tr>";
}
/* End Loop -------------------------------------------------------*/
echo "</table>";
echo "</form>";
/* End Table and Form -------------------------------------------------------*/
}
else {
echo "0 results";
}
?>
Processing script
<?php
$boilerref = $_POST['boilerref'];
$price = $_POST['price'];
if ($setboilerprice = mysqli_prepare($connection, "UPDATE emb_prices_boilers SET price = ? WHERE boilerref = ?")
) {
/* bind parameters for markers */
mysqli_stmt_bind_param($setboilerprice, "ds", $price, $boilerref );
/* execute query */
mysqli_stmt_execute($setboilerprice);
/* close statement */
mysqli_stmt_close($setboilerprice);
}
else {
echo "error: Failed to write to db";
}
?>
What you need is to create a separate form for each record. Remove <form>/</form> tags from the beginning and the end of <table> and use this code in your last td:
echo "
...
<td>
<form action='../admin/actions/set-boiler-price.php' method='post'>
£<input type='text' name='price' value='" .$price. "' />
<input type='hidden' name='boilerref' value='" .$boilerref. "' />
<input type='submit' value='update' />
</form>
</td>
</tr>";
With such mark up each form will send it's own data to your set-boiler-price.php and updating will work as you expect.

i want to search filter my database values using checkboxes php mysql

by using this code i can now fetch pro_price table but i need multiple column saerch. this is my working code for an solo column, but i need to fetch two column more from that table. How i can i do that. please help
Database name: auction
Table name : addproduct
Column names : pro_price, pro_code, hsn_code
This is my code,
if(isset($_REQUEST['search'])){
$pro_price = $_REQUEST['pro_price'];
foreach ($_REQUEST['pro_price'] as $pro_price) {
$statearray[] = mysql_real_escape_string($pro_price);
}
$states = implode ("','", $statearray);
$sql = "SELECT * FROM addproduct WHERE pro_price IN ('$states'))";
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result) == 0)
{
echo "Sorry, but we can not find an entry to match your query...<br><br>";
}
else
{
echo "<table border='1' width='900' class='srchrslt'>
<tr class='head'>
<td>pro_name</td>
<td>pro_brand</td>
<td>hsn_code</td>
<td>pro_tax2</td>
<td>pro_tax3</td>
</tr>";
while($row = mysql_fetch_assoc( $result ))
{
echo "<tr>";
echo "<td>" . $row['pro_name'] . " </td>";
echo "<td>" . $row['pro_brand'] . " </td>";
echo "<td>" . $row['hsn_code'] . " </td>";
echo "<td>" . $row['pro_tax2'] . " </td>";
echo "<td>" . $row['pro_tax3'] . " </td>";
echo "</tr>";
}
echo "</table>";
}
}
please help...and thank you
You do a SELECT *, which returns all columns in a table. In your case, it seems there's only a single column, so either you need to add additional columns to the table, or alternatively, you need to figure out whether perhaps you have not been granted permissions to see all columns in the table.
UPDATE:
It seems what you really want to do is add additional search criteria. This would mean your query would become something like the following:
SELECT * FROM addproduct WHERE pro_price IN ('$states') OR pro_code IN ('$codes')
For that to work, you would have to do what you already with the selected prices (i.e. store them i a variable called $states). In the SQL line above, I assume you store the selected values of the codes in a similar variable called $codes.
The full code with this addition would be something like tis:
if(isset($_REQUEST['search'])){
$pro_price = $_REQUEST['pro_price'];
$pro_code = $_REQUEST['pro_code'];
foreach ($_REQUEST['pro_price'] as $pro_price) {
$statearray[] = mysql_real_escape_string($pro_price);
}
foreach ($_REQUEST['pro_code'] as $pro_code) {
$codesarray[] = mysql_real_escape_string($pro_code);
}
$states = implode ("','", $statearray);
$codes = implode ("','", $codesarray);
$sql = "SELECT * FROM addproduct WHERE pro_price IN ('$states') OR pro_code IN ('$codes')";
Note that I'm not a PHP coder, so it is entirely possible the code can be optimized. I just wanted to show you what I think would work.
At last I got the solutions to my question. Thanks to #SchmitzIT who had supported me. Iam posting the code for further candidates.
index.php
<form name="search" method="post" action="searchplant">
<table width="900" border="1" class="srch">
<tr class="head"><td>Pro Price</td></tr>
<tr>
<td>
<input type="checkbox" value="250" name="pro_price[]">250<br />
<input type="checkbox" value="80" name="pro_price[]">80<br />
<input type="checkbox" value="50" name="pro_price[]">50<br />
<input type="checkbox" value="40" name="pro_price[]">40<br />
<input type="checkbox" value="299" name="pro_price[]">299<br />
<td>
</tr>
<tr class="head"><td>hsncode</td></tr>
<tr>
<td>
<input type="checkbox" value="101101" name="hsn_code[]">101101<br />
<input type="checkbox" value="101102" name="hsn_code[]">101102<br />
<input type="checkbox" value="101103" name="hsn_code[]">101103<br />
<input type="checkbox" value="101104" name="hsn_code[]">101104<br />
<input type="checkbox" value="101105" name="hsn_code[]">101105<br />
<td>
</tr>
<tr class="head"><td>procode</td></tr>
<tr>
<td>
<input type="checkbox" value="101" name="pro_code[]">101<br />
<input type="checkbox" value="102" name="pro_code[]">102<br />
<input type="checkbox" value="103" name="pro_code[]">103<br />
<input type="checkbox" value="104" name="pro_code[]">104<br />
<input type="checkbox" value="105" name="pro_code[]">105<br />
<td>
</tr>
<tr><td colspan="3" align="Right">
<input type="submit" name="search" value="Search" /></td></tr>
</table>
</form>
</div><!-- end service-->
<div id="media" class="group">
search.php
if(isset($_REQUEST['search']))
{
$pro_price = $_REQUEST['pro_price'];
$pro_code = $_REQUEST['pro_code'];
$hsn_code = $_REQUEST['hsn_code'];
foreach ($_REQUEST['pro_price'] as $pro_price) {
$statearray[] = mysql_real_escape_string($pro_price);
}
foreach ($_REQUEST['pro_code'] as $pro_code) {
$codesarray[] = mysql_real_escape_string($pro_code);
}
foreach ($_REQUEST['hsn_code'] as $hsn_code) {
$hsnarray[] = mysql_real_escape_string($hsn_code);
}
$states = implode ("','", $statearray);
$codes = implode ("','", $codesarray);
$hsn = implode ("','", $hsnarray);
$sql = "SELECT * FROM addproduct WHERE pro_price IN ('$states') OR pro_code IN ('$codes') OR hsn_code IN ('$hsn')";
//Now we search for our search term, in the field the user specified
$result = mysql_query($sql) or die(mysql_error());
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
if (mysql_num_rows($result) == 0)
{
echo "Sorry, but we can not find an entry to match your query...<br><br>";
}
else
{
echo "<table border='1' width='900' class='srchrslt'>
<tr class='head'>
<td>pro_name</td>
<td>pro_brand</td>
<td>hsn_code</td>
<td>pro_price</td>
<td>pro_code</td>
</tr>";
//And we display the results
while($row = mysql_fetch_assoc( $result ))
{
echo "<tr>";
echo "<td>" . $row['pro_name'] . " </td>";
echo "<td>" . $row['pro_brand'] . " </td>";
echo "<td>" . $row['hsn_code'] . " </td>";
echo "<td>" . $row['pro_price'] . " </td>";
echo "<td>" . $row['pro_code'] . " </td>";
echo "</tr>";
}
echo "</table>";
}
}
Just change the database name, table name, column name and values according to your data.
Hope you find this useful

php html return only one set of data records when clicking the view button

I need my system to show only the set of 'minutes' a user clicks on and not every set of minutes that the current user is involved in.
At the minute when I click the 'image' to view a set of minutes it prints out each set of minutes that user is involved in, I need it to only print out and be able to edit that one specific set of minutes... the edit function works but I just need it to print out what the user clicks on and not every set..PLEASE Can ANYONE help with this I have spent all day and don't understand how to get this to work.
Below is the code that shows the php and query in the minutes.php page, when you click the image for 'view' it takes you to the 'viewstudentminutes.php' page, I only want it to print out the minutes you click on not every set.
<?php
session_start();
if (!(isset($_SESSION["sess_username"]))) header ("Location: index.php");
$currentUser=$_SESSION["sess_username"];
$dbQuery = $db->prepare("select * from minute where '$currentUser'=B_number ");
$dbQuery->execute();
$numMinutes = $dbQuery->rowCount();
echo "<p>There are $numMinutes sets of minutes in the system</p>";
$oddRow=true;
while ($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) {
$ID = $dbRow['ID'];
$B_number = $dbRow['B_number'];
$Date = $dbRow['Date'];
$Time = $dbRow['Time'];
$Discussion = $dbRow['Discussion'];
$Actions = $dbRow['Actions'];
$Dateofnextmeeting = $dbRow['Dateofnextmeeting'];
$Status = $dbRow['Status'];
$E_number = $dbRow['E_number'];
$Supervisor_comments = $dbRow['Supervisor_comments'];
if ($oddRow) $rowClass="odd"; else $rowClass="even";
$oddRow=!$oddRow;
if (isset($_POST['editMinuteID']) && $_POST['editMinuteID']==$ID) {
echo "<tr class='$rowClass '>
<form style='display:inline' method='post' action='minutes.php'>
<input type='hidden' name='editCommitID' value='$ID'>
<td><input type='varchar' name='editB_number' value='$B_number'></td>
<td><input type='date' name='editDate' value='$Date'></td>
<td><input type='time' name='editTime' value='$Time'></td>
<td><input type='text' name='editDiscussion' value='$Discussion'></td>
<td><input type='text' name='editActions' value='$Actions'> </td>
<td><input type='date' name='editDateofnextmeeting' value='$Dateofnextmeeting'></td>
<td><input type='enum' name='editStatus' value='$Status'> </td>
<td><input type='varchar' name='editE_number' value='$E_number'></td>
<td><input type='text' name='editSupervisor_comments' value='$Supervisor_comments'></td>
<td colspan='2'><input type='image' src='edit.png'>
</form>
</tr>";
} else {
echo "<tr class='$rowClass'><td>$B_number</td><td>$Date</td> <td>$Time</td><td>$Discussion</td><td>$Actions</td><td>$Dateofnextmeeting</td> <td>$Status</td><td>$E_number</td><td>$Supervisor_comments</td>
echo "<tr class='$rowClass'><td>$B_number</td><td>$Date</td><td>$Time</td><td>$Discussion</td><td>$Actions</td><td>$Dateofnextmeeting</td><td>$Status</td><td>$E_number</td><td>$Supervisor_comments</td>
<td class='operation'>
<form class='display:inline' method='post' action='viewstudentminutes.php'>
<input type='hidden' name='editMinute_ID' value='$ID'>
<input type='image' src='edit.png' style='padding-top:7px'>
</form></td>
<td class='operation'>
<form class='display:inline' method='post' action='minutes.php'
onsubmit='return confirm(\"Are you sure?\")'>
<input type='hidden' name='deleteMinuteID' value='$ID'>
<input type='image' src='delete.png' style='padding-top:7px'>
</form></td>
<td class='operation'>
<form class='display:inline' method='post' action='viewstudentminutes.php'
onsubmit='viewstudentminutes.php'>
<input type='image' src='view.png' style='padding-top:7px'>
</form></td>
</tr>";
}
}
?>
Below is the code for the 'viewstudentminutes.php' when trying to edit the set of minutes
<h2>Minutes</h2>
<table class="table table-condensed">
<?php
$dbQuery= $db->prepare("SELECT * FROM `Minute` WHERE `B_number` = '" . $currentUser . "' AND `id` = " . $_POST['Minute_ID']);
$dbQuery->execute();
$oddRow=true;
while ($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) {
$ID = $dbRow['ID'];
$B_number = $dbRow['B_number'];
$Date = $dbRow['Date'];
$Time = $dbRow['Time'];
$Discussion = $dbRow['Discussion'];
$Actions = $dbRow['Actions'];
$Dateofnextmeeting = $dbRow['Dateofnextmeeting'];
$Status = $dbRow['Status'];
$E_number = $dbRow['E_number'];
$Supervisor_comments = $dbRow['Supervisor_comments'];
if ($oddRow) $rowClass="odd"; else $rowClass="even";
$oddRow=!$oddRow;
if (isset($_POST['editMinuteID']) && $_POST['editMinuteID']==$id) {
echo "<tr class='$rowClass'>
<form style='display:inline' method='post' action='viewstudentminutes.php'>
<input type='hidden' name='editCommitID' value='$ID'>
<tr><td colspan='2'><input type='image' src='edit.png'> </tr>
<tr><th>BNumber</th><td><input type='varchar' name='editB_number' value='$B_number'></td></tr>
<tr><th>Date</th><td><input type='date' name='editDate' value='$Date'></td></tr>
<tr><th>Time</th><td><input type='time' name='editTime' value='$Time'></td></tr>
<tr><th>Discussion</th><td><input type='text' name='editDiscussion' value='$Discussion'></td></tr>
<tr><th>Actions</th><td><input type='text' name='editActions' value='$Actions'></td></tr>
<tr><th>Date of Next Meeting</th><td><input type='date' name='editDateofnextmeeting' value='$Dateofnextmeeting'></td></tr>
<tr><th>Status</th><td><input type='enum' name='editStatus' value='$Status'></td></tr>
<tr><th>E Number</th><td><input type='varchar' name='editE_number' value='$E_number'></td></tr>
<tr><th>Supervisor Comments</th><td><input type='text' name='editSupervisor_comments' value='$Supervisor_comments'></td></tr>
</form>
</tr>";
} else {
echo "<tr class='$rowClass'>
<td class='operation'>
<form class='display:inline' method='post' action='viewstudentminutes.php'>
<input type='hidden' name='editMinute_ID' value='$ID'>
<input type='image' src='edit.png' style='padding-top:7px'>
</form></td>
<tr><th>BNumber</th><td>$B_number</td></tr>
<tr><th>Date</th><td>$Date</td></tr>
<tr><th>Time</th><td>$Time</td></tr>
<tr><th>Discussion</th><td>$Discussion</td></tr>
<tr><th>Actions</th><td>$Actions</td></tr>
<tr><th>Date of Next Meeting</th><td>$Dateofnextmeeting</td></tr>
<tr><th>Status</th><td>$Status</td></tr>
<tr><th>E Number</th><td>$E_number</td></tr>
<tr><th>Supervisor Comments</th><td>$Supervisor_comments</td></tr>
</tr>";
}
}
?>
</table>
The Minute table within the database holds the following fields...
1 ID Primary int(10) AUTO_INCREMENT
2 B_number varchar(15) latin1_swedish_ci
3 Date date
4 Time time
5 Discussion text latin1_swedish_ci
6 Actions text latin1_swedish_ci
7 Dateofnextmeeting date
8 Status enum('Submitted', 'Approved', 'Rejected', '') latin1_swedish_ci
9 Supervisor_comments text latin1_swedish_ci
10 E_number varchar(15) latin1_swedish_ci
Please help :(
There are a few variables that I'm unsure of what they are - updating your question to include what $currentUser is would help.
That said, the issue may be in your SQL statement:
$db->prepare("select * from minute where '$currentUser'=B_number ");
You are selecting where $currentUser is equal to B_number. But in your database schema, you show that B_number is a column.
I'd try this SQL statement instead:
$db->prepare("SELECT * FROM `Minute` WHERE `B_number` = '" . $currentUser . "' AND `id` = " . $yourIDVariable );
Edit
Your viewstudentminutes.php will iterate over every record returned in your SQL query. You need to uniquely identify your Minute record to show just one on your viewstudentminutes.php page. You access this page via clicking on an <img> tag. But you never send data for which Minute object you want with this form. You could add this via:
<form class='display:inline' method='post' action='viewstudentminutes.php' onsubmit='viewstudentminutes.php'>
<input type='image' src='view.png' style='padding-top:7px'>
<input type='hidden' name='minute_id' value='" . $ID . "'>
</form>
Then access this on your viewstudentminutes.php as
$db->prepare("SELECT * FROM `Minute` WHERE `B_number` = '" . $currentUser . "' AND `id` = " . $_POST['minute_id']);

how to make ascending id in php

this code not asceding id. we have id 1, 2, 3, 15. when adding next record is read id 16. so how to solve this problem ? id must 1,2,3,4,5,etc
<?php
include"class_koneksi.php";
$db=new database();
$db->konek();
if(empty($_GET['aksi'])){
$daftar=$db->tampilData();
echo"<table border=1>
<tr>
<td>Id</td>
<td>Nama</td>
<td>Email</td>
<td>Pesan</td>
<td>Edit</td>
<td>Hapus</td>
</tr>";
foreach($daftar as $data){
echo" <tr>
<td>".$data['id']."</td>
<td>".$data['nama']."</td>
<td>".$data['email']."</td>
<td>".$data['pesan']."</td>
<td><a href='?aksi=edit&id=$data[id]'>edit</a></td>
<td><a href='?aksi=hapus_data&id=$data[id]'>Hapus</a></td>
</tr>";
}
echo"</table> <br> <a href='?aksi=tambah'>TAMBAH</a>";
}elseif(isset($_GET['aksi']) && $_GET['aksi']=='tambah'){
echo"<br>
<form method=POST action='?aksi=tambah_data'>
<table>
<tr><td>Nama</td><td><input type=text name='nama'></td></tr>
<tr><td>Email</td><td><input type=text name='email'></td></tr>
<tr><td>Pesan</td><td><input type=text name='pesan'></td></tr>
<tr><td></td><td><input type=submit value='simpan'></td></tr>
</table>
</form>
";
}elseif(isset($_GET['aksi']) && $_GET['aksi']=='tambah_data'){
$id= isset($_POST['id']);
$nama=$_POST['nama'];
$email=$_POST['email'];
$pesan=$_POST['pesan'];
$db->tambahData($id,$nama,$email,$pesan);
}elseif(isset($_GET['aksi']) && $_GET['aksi']=='edit'){
$id=$_GET['id'];
echo"<br>
<form method=POST action='?aksi=update_data'>
<table>
<tr><td>Id</td><td><input type=text name='id' value='".$db->bacaData('id',$id)."'></td></tr>
<tr><td>Nama</td><td><input type=text name='nama' value='".$db->bacaData('nama',$id)."'></td></tr>
<tr><td>Email</td><td><input type=text name='email' value='".$db->bacaData('email',$id)."'></td></tr>
<tr><td>Pesan</td><td><input type=text name='pesan' value='".$db->bacaData('pesan',$id)."'></td></tr>
<tr><td></td><td><input type=submit value='simpan'></td></tr>
</table>
<input type='hidden' name='id' value='".$db->bacaData('id',$id)."'>
</form>
";
}elseif(isset($_GET['aksi']) && $_GET['aksi']=='update_data'){
$id=$_POST['id'];
$nama=$_POST['nama'];
$email=$_POST['email'];
$pesan=$_POST['pesan'];
$db->updateData($id,$nama,$email,$pesan);
}elseif(isset($_GET['aksi']) && $_GET['aksi']=='hapus_data'){
$id=$_GET['id'];
$db->hapusData($id);
}
?>
can you tell me where this code wrong?
can you help me solve this problem? thanks
Remove the auto increment property of the table which is showing 16.
Before inserting a new row in that table write a select query.
$sql = "select * from `table_name`";
$result = mysql_query($sql);
$id=count($result)+1;
if(count($result)>0){
$temp = 1;
while($row = mysql_fetch_assoc($result)){
if($row['id']!=$temp++){
$id=$temp; break;
}
}
}
$sql = "insert into `table_name` (`id`,`..`,`..`) values('"$id.."','..','..')";
mysql_query($sql);
I hope this will solve your query...
It is showing id as 16 becouse in table properties Id is set as auto increment
First remove auto increment from id in the respective database table you are having this issue. Then Replace this code section with your code.
.
.
.
elseif(isset($_GET['aksi']) && $_GET['aksi']=='tambah'){
$daftar=$db->tampilData();
$id=count($daftar)+1;
if(count($daftar)>0){
$temp = 1;
forech($daftar as $row){
if($row['id']!=$temp++){
$id=$temp; break;
}
}
}
echo"<br>
<form method=POST action='?aksi=tambah_data'>
<table>
<tr><td>Nama</td><td><input type=text name='nama'><input type="hidden" name='id' value="<?php echo $id ?>"></td></tr>
<tr><td>Email</td><td><input type=text name='email'></td></tr>
<tr><td>Pesan</td><td><input type=text name='pesan'></td></tr>
<tr><td></td><td><input type=submit value='simpan'></td></tr>
</table>
</form>
";
}elseif(isset($_GET['aksi']) && $_GET['aksi']=='tambah_data'){
$id= $_POST['id'];
$nama=$_POST['nama'];
$email=$_POST['email'];
$pesan=$_POST['pesan'];
$db->tambahData($id,$nama,$email,$pesan);
}
.
.
.
I hope this solves your requirement..

Categories