I Want to move data from one table to another - php

PHP Code in codecancel.php:
<?php
if(isset($_POST['codecancel_btn']))
{
$con = new mysqli('localhost', 'root', '', 'halcondentalclinic');
if($con->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$id=$_POST['codecancel_id'];
$sql = "INSERT INTO cancelled_table SELECT * FROM bookings WHERE booking_id='$id';";
if ($con->query($sql) === TRUE) {
$query = "DELETE FROM bookings WHERE booking_id='$id' ";
if ($con->query($query) === TRUE) {
$message = "Success!";
} else {
echo "Error: " . $query . "<br>" . $con->error;
}
} else {
echo "Error: " . $sql . "<br>" . $con->error;
}
}
?>
For my Table appointment_calendar.php
<?php
if(mysqli_num_rows($query_run)>0)
{
while($row= mysqli_fetch_assoc($query_run))
{
?>
<tr>
<td><?php echo $row['booking_id']; ?> </td>
<td> <?php echo $row['name']; ?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['timeslot'];?> </td>
<td><?php echo $row['date'];?> </td>
<td>
<form action="codecancel.php" method="post">
<input type="hidden" name="codecancel_id" value="<?php echo $row['booking_id']; ?> ">
<button type="submit" name="codecancel_btn" class="btn btn-danger"> CANCEL</button>
</form>
</td>
</tr>
<?php
}
}
else {
echo" No Appointment Found";
}
?>
I want to transfer/move data if the patient cancels his appointment and the cancel appointment move to cancelled_table in the database then the canceled appointment will be removed to the bookings table in the database this is my table I want to cancel.
if I clicked the cancel button the error is :
Sorry guys I am Newbie in PHP AND MYSQL :)

INSERT INTO cancel_table(Booking_ID, Name ,Email,Date)
SELECT id as Booking_id, Name ,Email,Date
FROM bookings
WHERE id = 1
try This And Take care To standard of col Name in DB

Related

Trying to create delete button to delete records in database

I am making a school project.
The meaning of this project is that i create a login/registration form,
where i can register, login, view the record.
But i also need to make a dropdown menu were i can see all the records (users) and below it i need to make a button and when i select a record/user and press the delete button it need to be deleted from the database. and i have no clue how i can do this.
here is my code:
<?php
include ("connectie.php");
include ("deletecode.php");
$conn = new mysqli("localhost", "student14_admin", "lol12345","student14_jordi");
mysqli_select_db($conn,'student14_jordi');
$sql = "DELETE FROM users WHERE ID='$_GET[id]'";
echo "<select name='user'>";
if ($result->num_rows>0) {
while($row = $result->fetch_assoc()) {
echo "<option value=" . $row['username'] . ">" . $row['username'] . "</option>";
}
}
echo "</select>";
?>
<form action="">
<a class="delete" href="delete.php?id=<?php echo $row['id']; ?>">Delete</a>
</form>
Create a button and name it Delete
<a class="delete" href="delete.php?id=<?php echo $row['id']; ?>">Delete</a>
Then make a delete.php file and have these code there.
<?php include_once 'db_config_filename.php';
// get id value
$id = $_GET['id'];
// sql to delete a record
$sql = "DELETE FROM tablename WHERE id='$id'";
// print_r($sql);
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
}
else {
echo "Error deleting record: " . mysqli_error($conn);
}
//redirect here
include 'data.php';
?>
I almost have it working. i have now this code:
delete.php:
<?php
include ("connectie.php");
$sql = "SELECT * FROM users ";
$result = $conn->query($sql);
echo "<select name='user'>";
if ($result->num_rows>0) {
while($row = $result->fetch_assoc()) {
echo "<option value=" . $row['username'] . ">" . $row['username'] . "
</option>";
}
}
echo "</select>";
?>
<form action="" method="GET">
<input type="hidden" name="id" value=".$row['id']." />
<input type="submit" name="delete" value="verwijderen">
</form>
<?php
include ("deletecode.php");
?>
Deletecode.php:
<?php
$servername = "localhost";
$username = "student14_admin";
$password = "lol12345";
$dbname = "student14_jordi";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// sql to delete a record
$sql = "DELETE FROM users WHERE id=3";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
well this works for only record 3 afcourse. but now i need to have it that if i select a record from the dropdown menu and click on the delete button it needs to delete that record from my database.

How to get different buttons to submit different mySQL queries

I'm trying to create a button in a HTML form that adds a number to the corresponding mySQL location. Basically, a voting option for a post. Right now, the buttons all do the same thing: they submit the original post query from the bottom. How do I make them act individually?
PHP:
if (!$thepipeline) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
if (!empty ($_POST)) {
$squery = "INSERT INTO votes (votes) VALUE (1)";
print $squery;
$votes_result = mysqli_query ($thepipeline, $squery);
}
$query = "SELECT p.post_id, p.member_id, p.content, p.date_created,
SUM(v.votes) AS votes_total
FROM posts p LEFT JOIN
votes v
ON p.post_id = v.post_id
GROUP BY post_id
ORDER BY votes_total;";
echo "<table>\n";
echo '<tr>
<td> USER ID</td>
<td> PROPOSAL</td>
<td> DATE POSTED</td>
<td> SCORE</td>
</tr>';
if ($amorphous_cloud = mysqli_query ($thepipeline,$query)) {
while ($row = mysqli_fetch_assoc ($amorphous_cloud)){
echo '<tr>
<td> ' . $row['member_id'].'</td>
<td> ' . $row['content'].'</td>
<td> ' . $row['date_created'].'</td>
<td> ' . $row['votes_total'].'
<br><form method="POST"><input type="button" name="vote" value="Vote" /></form></td>
</tr>';
}
}
else {
print ":(";
}
echo "</table>\n";
?>
<br>
</div>
<div id="addpost">
<h3>Enter Proposal</h3>
<p>Off-topic posts will be deleted without warning.</p>
<?php
$member_id = $_GET['member_id'];
$content = $_GET['content'];
$link = mysqli_connect ('localhost', '******', '************,', '*************');
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
if (!empty ($_GET)) {
$query = "INSERT INTO posts (member_id, content) VALUES ('".$member_id."', '".$content."')";
print $query;
$member_result = mysqli_query ($link, $query);
}
$user_query = "SELECT member_id, member_name FROM members";
$user_result = mysqli_query ($link, $user_query);
$user_html = "";
while ($row = mysqli_fetch_assoc ($user_result)) {
$user_html = $user_html . "<option value='".$row['member_id']."'>".$row['member_name']."</option>\n";
}
?>
<form method="GET">
<!-- member -->
<p>Member</br>
<select name="member_id" >
<?php echo $user_html; ?>
</select>
</p>
<!-- date -->
<p>Content
</p>
<textarea name="content" cols="40" rows="5"></textarea>
</br>
<input type="submit" />
</form>
</div>
</div>

Passing php variable from one form to another page with hidden value

After passing a php variable(Tno) from one page to another page using button with hidden value, with that variable(Tno) tried to retrieve the data from the database, but I am not able to retrieve with that variable i.e. Tno. I had attached form1.php and retrieve.php.
form1.php
<?php
session_start();
?>
<?php
if( isset ($_SESSION["Email"])) {
$con=mysql_connect("localhost","admin","password");
// Check connection
if (!$con)
{
//echo "i am not ale to connect.<br>";
die('Could not connect: ' . mysql_error());
}
else{
//echo " i am able to connect.<br>";
}
mysql_select_db("maintable", $con);
$Email = mysql_real_escape_string($_SESSION['Email']);
$sql1="select * from customer where Email=('$Email')";
$result1=mysql_query($sql1);
while($row = mysql_fetch_array($result1)){
$var1 = $row['Identity'];
}
//echo" Email of the customer = $Email.<br>";
//echo "identity of the customer = $var1.<br>";
$sql2= "SELECT * FROM `suptest` WHERE Tno LIKE '$var1%'";
$result2=mysql_query($sql2); ?>
<table border=0 width=50%>
<tr>
<th>Ticket Ref. No.</th> <th>Subject</th><th>Status</th></tr>
<?php
while($row = mysql_fetch_array($result2)){
$ticketno= $row['Tno'];
?>
<form action="retrieve.php" method="post">
<tr><td><?php echo $row['Tno']; ?><input type="text" value="<?php echo $row['Tno']; ? >" name="Tno" /></td>
<td><?php echo $row['sub']; ?></td>
<td><?php echo $row['status']; ?></td>
<td><input type="Submit" value="Update" /></td></tr>
</form>
<?php
} ?>
</table>
<?php
mysql_close($con);
}
?>
<?php
session_start();
?>
<html><head></head>
<body>
<?php
$con=mysql_connect("localhost","admin","password");
//echo "Check connection";
if (!$con)
{
//echo "I am not ale to connect.<br>";
die('Could not connect: ' . mysql_error());
}
else{
//echo "I am able to connect.<br>";
}
mysql_select_db("maintable", $con);
$Tno = mysql_real_escape_string($_POST['Tno']);
//echo "my tno is $Tno.<br>";
$Email = mysql_real_escape_string($_SESSION['Email']);
echo $Email;
$sql = "select * from `active1active1` where Tno = ('$Tno')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
$result=mysql_query($sql);
echo $result;
echo mysql_num_rows($result);
echo "entering while loop";
while($row=mysql_fetch_array($result))
{
echo "entered while loop";
echo $row['Tno'] . "<br />";
echo $row['Email'] . "<br />";
echo $row['pdesc'] . "<br />";
echo $row['Activity'] . "<br />";
}
?>
</body></html>
im going to take a stab here:
this line
<tr><td><?php echo $row['Tno']; ?><input type="text" value="<?php echo $row['Tno']; ? >" name="Tno" /></td>
should be this?
<tr><td><?php echo $row['Tno']; ?><input type="hidden" name='Tno' value="<?php echo $row['Tno']; ? >" name="Tno" /></td>
note i added type=hidden and a name for the input
try:
$sql = "select * from `active1active1` where Tno = '" . $Tno . "'";
value="<?php echo $row['Tno']; ? >
There should not be any space after ?
change to ?>
<form action="retreive.php" method="post">
<tr>
<td>
<input type="hidden" value="<?php echo $row['Tno']; ?>" name="Tno" />
</td>
<td>
<input type="Submit" value="Update" />
</td>
</tr>
</form>
retrie`
try this simply:
$Tno = mysql_real_escape_string($_POST['Tno']);
echo $sql = "select * from `active1active1` where Tno = '$Tno'";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
print_r($row);
}

PHP/ SQL restricting viewing rights

I want to ensure "StaffID" is stored when the "View Contacts" page is loaded from a link, rather than straight from the Login Form
LOGIN FORM:
<?php session_start(); // Start PHP session
$StaffID = isset($_SESSION["StaffID"]) ? $_SESSION["StaffID"] : "";?>
<form name="staffaccess" method="post" action="staff-login.php">
<table border="1" cellpadding="3" cellspacing="1">
<tr>
<td colspan="3"><strong>Staff Login </strong></td>
</tr>
<input type="hidden" name="StaffID" id="StaffID" value="<?php echo $StaffID; ?>" />
<tr>
<td>Username:</td>
<td><input name="StaffUsername" size= "30" type="text" id="StaffUsername" value="<?php echo $StaffUsername; ?>"/></td>
</tr>
<tr>
<td>Password:</td>
<td><input name="StaffPassword" size= "30" type="text" id="StaffPassword" value="<?php echo $StaffPassword; ?>"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="Login"/></td>
</tr>
</table>
</form>
LOGIN CHECK:
<?php session_start(); // Start PHP session?>
<body>
<?php
$_SESSION["StaffUsername"] = isset($_POST["StaffUsername"]) ? $_POST["StaffUsername"] : "";
$_SESSION["StaffPassword"] = isset($_POST["StaffPassword"]) ? $_POST["StaffPassword"] : "";
$_SESSION["StaffID"] = isset($_GET["StaffID"]) ? $_GET["StaffID"] : "";
<?php
//connect to database//
$dbc = mysql_connect("", "", "");
if (!$dbc)
die ('Could not connect: ' .mysql_error());
//select database//
$db_selected = mysql_select_db("tafe", $dbc );
if (!$db_selected)
die ('Could not connect: ' . mysql_error());
// username and password sent from form
$StaffUsername=$_POST['StaffUsername'];
$StaffPassword=$_POST['StaffPassword'];
// To protect MySQL injection (more detail about MySQL injection)
$StaffUsername = stripslashes($StaffUsername);
$StaffPassword = stripslashes($StaffPassword);
$StaffUsername = mysql_real_escape_string($StaffUsername);
$StaffPassword = mysql_real_escape_string($StaffPassword);
$qry=("SELECT * FROM staffaccess WHERE Username= '" . $StaffUsername . "' AND Password= '" .$StaffPassword ."'");
$rst = mysql_query($qry, $dbc);
$row = mysql_fetch_array($rst);
if ($row["Username"]==$StaffUsername && $row["Password"]==$StaffPassword)
{
$_SESSION["StaffID"] = $row["StaffID"];
echo "Your login was successful";
echo "</br></br>";
echo "<a href=list-contacts.php>Continue</a>";
}
else {
echo "Sorry your details are not valid";
echo "</br></br>";
echo "<a href=staff-login.htm>Return</a>";
}
?>
VIEW CONTACTS (i only want this to allow to view contacts that particular user has added)
<?php
//connect to database
$dbc = mysql_connect("", "", "");
if (!$dbc)
die ('Could not connect: ' .mysql_error());
//select database
$db_selected = mysql_select_db("tafe", $dbc );
if (!$db_selected)
die ('Could not connect: ' . mysql_error());
$StaffID = (int)$_GET['StaffId'];
// build sql insert statement
**$qry = "SELECT * FROM contacts WHERE StaffID= $StaffID ORDER by name ASC";**
//run insert satement against database
$rst = mysql_query($qry, $dbc);
// print whether successful or not
if ($rst)
{
if (mysql_num_rows($rst)>0) // check that there are records
{
echo "<table border=\"1\" cellspacing=\"0\">";
/***print out field names***/
echo "<tr>"; // start row
for ($i=0; $i<mysql_num_fields($rst); $i++) // for each field print out field name
{
echo "<th>" . mysql_field_name($rst, $i) . "</th>";
}
echo "<th> </th>";
echo "<th> </th>";
echo "</tr>";
/***print out field values***/
while ($row = mysql_fetch_array($rst)) // fetch each of the rows
{
echo "<tr>";
echo "<td>".$row['ContactID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "<td>".$row['Address']."</td>";
echo "<td>".$row['Phone']."</td>";
echo "<td>".$row['Mobile']."</td>";
echo "<td>".$row['Email']."</td>";
echo "<td><a href='edit-contact.php?id=".$row['ContactID']."'>Edit</a></td>";
echo "<td><a href='delete-contact.php?id=".$row['ContactID']."'>Delete</a></td><tr>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "<b><font color='black'>No records returned.</font></b>";
}
}
else
{
echo "<b><font color='red'>Error: ".mysql_error($dbc) . "</font></b>";
}
?>
you didnt pass the staff id on contact page so you pass the staff id like this
change following change in logincheck page
if ($row["Username"]==$StaffUsername && $row["Password"]==$StaffPassword)
{
echo "Your login was successful";
echo "</br></br>";
echo "Continue";
}
you can also use session for logged user
Depending on you mysql version, you may need to quote your where properties, I'm not sure if this is causing your problem, but it may be related. Also, are you sure that your value for the StaffID field is being correctly inserted into the database?
I checked the code and you are using
echo "<a href=list-contacts.php>Continue</a>";
to send user to see the contacts and in this page you are doing
$StaffID = (int)$_GET['StaffId'];
So you need to pass that value in the query string as
echo "Continue";

insert output from mysql query into html table one after another

heloo, i want to serarch particular row from mysql database and same should be displayed on html table. I have tried the below code but it only gives the single row which is result of current query, previous row get overwritten. I am fetching a row for particular id,next time when i give another id,another row should be fetched and it should be added to the table next to previous one..
<html>
<body>
<form action="<?php $_PHP_SELF ?>" method="POST">
<tr>
ProductID: <input type="number" name="ProductID">
<input type="submit" value ="Go">
</form>
</tr>
</body>
</html>
<?php
$con=mysqli_connect("localhost","root","m70830807","Inventory");
// Check connection
if (!$con)
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db("Inventory",$con);
$ID=$_POST['ProductID'];
//echo $ID;
$result = mysqli_query($con,"SELECT ProductID, ProductName, UnitPrice FROm Products_Sold where ProductID =" .$ID);
echo"<table border = '1'>
<tr>
<th>ProductID</th>
<th>ProductName</th>
<th>UnitPrice</th>
</tr> ";
//$row=mysqli_fetch_array($result);
//$c1= $row['ProductID'];
//$c2=$row['ProductName'];
//$c3=$row['UnitPrice'];
//echo $c1;
//echo $c2;
//echo $c3;
//$ins= mysqli_query($con,"insert into Temp (ProductID,ProductName,UnitPrice) values ('%d','%s','%f')", $c1,$c2,$c3);
//$fetch=mysqli_query($con,"select ProductId,ProductName,UnitPrice from Temp");
while( $row = mysqli_fetch_array($result))
{ echo "<tr>";
echo "<td>". $row['ProductID'] . "</td>";
echo "<td>" . $row['ProductName'] ." </td>";
echo "<td>" . $row['UnitPrice'] . "</td>";
echo "</tr> ";
}
echo "</table>";
mysqli_close($con);
?>
If you need to do this within a user session (one user has one list. Another got different) so you need to use session
In your example -
<?php
session_start();
$con=mysqli_connect("localhost","root","m70830807","Inventory");
// Check connection
if (!$con)
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db("Inventory",$con);
$ID=$_POST['ProductID'];
$_SESSION['ids'][mysql_escape_string($ID)] = array();
//echo $ID;
$result = mysqli_query($con,"SELECT ProductID, ProductName, UnitPrice FROm Products_Sold where ProductID IN (\"" . implode('","', usort(array_keys( $_SESSION['ids']))). '");');
echo"<table border = '1'>
<tr>
<th>
ProductID
</th>
<th>
ProductName
</th>
<th>
UnitPrice
</th>
</tr>
";
while( $row = mysqli_fetch_array($result)) { echo "
<tr>
"; echo "
<td>
". $row['ProductID'] . "
</td>
"; echo "
<td>
" . $row['ProductName'] ."
</td>
"; echo "
<td>
" . $row['UnitPrice'] . "
</td>
"; echo "
</tr>
"; } echo "
</table>
";
mysqli_close($con);
?>
You need to use ajax and jquery to do this.
send request via ajax and get response as your row. Manipulate the table using jquery to append in the existing set of records

Categories