The code below is supposed to allow you to view a list of friends
and edit or delete from the same page. The viewing works, but the deleting and updating does not. Any help would be appreciated.
View code:
$query ="SELECT * FROM tblFriends";
$result = mysqli_query($conn,$query);
while($row =$result->fetch_assoc()){
$fname=$row['fname'];
$lname=$row['lname'];
$address=$row['address'];
$desc=$row['description'];
$zip=$row['zip'];
$city=$row['city'];
$state=$row['state'];
$id =$row['key'];
echo
"<tr>
<td><input type='text' name='fname' value='$fname'/></td>
<td><input type='text' name='lname' value='$lname'/></td>
<td><input type='text' name='address' value='$address'/></td>
<td><input type='text' name='city' value='$city'/></td>
<td><input type='text' name='desc' value='$desc'/></td>
<td><input type='text' name='state' value='$state'/></td>
<td><input type='submit' name='Edit' value='Edit'/></td>
<td><input type='submit' name='Delete' value='Delete'/></td>
<input type='hidden' name='id' value='$id'/>
<input type='hidden' name='zip' value='$zip'/>
</tr>";
}
Update Code:
if(isset($_POST['Edit'])){
$fname1=$_POST['fname'];
$lname1=$_POST['lname'];
$city1=$_POST['city'];
$state1=$_POST['state'];
$zip1=$_POST['zip'];
$desc1=$_POST['desc'];
$address1=$_POST['address'];
$id1=$_POST['id'];
$UpdateQuery ="UPDATE tblfriends SET fname='$fname1', lname='$lname1',city='$city1',address='$address1',zip='$zip1',state='$state1' WHERE id=$id1";
echo $id1;
if( mysqli_query($conn,$UpdateQuery)){
echo "Updated";
}else{
echo "Not Updated";
}
}
Delete Code:
This mysqli_query and the update one both return false
if(isset($_POST['Delete'])){
$id2 =$_POST['id'];
$deleteQuery= "DELETE FROM tblfriends WHERE id=$id2";
if( mysqli_query($conn,$deleteQuery)){
echo "Deleted";
}else{
echo "Not Deleted";
}
Ive looked throug your quesry lol, SPOT the odd one out?
$id =$row['key'];
"UPDATE tblfriends SET fname='$fname1', lname='$lname1',city='$city1',address='$address1',zip='$zip1',state='$state1' WHERE id=$id1
"DELETE FROM tblfriends WHERE id=$id2";
Just to help you out, your id name is key? but your checking if id matched id? Not sure but that might be your problem
So change your query to this
$query ="SELECT * FROM tblFriends";
$result = mysqli_query($conn,$query);
while($row = $result->fetch_assoc()){
$fname=$row['fname'];
$lname=$row['lname'];
$address=$row['address'];
$desc=$row['description'];
$zip=$row['zip'];
$city=$row['city'];
$state=$row['state'];
$id =$row['key'];
/**
**Wrapped your input into a form which will post the request
**/
echo
"
<form action=\"#\" method=\"POST\">
<tr>
<td><input type='text' name='fname' value='$fname'/></td>
<td><input type='text' name='lname' value='$lname'/></td>
<td><input type='text' name='address' value='$address'/></td>
<td><input type='text' name='city' value='$city'/></td>
<td><input type='text' name='desc' value='$desc'/></td>
<td><input type='text' name='state' value='$state'/></td>
<td><input type='submit' name='Edit' value='Edit'/></td>
<td><input type='submit' name='Delete' value='Delete'/></td>
<input type='hidden' name='id' value='$id'/>
<input type='hidden' name='zip' value='$zip'/>
</tr>
<br>
</form>
";
};
if(isset($_POST['Edit'])){
$fname1= $_POST['fname'];
$lname1= $_POST['lname'];
$city1= $_POST['city'];
$state1= $_POST['state'];
$zip1= $_POST['zip'];
$desc1= $_POST['desc'];
$address1= $_POST['address'];
$id1= $_POST['id'];
/**
**Updated your query to match the key rather than ID. `key`
**/
$UpdateQuery ="UPDATE tblfriends SET fname='$fname1', lname='$lname1',city='$city1',address='$address1',zip='$zip1',state='$state1' WHERE `key` = '$id1' ";
echo $id1;
if( mysqli_query($conn,$UpdateQuery)){
echo "Updated";
}else{
echo "Not Updated";
};
};
if(isset($_POST['Delete'])){
$id2 = $_POST['id'];
/**
**Updated your query to match the key rather than ID. `key`
**/
$deleteQuery= "DELETE FROM tblfriends WHERE `key` = '$id2' ";
if( mysqli_query($conn,$deleteQuery)){
echo "Deleted";
}else{
echo "Not Deleted";
}
};
Change this query "DELETE FROM tblfriends WHERE id=$id2" with "DELETE FROM tblfriends WHERE id='$id2'". Better if you check your query by run it in query editor (like phpmyadmin in tab sql).
Try testing like this in your code:
$deleteQuery= "DELETE FROM tblfriends WHERE id=$id2";
die($deleteQuery);
then copy the result that shown in browser, then paste in phpmyadmin. run it there. Hopefully it can help you
Related
I'm new to PHP and I've been trying out PHP for a while. I have been able to retrieve data from my database and viewed it in a form but I cant seem to update it when I try to change any of the values. My code is below. Ive been trying a few things so sorry for the bad code
<form action="user.php" method="POST">
<h4>Edit Account</h4>
<input type="text" name="editstudent_number" placeholder="Enter Student Number">
<input type="submit" name="editaccount" value="Search"><br>
<?php
if (isset($_POST['editaccount'])){
$GLOBALS['editstudent_number'] = $_POST['editstudent_number'];
$editstudent = $GLOBALS['editstudent_number'];
$edit_sql = "SELECT username, student_number, email, password, progress, rank FROM users WHERE student_number = '$editstudent'";
$edit_query = mysqli_query($conn, $edit_sql);
$edit_fetch = mysqli_fetch_assoc($edit_query);
$username = $edit_fetch['username'];
$student_number = $edit_fetch['student_number'];
$email = $edit_fetch['email'];
$password = $edit_fetch['password'];
$progress = $edit_fetch['progress'];
$rank = $edit_fetch['rank'];
echo "<input type='text' name='username' value='$username' /><br>";
echo "<input type='text' name='student_number' value='$student_number' /><br>";
echo "<input type='text' name='email' value='$email' /><br>";
echo "<input type='text' name='password' value='$password' /><br>";
echo "<input type='text' name='progress' value='$progress' /><br>";
echo "<input type='text' name='rank' value='$rank' />";
echo "<input type='submit' name='editaccount' value='Save changes' />";
}
$GLOBALS['username'] = $_POST['username'];
$GLOBALS['student_number'] = $_POST['student_number'];
$GLOBALS['email'] = $_POST['email'];
$GLOBALS['password'] = $_POST['password'];
$GLOBALS['progress'] = $_POST['progress'];
$GLOBALS['rank'] = $_POST['rank'];
$edit_username = $GLOBALS['username'];
$edit_student_number = $GLOBALS['student_number'];
$edit_email = $GLOBALS['email'];
$edit_password = $GLOBALS['password'];
$edit_progress = $GLOBALS['progress'];
$edit_rank = $GLOBALS['rank'];
if (isset($_POST['editaccount'])){
$sql = "UPDATE users SET username='$edit_username', student_number='$edit_student_number', email='$edit_email', password='$edit_password', progress='$edit_progress', rank='$edit_rank' WHERE student_number = '$editstudent'";
$query = mysqli_query($conn, $sql);
}
?>
</form>
Your Search button and Save change button have same name, which might cause the conflict.
Search
<input type="submit" name="editaccount" value="Search"><br>
Save
<input type='submit' name='editaccount' value='Save changes' />
And looks at how you have SAME if (isset($_POST['editaccount'])){ two times in the codes.
Change the names.
And also, use different FORM for the Search and the Update.
This is your codes that I have cleaned up a bit, and hopefully working.
<h4>Edit Account</h4>
<!-- search form -->
<form action="user.php" method="POST">
<input type="text" name="editstudent_number" placeholder="Enter Student Number">
<input type="submit" name="searchstudent" value="Search"><br>
</form>
<!-- update form -->
<form action="user.php" method="POST">
<?php
if (isset($_POST['editaccount']))
{
$edit_username = $_POST['username'];
$edit_student_number = $_POST['student_number'];
$edit_email = $_POST['email'];
$edit_password = $_POST['password'];
$edit_progress =$_POST['progress'];
$edit_rank = $_POST['rank'];
$sql = "UPDATE users SET username='$edit_username', student_number='$edit_student_number', email='$edit_email', password='$edit_password', progress='$edit_progress', rank='$edit_rank' WHERE student_number = '$edit_student_number'";
$query = mysqli_query($conn, $sql);
}
if (isset($_POST['searchstudent']))
{
$editstudent = $_POST['editstudent_number'];
$edit_sql = "SELECT username, student_number, email, password, progress, rank FROM users WHERE student_number = '$editstudent'";
$edit_query = mysqli_query($conn, $edit_sql);
$edit_fetch = mysqli_fetch_assoc($edit_query);
$username = $edit_fetch['username'];
$student_number = $edit_fetch['student_number'];
$email = $edit_fetch['email'];
$password = $edit_fetch['password'];
$progress = $edit_fetch['progress'];
$rank = $edit_fetch['rank'];
echo "<input type='text' name='username' value='$username' /><br>";
echo "<input type='text' name='student_number' value='$student_number' /><br>";
echo "<input type='text' name='email' value='$email' /><br>";
echo "<input type='text' name='password' value='$password' /><br>";
echo "<input type='text' name='progress' value='$progress' /><br>";
echo "<input type='text' name='rank' value='$rank' />";
echo "<input type='submit' name='editaccount' value='Save changes' />";
}
else
{
echo "Please search the student number to update the details.";
}
?>
</form>
Just a note, you SHOULD NOT allow user to UPDATE the PRIMARY KEY of your table. In your case, the Student Number is the primary keys, yet you allow user to update it. This will causes conflicts and errors in the update process.
I have a database that is linked with the php. When I'm writing the first code, my web page gets redirected to the page 'mypage1.php'. But when I'm writing the second code, which is almost(exactly) the same, it doesn't get redirected to the mypage1.php. In the table inside my database, I have Orderid's that I'm using to relate to the php code. Could you tell me why doesn't the second code redirects to the same page? (It stays on the same page). The web page gets redirected to the mypage1.php when I am removing the text areas code from the 2nd code, particularly when I'm removing this code:
<input type='textarea' name='staffname'>
</td><td>
<input type='textarea' name='Time'>
</td><td>
<input type='textarea' name='custdetail'>
</td><td>
<input type='textarea' name='orderId'>
CODE 1 (PHP CODE):
echo "";
echo "<tr>";
echo "<td>idSpatula</td><td>Qauntity</td><td>Staff Name</td><td>Staff Id</td><td>Customer Details</d><td>Order Id</td>";
echo "</tr>";
echo "<tr>";
echo "<form >";
echo "<td>" ."Spatula Id=3"."</td><td>
<input type='textarea' name='3'>
</td><td>
<input type='textarea' name='staffname'>
</td><td>
<input type='textarea' name='Time'>
</td><td>
<input type='textarea' name='custdetail'>
</td><td>
<input type='textarea' name='orderId'>
<input type='submit' id='submit' value='submit'>
</td>
</form>";
echo "</tr>";
echo "</table>";
echo "</br>";
if(isset($_GET['3'])){
// means submit clicked!
$queryy3=0;
$queryy3 = $_GET['3'];
header('Location: mypage1.php');
$sqll3 = "UPDATE Spatula SET QuantityInStock=QuantityInStock-$queryy3 WHERE idSpatula=3 and QuantityInStock>0";
$sqlorder3 = "insert into `Order`(idOrder, RequestedTime, ResponsibleStaffMember, CustomerDetails) values (3,'1987-04-20 11:34:09','Shubhams','tell')";
$result1003 = mysqli_query($con, "SELECT QuantityInStock FROM Spatula where idSpatula=3");
mysqli_query($con, $sqlorder2);
if (mysqli_query($con, $sqll3) & $col1003['QuantityInStock']!=0 ) {
//$sql = "UPDATE Spatula SET QuantityInStock=QuantityInStock-$query WHERE idSpatula= and QuantityInStock>0";
echo "Updated";
}
while($col1003 = mysqli_fetch_array($result1003)) {
echo $col1003['QuantityInStock'];
if ($col1003['QuantityInStock']<=0){
header('Location: mypage2.php');
}
}
}
CODE 2:
echo "<table border='1'>";
echo "<tr>";
echo "<td>idSpatula</td><td>Qauntity</td><td>Staff Name</td><td>Staff Id</td><td>Customer Details</d><td>Order Id</td>";
echo "</tr>";
echo "<tr>";
echo "<form >";
echo "<td>" ."Spatula Id=4"."</td><td>
<input type='textarea' name='4'>
</td><td>
<input type='textarea' name='staffname'>
</td><td>
<input type='textarea' name='Time'>
</td><td>
<input type='textarea' name='custdetail'>
</td><td>
<input type='textarea' name='orderId'>
</td>
<input type='submit' id='submit' value='submit'>
</form>";
echo "</tr>";
echo "</table>";
if(isset($_GET['4'])){
// means submit clicked!
$queryy4=0;
$queryy4 = $_GET['4'];
header('Location: mypage1.php');
$sqll4 = "UPDATE Spatula SET QuantityInStock=QuantityInStock-$queryy4 WHERE idSpatula=4 and QuantityInStock>0";
$sqlorder4 = "insert into `Order`(idOrder, RequestedTime, ResponsibleStaffMember, CustomerDetails) values (5,'1987-04-20 11:34:09','Shusss','tss')";
$result1004 = mysqli_query($con, "SELECT QuantityInStock FROM Spatula where idSpatula=4");
mysqli_query($con, $sqlorder4);
if (mysqli_query($con, $sqll4) & $col1004['QuantityInStock']!=0 ) {
echo "Updated";
}
while($col1004 = mysqli_fetch_array($result1004)) {
echo $col1004['QuantityInStock'];
if ($col1004['QuantityInStock']<=0){
header('Location: mypage2.php');
}
}
}
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']);
i have create two table in my databases, 1.loginacc (stay login information(LoginID , Password and Permission)2.borrow (have book that the login borrowed,userid)
below is the code: and i have get all the user id,beside every user that i get from database loginacc ,it has a button called 'get information'.when i click it,it will have order of" select * from borrow where userid = (the loginid that shown out )
<?php
$con = mysql_connect("127.0.0.1","root","password");
mysql_select_db("babytradeapps");
$sql = "Select LoginID , Password , Permission
from loginacc where Permission = 1 ";
$results = mysql_query($sql,$con);
echo "<tr><th>Admin</th></tr>";
echo "<table border=5 cellpadding=10>";
echo "<tr><th></th><th>ac</th><th>pw</th><th>per</th><th></th></tr>";
while($row = mysql_fetch_array($results)) {
echo "<tr><td><form action='searchtable.php' method='get'><button type='button' name='button2' value='button2'>get information</button></td>
<td><input type=text id='row0' name='row0' value='$row[0]' /></td><td><input type=text id='row1' name='row1' value='$row[1]' /></td>
<td><input type=text id='row2' name='row2' value='$row[2]' /></td><td><input type='submit' name='button' value='change' /></td></tr>";
}
echo "</table>";
Why don't you use anchor (link) to send the loginID rather than using button , it will be simple . As far as i've understood your problem , you can do it as below .
echo "<table border=5 cellpadding=10>";
echo "<tr><th></th><th>ac</th><th>pw</th><th>per</th><th></th></tr>";
while($row = mysql_fetch_array($results)) {
echo "<tr><td>
<a href='searchtable.php?lid=$row[0]'>get information</a></td>
<td><input type=text id='row0' name='row0' value='$row[0]' /></td><td><input type=text id='row1' name='row1' value='$row[1]' /></td>
<td><input type=text id='row2' name='row2' value='$row[2]' /></td><td><input type='submit' name='button' value='change' /></td></tr>";
}
echo "</table>";
Now in the next page i.e. searchtable.php, just get that id using GET method and execute the query . This should be somehow like ,
<?php
$loginId = $_GET['lid'];
$con = mysql_connect("127.0.0.1","root","password");
mysql_select_db("babytradeapps");
$sql = "Select * from borrow where userid = $loginId";
$results = mysql_query($sql,$con);
$final_result = mysql_fetch_array($results);
print_r($final_result);
// Manipulate view with the result
?>
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..