I didn’t know that was an option, so it’ll be loads easier if I deal with only 1 form instead of 2 then I could handle it like
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST"){
echo '<pre>';
print_r($_POST);
echo '</pre>';
$width = isset($_POST['width']) ? $_POST['width'] : NULL;
$beginning_x = isset($_POST['beginning_x']) ? $_POST['beginning_x'] : NULL;
$sql = "INSERT INTO devices (back,beginning_slot,ending_slot,device,width,beginning_x) VSLUES (".$_POST[back].",".$_POST[beginning_slot].",
".$_POST[ending_slot].",'".$_POST[device]."',".$width.",".$beginning_x.")
ON DUPLICATE KEY UPDATE devices SET back = '".$_POST[back]."', beginning_slot = '".$_POST[beginning_slot]."',
ending_slot = '".$_POST[ending_slot]."', device = '".$_POST[device]."', width = '".$width."', beginning_x = '".$beginning_x."'
WHERE panel_id = ".$_POST[panel_id];
if(mysqli_query($conn, $sql)){
echo "<h1 class='text-center'style='margin:20px'><img src='images/loading.gif'></h1>";
?>
Then, I can run either query from 1 form
<form method="POST" action="<?=htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="form-row">
<div class="form-group col-sm-2">
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">#</div>
</div>
<input type="text" name="beginning_slot" class="form-control" value="<?=$row['beginning_slot']?>">
<input type="text" name="ending_slot" class="form-control" value="<?=$row['ending_slot']?>">
</div>
</div>
<div class="form-group col-sm-2">
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">#</div>
</div>
<input type="text" name="beginning_x" class="form-control" value="<?=$beginning_x?>">
</div>
</div>
<div class="form-group col-sm-2">
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">#</div>
</div>
<input type="text" name="width" class="form-control" value="<?=$width?>">
</div>
</div>
<div class="form-group col-sm-2">
<select name="back" class="form-control">
<option value="<?=$row['back']?>"><?=$orientation?></option>
<option value="1">Back</option>
<option value="0">Front</option>
</select>
</div>
<div class="form-group col-sm-3">
<input type="text" name="device" class="form-control" value="<?=$row['device']?>">
</div>
<div class="form-group col-sm-1">
<button type="submit" class="btn btn-outline-dark" style="margin-left:5px">Update</button>
</div>
</div>
<input type="hidden" name="panel_id" value="<?=$row['panel_id']?>">
<input type="hidden" name="rack_id" value="<?=$rack_id?>">
</form>
I do have 2 questions though,
This form, if a new record is being added, it wont give me an error if I have value=“” on some of the inputs
<input type="text" name="device" class="form-control" value="">
and manually enter a name
Also, my table is sett up like
create table devices (
panel_id INT NOT NULL AUTO_INCREMENT,
rack_id INT NOT NULL,
back CHAR(1) DEFAULT '1',
beginning_slot DECIMAL(3,1) NOT NULL,
ending_slot DECIMAL(3,1) NOT NULL,
device VARCHAR(100) NOT NULL,
width INT DEFAULT NULL,
beginning_x INT DEFAULT NULL,
FOREIGN KEY ( rack_id ) REFERENCES racks ( rack_id) ,
PRIMARY KEY ( panel_id )
);
In order for Better query to work, don I need to set up a unique key so I can test if a DUPLICATE one exhists?