Insert foreign key value in my sql database? - php

I want to insert foreign key value in table.I have two tales employee(employee_id) and attendance. Here employee_id is foreign key in attendance table.
I try a lot but value is not inserted.
Here is my code
if(isset($_POST['submit']))
{
$date = date('Y-m-d',strtotime($_POST['daily_date']));
$in = $_POST['daily_in'];
$l_out = $_POST['lunch_out'];
$l_in = $_POST['lunch_in'];
$out = $_POST['daily_out'];
$emp_remarks = $_POST['remarks'];
$sql = "INSERT INTO attendance (atten_id,daily_date,daily_in,lunch_out,lunch_in,daily_out,remarks,employee_id)
VALUES('NULL','$date','$in','$l_out','$l_in','$out','$emp_remarks','".$_REQUEST['employee_id']."')";
$res = mysql_query($sql);
if ($res > 0) {
echo "inserted";
}
If I run below code then
if(isset($_POST['submit']))
{
$date = $_POST['daily_date'];
$in = $_POST['daily_in'];
$l_out = $_POST['lunch_out'];
$l_in = $_POST['lunch_in'];
$out = $_POST['daily_out'];
$emp_remarks = $_POST['remarks'];
if(isset($_REQUEST['employee_id']))
{
echo "Employee Id" .$_REQUEST['employee_id'];
}
else {
echo "Smoething went wrong";
}
$sql = "INSERT INTO attendance (atten_id,daily_date,daily_in,lunch_out,lunch_in,daily_out,remarks,employee_id)
VALUES
('NULL','$date','$in','$l_out','$l_in','$out','$emp_remarks','".$_REQUEST['employee_id']."')";
its gives
Smoething went wrong not inserted error

Before inserting try this , try this to check whether value was passed or not.
isset($_REQUEST['employee_id'])
{ echo "Employee Id" .$_REQUEST['employee_id'];
} else {
echo "Smoething went wrong";
}

Related

How to retrieve data after insert with new value in another table

<?php include('dbcon.php');
include('header.php');
//variable
$clientID='';
$billAmount='';
$arrear='';
$monthlyBill='';
$surcharge='';
//data add
$clientID=$_POST['clientID'];
$sqll = "SELECT `client`.`clientId` , (
`arrear` + `surcharge` + `monthlyBill`) AS 'billamount', (
FROM `billifno`
JOIN `client` ON `billifno`.`clientID` = `client`.`clientID` WHERE `billifno`.`clientID`='".$_POST["clientID"]."'";
$result = mysqli_query($con,$sqll);
// if ($result->num_rows > 0)
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$clientId = $row['clientId'];
$arrear = $row['arrear'];
$monthlyBill= $row['monthlyBill'];
$billAmount = $row['billAmount'];
$surcharge = $row['surcharge'];
}
//echo $billAmount;
$sql = "INSERT INTO `billIfno`
(`clientID`, `billAmount`,`arrear`, `monthlyBill`,`surcharge`) VALUES
('$clientID','$billAmount','$arrear','$monthlyBill','$surcharge')";
if ($con->query($sql)=== true)
{
echo "Recorded!!!";
}
else
{
echo "Not Recorded !!!";
}
$con->close();
?>
Hare I am taking some value and process it using query then again insert into billInfo table but it not inserting the new values like
If I echo billAmonu it print the correct value that is calculated by query bt that value is not inserting into database.

Not able to insert the data from url to database in php

final.php
Here I am trying to get the data from the url using GET method and trying to insert into the database. I was able to insert the data for first few rows after that the data is not inserted. Can anyone help me regarding this?
when I try to run the url: www.myii.com/app/final.php?name=123&glucose=3232...
the data is not inserting.
<?php
include("query_connect.php");
$name = $_GET['name'];
$glucose = $_GET['glucose'];
$temp = $_GET['temp'];
$battery = $_GET['battery'];
$tgs_a = $_GET['tgs_a'];
$tgs_g = $_GET['tgs_g'];
$heartrate = $_GET['heartrate'];
$spo2 = $_GET['spo2'];
$rr = $_GET['rr'];
$hb = $_GET['hb'];
$ina22 = $_GET['ina22'];
$accucheck = $_GET['accucheck'];
$isactive = $_GET['isactive'];
$address = $_GET['address'];
$deviceno = $_GET['deviceno'];
$sql_insert = "insert into query (name,glucose,temp,battery,tgs_a,tgs_g,heartrate,spo2,rr,hb,ina22,accucheck,isactive,address,deviceno) values ('$name','$glucose','$temp',$battery','$tgs_a','$tgs_g','$heartrate','$spo2','$rr','$hb','$ina22','$accucheck','$isactive','$address','$deviceno')";
mysqli_query($sql_insert);
if($sql_insert)
{
echo "Saving succeed";
//echo $date_time;
}
else{
echo "Error occured";
}
?>
Query_connect.php
This is my database config php file.
<?php
$user = "m33root";
$password = "me3i434";
$host = "localhost";
$connection = mysqli_connect($host,$user,$password);
$select = mysqli_select_db('miiyy',$connection);
if($connection)
{
echo "connection succesfull<br>";
}
else {
echo "Error";
}
?>
Make sure that all columns can contain NULL so that not filled fields will stay NULL instead of throwing an error.
Try below to see mysql error:
mysql_query($sql_insert);
echo mysql_error()
Try these
In SQL
Change the table name of query to some other name. Because "query" is reserved in SQL
In code
if (!mysqli_query($con,$sql_insert ))
{
echo("Error description: " . mysqli_error($con));
}
else
{
echo "Success";
}
Use mysqli_error() function

how to check the exisitng data from database before inserting

This is my code and it does not work, it's not inserting into the database. Please help me fix this problem.
$orgexist = $_POST['orgName1'];
$_SESSION['id'] = $_POST['id'];
$orgid = $_POST['id'];
$orgnme = $_POST['orgName1'];
$orgdesc = $_POST['orgDesc'];
$orgcat = $_POST['cat'];
$orgdept = $_POST['coldept'];
$orgvis = $_POST['vision'];
$orgmis = $_POST['mision'];
//get the value of category from database
//echo $orgdept;
$dept = "SELECT `col_id`, `col_description` FROM `college` WHERE `col_description` = '$orgdept'";
$deptresult = mysql_query($dept);
while ($rows = mysql_fetch_array($deptresult)) {
$getcol = $rows['col_id'];
//echo $getcol;
}
$sqlorg = mysql_query("SELECT * FROM `organization`");
while ($orgrows = mysql_fetch_array($sqlorg)) {
//$dborgid = $orgrows['org_id'];
$dborgnme = $orgrows['org_name'];
}
if ($dborgnme == $orgexist) {
echo "<script type='text/javascript'>
alert('Organization Name Already Used by other Organization');
history.back();
</script>";
} else {
$orginsrt = mysql_query("INSERT INTO `organization`(`org_id`,`org_name`,`org_desc`,`category`,`vision`,`mission`,`col_id`,`image`) VALUES ('$orgid','$orgexist','$orgdesc','$orgcat','$orgvis','$orgmis','$getcol','$image')");
echo "<script type='text/javascript'>
alert('Proceed to next Step');</script>";
//require ('orgsignup.php');
header('Location:orgsignup2.php');
//echo "Not in the Record";
}
}
You're using deprected functions, replace mysql_query by mysqli_query.
For more references see http://php.net/manual/en/function.mysql-query.php

query wongt output to table. correct syntax

What am i doing wrong here?
(Know its mysql)
this else is not working it look like. user can insert same modul id many times.
$besvarelse = $_GET['besvarelse'];
$modulid = $_GET['modulid'];
$username = $_GET['username'];
$tilkobling = kobleTil();
if (!empty($besvarelse) ) {
$insert = mysql_query("INSERT INTO oppgave (besvarelse, modulid, username) VALUES ('$besvarelse', '$modulid','$username')");
if($insert) {
echo "<h1>Levering OK</h1><br>";
}
}
else {
die("<h1>Du har lever før</h1>");
}
?>
Try with this and post the errors that PHP shows, please:
//Assuming that connection to mysql server its done somewhere with mysql_connect()
error_reporting(E_ALL); ini_set('display_errors', 1);
explode($_GET);
echo "<pre>";
print_r($_GET); //Just to see what's on the variables...
echo "</pre>;
// $tilkobling = kobleTil(); Need to explain this line
if (!empty($besvarelse) ) {
$sql = "INSERT INTO oppgave (besvarelse, modulid, username) VALUES ('$besvarelse', '$modulid','$username')";
if($insert = mysql_query($sql))
{
echo "<h1>Levering OK</h1><br>";
}
else {
die("<h1>Du har lever før</h1>");
}
} // you were missmatching the IF closing bracket
?>
try to check for exist rows like
$besvarelse = $_GET['besvarelse'];
$modulid = $_GET['modulid'];
$username = $_GET['username'];
$tilkobling = kobleTil();
if (!empty($besvarelse) ) {
// check exist moduleid
$check = mysql_query("select * from oppgave where modulid = '$modulid'");
$conut_rows= mysql_num_rows($check);
if($conut_rows > 0) {
echo "module id already exist";
}
else {
$insert = mysql_query("INSERT INTO oppgave (besvarelse, modulid, username) VALUES ('$besvarelse', '$modulid','$username')");
if($insert) {
echo "<h1>Levering OK</h1><br>";
}
else {
die("<h1>Du har lever før</h1>");
}
}
}

PHP Validating Submit

I'm working on a project where a user can click on an item. If the user clicked at it before , then when he tries to click at it again it shouldn't work or INSERT value on the DB. When I click the first item(I'm displaying the items straight from database by id) it inserts into DB and then when I click at it again it works(gives me the error code) doesn't insert into DB. All other items when I click at them , even if I click for the second, third, fourth time all of it inserts into DB. Please help guys. Thanks
<?php
session_start();
$date = date("Y-m-d H:i:s");
include("php/connect.php");
$query = "SELECT * FROM test ORDER BY `id` ASC LIMIT 3";
$result = mysql_query($query);
if (isset($_SESSION['username'])) {
$username = $_SESSION['username'];
$submit = mysql_real_escape_string($_POST["submit"]);
$tests = $_POST["test"];
// If the user submitted the form.
// Do the updating on the database.
if (!empty($submit)) {
if (count($tests) > 0) {
foreach ($tests as $test_id => $test_value) {
$match = "SELECT user_id, match_id FROM match_select";
$row1 = mysql_query($match)or die(mysql_error());
while ($row2 = mysql_fetch_assoc($row1)) {
$user_match = $row2["user_id"];
$match = $row2['match_id'];
}
if ($match == $test_id) {
echo "You have already bet.";
} else {
switch ($test_value) {
case 1:
mysql_query("UPDATE test SET win = win + 1 WHERE id = '$test_id'");
mysql_query("INSERT INTO match_select (user_id, match_id) VALUES ('1','$test_id')");
break;
case 'X':
mysql_query("UPDATE test SET draw = draw + 1 WHERE id = '$test_id'");
mysql_query("INSERT INTO match_select (user_id, match_id) VALUES ('1','$test_id')");
break;
case 2:
mysql_query("UPDATE test SET lose = lose + 1 WHERE id = '$test_id'");
mysql_query("INSERT INTO match_select (user_id, match_id) VALUES ('1','$test_id')");
break;
default:
}
}
}
}
}
echo "<h2>Seria A</h2><hr/>
<br/>Welcome,".$username."! <a href='php/logout.php'><b>LogOut</b></a><br/>";
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$home = $row['home'];
$away = $row['away'];
$win = $row['win'];
$draw = $row['draw'];
$lose = $row['lose'];
echo "<br/>",$id,") " ,$home, " - ", $away;
echo "
<form action='seria.php' method='post'>
<select name='test[$id]'>
<option value=\"\">Parashiko</option>
<option value='1'>1</option>
<option value='X'>X</option>
<option value='2'>2</option>
</select>
<input type='submit' name='submit' value='Submit'/>
<br/>
</form>
<br/>";
echo "Totali ", $sum = $win+$lose+$draw, "<br/><hr/>";
}
} else {
$error = "<div id='hello'>Duhet te besh Log In qe te vendosesh parashikime ndeshjesh<br/><a href='php/login.php'>Kycu Ketu</a></div>";
}
?>
Your problem is here :
$match = "SELECT user_id, match_id FROM match_select";
$row1 = mysql_query($match)or die(mysql_error());
while ($row2 = mysql_fetch_assoc($row1)) {
$user_match = $row2["user_id"];
$match = $row2['match_id'];
}
You are not checking it correctly. You have to check if the entry in match_select exists for the user_id and the match_id concerned. Otherwise, $match would always be equal to the match_id field of the last inserted row in your database :
$match = "SELECT *
FROM `match_select`
WHERE `user_id` = '<your_id>'
AND `match_id` = '$test_id'";
$matchResult = mysql_query($match)or die(mysql_error());
if(mysql_num_rows($matchResult)) {
echo "You have already bet.";
}
By the way, consider using PDO or mysqli for manipulating database. mysql_ functions are deprecated :
http://www.php.net/manual/fr/function.mysql-query.php
validate insertion of record by looking up on the table if the data already exists.
Simplest way for example is to
$query = "SELECT * FROM match_select WHERE user_id = '$user_id'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0)
{
// do not insert
}
else
{
// do something here..
}
In your form you have <select name='test[$id]'> (one for each item), then when you submit the form you are getting $tests = $_POST["test"]; You don't need to specify the index in the form and can simply do <select name='test[]'>, you can eventually add a hidden field with the id with <input type="hidden" value="$id"/>. The second part is the verification wich is not good at the moment; you can simply check if the itemalready exist in the database with a query

Categories