Multiple queries with addition of column in another table - php

Please i would like to delete a record from a particular table and insert same record into another table. this is working fine if all the tables contain same columns but i need to add another column to the table where the deleted record is inserted.
here is my code thank you
<?php
// connect to the database
$con=mysqli_connect("localhost", "root", "");
if(mysqli_select_db($con, "e-office"));
$execute ='';
$Posting_User = mysqli_escape_string($con, $_SESSION[('Uname')]);
// confirm that the 'id' variable has been set
if (isset($_GET['execute'])) $execute = $_GET['execute'];
{
// get the 'id' variable from the URL
if($execute=='delete'){
$id = $_GET['id'];
// delete record from database
$sql = mysqli_query($con, "INSERT INTO tbl_income_approved SELECT * FROM
tbl_income WHERE (trn_no = '$id' AND Approved_by ='$Posting_User') ");
$sql = mysqli_query($con, "DELETE FROM tbl_income WHERE trn_no = '$id'");
if($sql)
// redirect user after delete is successful
header("Location: income_report.php");
else
// if the 'id' variable isn't set, redirect the user
echo "query not successful";
}
}
?>

To God be the glory! I have a working code now, thanks all
<?php
session_start();
if(!$_SESSION[('Uname')]){
header("location:login.php");
}
// connect to the database
$con=mysqli_connect("localhost", "root", "");
if(mysqli_select_db($con, "e-office"));
$execute ='';
$Posting_User = mysqli_escape_string($con, $_SESSION[('Uname')]);
// confirm that the 'id' variable has been set
if (isset($_GET['execute'])) $execute = $_GET['execute'];
{
$id = $_GET['id'];
///testing
$sql="SELECT * FROM tbl_income WHERE trn_no='$id'";
$result=mysqli_query($con, $sql);
//echo $count;
while($row = mysqli_fetch_assoc($result)){
$Posting_User = mysqli_escape_string($con, $row['Posting_User']);
$date = mysqli_escape_string($con, $row['date']);
$rno = mysqli_escape_string($con, $row['rno']);
$source = mysqli_escape_string($con, $row['source']);
$subsidiary = mysqli_escape_string($con, $row['subsidiary']);
$deposit = mysqli_escape_string($con, $row['deposit']);
$amount = mysqli_escape_string($con, $row['amount']);
$narration = mysqli_escape_string($con, $row['narration']);
$timestamp = mysqli_escape_string($con, $row['timestamp']);
$trn_no = mysqli_escape_string($con, $row['trn_no']);
$Approved_by = mysqli_escape_string($con, $_SESSION[('Uname')]);
$sql=mysqli_query($con, "INSERT INTO tbl_income_approved (Posting_User, date, rno, subsidiary, deposit, source, amount, narration, Approved_by) VALUES ('$Posting_User','$date','$rno', '$subsidiary', '$deposit', '$source', '$amount', '$narration', '$Approved_by')");
}
///close testing
// get the 'id' variable from the URL
if($execute=='delete'){
$id = $_GET['id'];
$sql = mysqli_query($con, "DELETE FROM tbl_income WHERE trn_no = '$id'");
if($sql)
// redirect user after delete is successful
header("Location: income_report.php");
else
// if the 'id' variable isn't set, redirect the user
echo "query not successful";
}
}
?>

Related

table doens't show the updated column

in my code, the table doesn't refresh and show the updated column once the update button is clicked.
here is my code.
<?php
if(isset($_POST['update'])){
$Project = $_POST['Project'];
$No = $_POST['No'];
$SubID = $_POST['SubID'];
$RequestAmount = $_POST['RequestAmount'];
$PaidAmount = $_POST['PaidAmount'];
$AmountToPay = $_POST['AmountToPay'];
$State = $_POST['State'];
//UPDATE Query of SQL
$sql = "UPDATE memo SET Project='$Project',No='$No',SubID='$SubID',RequestAmount='$RequestAmount',PaidAmount='$PaidAmount',AmountToPay='$AmountToPay',State='$State' WHERE No='$No' AND SubID='$SubID'"
or die("Failed to query database" .mysqli_error());
$result = $link->query($sql);
}
?>
It is because you update database after loading the page.
You can do something like that:
<?php
if(isset($_POST['update'])) {
$Project = $_POST['Project'];
$No = $_POST['No'];
$SubID = $_POST['SubID'];
$RequestAmount = $_POST['RequestAmount'];
$PaidAmount = $_POST['PaidAmount'];
$AmountToPay = $_POST['AmountToPay'];
$State = $_POST['State'];
//UPDATE Query of SQL
$sql = "UPDATE memo SET Project='$Project',No='$No',SubID='$SubID',RequestAmount='$RequestAmount',PaidAmount='$PaidAmount',AmountToPay='$AmountToPay',State='$State' WHERE No='$No' AND SubID='$SubID'"
or die("Failed to query database" .mysqli_error());
$result = $link->query($sql);
header("Refresh: 0;");
die();
}
?>
I added lines
header("Refresh: 0;");
die();
It simply refreshes the page after update.
Hope this helps, if not, then sorry.

Not saving in database table

I want edit record in db table but it doesn't save in db table and nothing changed after i submit this form.
Here codes that i forgot to put.
<?php
require('db.php');
include("auth.php"); //include auth.php file on all secure pages
$id_doc=$_REQUEST['id_doc'];
$query = "SELECT * from doc where id_doc='".$id_doc."'";
$result = mysqli_query($connection, $query) or die ( mysqli_error());
$row = mysqli_fetch_assoc($result);
?>
This is my php codes
<?php
if(isset($_POST['new']) && $_POST['new']==1)
{
$id_doc=$_REQUEST['id_doc'];
$query = "SELECT * from doc where id_doc='".$id_doc."'";
$result = mysqli_query($connection, $query) or die ( mysqli_error());
$row = mysqli_fetch_assoc($result);
$title =$_REQUEST['title'];
$date = $_REQUEST['date'];
$from_to = $_REQUEST['from_to'];
$details = $_REQUEST['details'];
$d_location = $_REQUEST['d_location'];
$d_stat = $_REQUEST['d_stat'];
$update="update doc set title='".$title."', date='".$date."', from_to='".$from_to."', details='".$details."', d_location='".$d_location."', d_stat='".$d_stat."' where id_doc='".$id_doc."'";
mysqli_query($connection, $update) or die(mysql_error());
$status = "File Record Updated Successfully. </br></br><a href='v_doc.php'>View Updated Record</a>";
echo '<p style="color:#FF0000;">'.$status.'</p>';
}else {
// here some else code
}
?>
Not an answer. Too long for a comment.
The issue of parametrised queries aside, I find this easier to read:
UPDATE doc
SET title = '$title'
, date = '$date'
, from_to = '$from_to'
, details = '$details'
, d_location = '$d_location'
, d_stat = '$d_stat'
WHERE id_doc = '$id_doc'
And now see about parametrised queries
Try below:
<?php
if(isset($_POST['new']) && $_POST['new']==1)
{
$id_doc=$_REQUEST['id_doc'];
$query = "SELECT * from doc where id_doc='".$id_doc."'";
$result = mysqli_query($connection, $query) or die ( mysqli_error());
$row = mysqli_fetch_assoc($result);
$title =$_REQUEST['title'];
$date = $_REQUEST['date'];
$from_to = $_REQUEST['from_to'];
$details = $_REQUEST['details'];
$d_location = $_REQUEST['d_location'];
$d_stat = $_REQUEST['d_stat'];
$update="update doc set title='".$title."', date='".$date."', from_to='".$from_to."', details='".$details."', d_location='".$d_location."', d_stat='".$d_stat."' where id_doc='".$id_doc."'";
if(mysqli_query($connection, $update)) {
$status = "File Record Updated Successfully. </br></br><a href='v_doc.php'>View Updated Record</a>";
} else {
die(mysqli_error($connection));
}
echo '<p style="color:#FF0000;">'.$status.'</p>';
} else {
// here some else code
}
?>
This should show you exact error, once you get it. show it here, so we can check and do correction.

Problems updating correct row in databse with php

I'm trying to create a voting system for artists played on my radio station. I'm using the source code from: http://dl.howcode.org/download/97ff383c7d4dc9939c65c9e6fab2a5dc
The problem I have found is that the votes update using the number from the first row in the database no matter which option is selected, thus if for instance the first row has 3 votes in and the user tries to vote on someone with 0 votes, it will change the votes for the correct artist to 4 instead of 1... I hope that makes sense?
The code I have is:
[EDIT] I have changed the queries to fetch assoc to make it easier to understand.
<?php
$voteID = $_GET['voteID'];
$connect = mysqli_connect('xxx', 'xxx', 'xxx', 'xxx');
$query = "SELECT * FROM listenervotes WHERE voteID='$voteID'" ;
$q = mysqli_query($connect, $query);
while($row = mysqli_fetch_assoc($q)){
$id = $row["id"];
$voteTitle = $row["voteTitle"];
$voteID = $row["voteID"];
$ipaddress = $row["ipAddress"];
echo "<h3>$voteTitle</h3>";
?>
<table>
<form action="" method="POST">
<?php
$artists = "SELECT * FROM artists WHERE voteID='$voteID'" ;
$q2 = mysqli_query($connect, $artists);
while($r = mysqli_fetch_assoc($q2)){
$artist = $r["artistName"];
$votes = $r["votes"];
$genre = $r["genre"];
$ip = $_SERVER['REMOTE_ADDR'];
$newIpAddress = $ipaddress."$ip, ";
$newVotes = $votes + 1;
if (isset($_POST['vote'])) {
$voteOption = $_POST['voteOption'];
if ($voteOption == ""){
die("You haven't selected anyone!");
}else{
$ipaddressE = explode(",", $ipaddress);
if(in_array($ip, $ipaddressE)){
die("You have already voted!");
}else{
mysqli_query($connect, "UPDATE artists SET votes='$newVotes' WHERE voteID='$voteID' AND artistName='$voteOption'");
mysqli_query($connect, "UPDATE listenervotes SET ipaddress='$newIpAddress' WHERE voteID='$voteID'");
die('You voted successfully!<br><tr><td>'.$artist.'</td><td>'.$genre.'</td><td>'.$votes.' Votes</td></tr>');
}
}
}
echo '<tr><td>'.$artist.'</td><td>'.$genre.'</td><td><input type="radio" name="voteOption" value="'.$artist.'"</td></tr>';
}
}
?>
I could be missing something obvious, in my mind I'm thinking that I somehow need to iterate through the rows before setting the new value, if so, how and where?
It looks like you are always looping over all rows and updating the relevant row with the first value found. Adding a check on the ID should do:
<?php
$voteID = $_GET['voteID'];
$connect = mysqli_connect('xxx', 'xxx', 'xxx', 'xxx');
$query = "SELECT * FROM listenervotes WHERE voteID='$voteID'" ;
$q = mysqli_query($connect, $query);
while($row = mysqli_fetch_assoc($q)){
$id = $row["id"];
$voteTitle = $row["voteTitle"];
$voteID = $row["voteID"];
$ipaddress = $row["ipAddress"];
echo "<h3>$voteTitle</h3>";
?>
<table>
<form action="" method="POST">
<?php
$artists = "SELECT * FROM artists WHERE voteID='$voteID'" ;
$q2 = mysqli_query($connect, $artists);
while($r = mysqli_fetch_assoc($q2)){
$artist = $r["artistName"];
$votes = $r["votes"];
$genre = $r["genre"];
$ip = $_SERVER['REMOTE_ADDR'];
$newIpAddress = $ipaddress."$ip, ";
$newVotes = $votes + 1;
if (isset($_POST['vote'])) {
$voteOption = $_POST['voteOption'];
if ($voteOption == ""){
die("You haven't selected anyone!");
}else{
$ipaddressE = explode(",", $ipaddress);
if(in_array($ip, $ipaddressE)){
die("You have already voted!");
}elseif ($voteOption === $artist) { // Don't run UPDATE when we're on the wrong row.
mysqli_query($connect, "UPDATE artists SET votes='$newVotes' WHERE voteID='$voteID' AND artistName='$voteOption'");
mysqli_query($connect, "UPDATE listenervotes SET ipaddress='$newIpAddress' WHERE voteID='$voteID'");
die('You voted successfully!<br><tr><td>'.$artist.'</td><td>'.$genre.'</td><td>'.$votes.' Votes</td></tr>');
}
}
}
echo '<tr><td>'.$artist.'</td><td>'.$genre.'</td><td><input type="radio" name="voteOption" value="'.$artist.'"</td></tr>';
}
}
?>

Half of PHP script not submitting

I have made a php script to pull from two different tables within the same database. After the data is pulled, it is put into another table that will hold that specific information for later use. Right now, it will submit the userid and username but will not submit the puid variable I have stated.
Here is the script
include('data.php');
//Database Connection
$con=#mysql_connect("$ip", "$guser", "$gpass")
or die(mysql_error());
//Select Database
$dbcon=#mysql_select_db($forums, $con)
or die(mysql_error());
$search = $_POST['term'];
$sql = mysql_query("select userid, usergroupid, username from $users where username like '%$search%'");
while ($row = mysql_fetch_array($sql)) {
$id = $row['userid'];
$name = $row['username'];
$ugid = $row['usergroupid'];
}
if ($ugid == '21') {
$sql4 = "INSERT INTO $vip (fuid, username) VALUES ('$id', '$name')";
$res2 = #mysql_query($sql4, $con) or die(mysql_error());
$sql2 = mysql_query("SELECT $id, field5 FROM $userfield");
while ($row = mysql_fetch_array($sql2)) {
$puid = $row['field5'];
}
$sql3 = "INSERT INTO $vip (puid) VALUES ('$puid')";
$res = #mysql_query($sql3, $con) or die(mysql_error());
echo 'Completed';
} else {
echo 'User is not VIP';
}
you are not doing those queries for all retrieved rows.you should get those queries inside foreach loop otherwise only the last row will be effected!

PHP writing to MYSQL is variable equals 0

I am trying to write to my database if a variable is equal to 0. The problem is that it still writes to the database even when the variables equals 1. What is wrong??
echo $new_user;
if ($new_user == 0) {
//SENT NEW USER WELCOME MESSAGE
$adminid = '9';
$welcomemessagetitle = 'Welcome to The site';
$welcomemessagecontent = 'Hello and welcome';
$addmessages = "INSERT into `user_messages`(`to_user`,`from_user`,`title`,`content`)
VALUES ('$userid','$adminid','$welcomemessagetitle','$welcomemessagecontent');";
$query = mysql_query($addmessages) or die(mysql_error());
//SET USER AS NOT NEW USER
$newuservalue = '1';
$notnewuser = "UPDATE users SET new_user = $newuservalue WHERE id = $userid" ;
$query2 = mysql_query($notnewuser) or die(mysql_error());
} elseif ($new_user == 1) {};
UPDATE FULL CODE::
<?php
session_start();
include "../includes/db_connect.php";
///profile/index.php
if($_SESSION['id'])
{
$username = $_SESSION['username'];
$userid = $_SESSION['id'];
//WRITE FIRST TIME LOGIN INFORMATION TO DATABASE
$sql="SELECT new_user from `users` WHERE `id`= $userid ";
$res=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_assoc($res)) $new_user = $row['new_user'] ;
echo $new_user;
if ($new_user == 0) {
//SENT NEW USER WELCOME MESSAGE
$adminid = '9';
$welcomemessagetitle = 'Welcome to Escorvee';
$welcomemessagecontent = 'Hello and welcome';
$addmessages = "INSERT into `user_messages`(`to_user`,`from_user`,`title`,`content`)
VALUES ('$userid','$adminid','$welcomemessagetitle','$welcomemessagecontent');";
$query = mysql_query($addmessages) or die(mysql_error());
//SET USER AS NOT NEW USER
$newuservalue = '1';
$notnewuser = "UPDATE users SET new_user = $newuservalue WHERE id = $userid" ;
$query2 = mysql_query($notnewuser) or die(mysql_error());
} elseif ($new_user == 1) {};
}
?>
It the variable $new_user is the value 1 then your code won't be executed so I would guess one of the following applies:
$new_user isn't 1.
The database is being modified from another location.
Your script is being called twice.
To work out which you will have to provide more information in your question.

Categories