I am using html php and mysql for my program. i want it to check a record from a database then insert a record on it if it does not exist. if it exists, it will still add the record and put the existing record on a different table. but i want it to confirm the user first before doing it.
i think my code is kind of brute force but its all i have. thanks.
<html>
<head>
<title>Move Monitor</title>
<?php require 'mysqlconnect.php';
include('session.php');
?>
<link rel="stylesheet" href="css/style.css">
<meta charset="UTF-8">
</head>
<body>
<?php
if (isset($_POST['ws'])) {
$ws=$_SESSION['todeploy'];
$comp=$_POST['ws'];
$daterecord=date("m/d/y");
$sql = "SELECT count(workstation_id) from workstation where workstation_id = '$comp' ";
$query = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($query);
$rows = $row[0];
if ($rows<1) {
$sql="insert into workstation (workstation_id,monitor) values ('$comp','$ws')";
if (!mysqli_query($conn,$sql))
{
die('Error adding ws' . mysql_error());
}
$sql="UPDATE computer_unit set date_recorded= '$daterecord' , deployed_stocked='deployed' status ='OK' where asi_code = '$ws' " ;
if (!mysqli_query($conn,$sql))
{
die('Error qupdate unit' . mysql_error());
}
}else{
$sql = "SELECT monitor from workstation where workstation_id = '$comp' ";
$query = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($query);
$monitor = $row['monitor'];
if ($monitor=='') {
$sql="UPDATE workstation set monitor = '$ws' where workstation_id = '$comp'";
if (!mysqli_query($conn,$sql))
{
die('Error upd ws' . mysql_error());
}
$sql="UPDATE computer_unit set date_recorded= '$daterecord' , deployed_stocked='deployed' , status='OK' where asi_code = '$ws' " ;
if (!mysqli_query($conn,$sql))
{
die('Error qupdate unit' . mysql_error());
}
}else{
$sql="UPDATE computer_unit set date_recorded = '$daterecord' , deployed_stocked='stocked' where asi_code = '$monitor' " ;
if (!mysqli_query($conn,$sql))
{
die('Error unit deployed' . mysql_error());
}
$sql="UPDATE computer_unit set date_recorded= '$daterecord' , deployed_stocked='deployed' , status ='OK' where asi_code = '$ws' " ;
if (!mysqli_query($conn,$sql))
{
die('Error unit stocked' . mysql_error());
}
$sql="UPDATE workstation set monitor = '$ws' where workstation_id = '$comp' " ;
if (!mysqli_query($conn,$sql))
{
die('Error ws' . mysql_error());
}
}
}
unset($_SESSION['todeploy']);
echo "<script> alert('Success.')</script>";
?><script>document.location="monitorstock.php" </script>
<?php
}
else{
$_SESSION['todeploy']=$_GET['del'];
?>
<div id="addform">
Add Details: <br><br>
<form action="monitordeploy.php" method="post" enctype="multipart/form-data">
<label>Workstation:</label><br> <input type="text" id="ws" name="ws" size="70%" style="margin-bottom:10px;" required>
<br>
<input type="Submit" name="add" value="Submit" style=" float: right; margin-top: 10%; width: 100;font-family: arial helvetica san-serif; font-size:15px;">
</form>
<button onclick="window.location='monitorstock.php'" style="float: right;margin-top:15%;margin-right:-100;width: 100;"> Back </button>
<?php } ?>
</body>
</html>
Using mysql and PHP. If I understand you correctly, just query the database.
$sql = "SELECT id FROM MYTABLE where id = 'val' ";
$query = mysqli_query($conn,$sql);
if(!$row = mysqli_fetch_array($query))
{
//Insert data as no record exists
}
else
{
//add data to another table
}
I think there is issue while getting the results and processing.You can count number of row result by
$rows = mysql_num_rows($query)
Then
If($rows<1){ //Your code }
You can build a function for example will call it unique($sql) that accept sql select statement which will return true or false, if it returns true that means you have a record if false you haven't, it will be something like that:
function unique($sql) {
$result = mysqli_query($conn,$sql);
$count = mysqli_num_rows($result);
if ($count) {
return true;
}
return false;
}
so after that what you can do is to say:
if (unique($sql)) {
// insert some data in the database
}
You could use ajax to fetch data immediately and the code will be something like this:
<script>
$("button").click(function(){
var data = $('#your-input');
$.ajax({
url: // the url to the php page for example test.php,
type: 'post',
data: {data: data},
success: function (response) { // this function will be triggered if ajax request has been sent correctly
// and here what you can do is to check data received from response and perform the right response to the user
}
});
});
</script>