so my newest problem! i have a save button and that save button already has the insert into query and working fine now what i wanna do is when the user clicks on the save button what it does first is, it checks if a record of that user already exists and if there already is a record in regards to that user then instead of inserting it will update the table. but before updating i want a confirmation message that asks the user if they would like to update because there already is a record. and if there isnt a record then it will just insert. ive already googled things like "check if record exist in mysql db" and "Redirect to other page on alert box confirm" and got the coding (just had to tweek it to make it work with mine) but to combine the whole thing im very sure im not doing it right as i have files calling other files(im pretty certain theres a better way than mine, always is).
what i have so far:
saveRecord.php
<?php
session_start();
if($_SESSION['name']){
if(isset($_POST['save']))
{
$ahouse = $_POST['ahouse'];
$avehicle = $_POST['avehicle'];
$acurrent = $_POST['acurrent'];
$asaving = $_POST['asavings'];
$akwsp = $_POST['akwsp'];
$ahaji = $_POST['ahaji'];
$aasb = $_POST['aasb'];
$astock = $_POST['astock'];
$aproperty = $_POST['aproperty'];
$aothers = $_POST['aothers'];
$total = $_POST['assets'];
require "connect.php";
$name=$_SESSION['name'];
$check=mysql_query("SELECT COUNT(*) FROM `asset` WHERE name = '$name' LIMIT 1");
if(!$check)
{
die('Invalid query: ' .mysql_error());
}
if(mysql_num_rows($check) == 0)
{
$name=$_SESSION['name'];
$query1=mysql_query("INSERT INTO asset(name,house,vehicle,current,savings,kwsp,haji,asb,stock,property,others,total) VALUES ('$name','$ahouse','$avehicle','$acurrent','$asaving','$akwsp','$ahaji','$aasb','$astock','$aproperty','$aothers','$total')");
if(!$query1)
{
die('Invalid query: ' .mysql_error());
}
else
{
echo ('<script>alert("Make sure you fill in your liability sheet!")
window.location="asset.php"</script>');
}
}
else
{
echo ('<script type="text/javascrit">
function deletePost()
{
var ask = window.confirm("You have already filled in this form. Would you like to update?");
if (ask)
{
document.location.href = "update.php";
}
}
<script>');
}
}
i had also used SELECT name FROM asset WHERE name = '$name' LIMIT 1 instead of COUNT(asterik) because i read that using COUNT isnt good but in this code im showing you guys it uses count coz i changed it.
update.php (i also think this file is messed up and i probably dont need this file but i didn't know how to go about doing this whole coding to achieve what i want)
<?php
session_start();
if($_SESSION['name'])
{
if(isset($_POST['save']))
{
$ahouse = $_POST['ahouse'];
$avehicle = $_POST['avehicle'];
$acurrent = $_POST['acurrent'];
$asaving = $_POST['asavings'];
$akwsp = $_POST['akwsp'];
$ahaji = $_POST['ahaji'];
$aasb = $_POST['aasb'];
$astock = $_POST['astock'];
$aproperty = $_POST['aproperty'];
$aothers = $_POST['aothers'];
$total = $_POST['assets'];
require "connect.php";
$name=$_SESSION['name'];
$update=mysql_query("UPDATE asset SET house='$ahouse', vehicle='$avehicle', current='$acurrent', savings='$asaving', kwsp='$akwsp',
haji='$ahaji', asb='$aasb', stock='$astock', property='$aproperty', others='$aothers', total='assets' WHERE name='$name'");
if($update)
{
echo ('<script>alert("Your asset values have been updated")
header("Location : saveRecord.php");</script>');
}
}
}
?>
thanks in advance