• I am using airtable as a tool database for my company. This lets us keep track of what we have in stock and easily search for tool details and location. I built a basic local frontend for us to use, displaying and filtering through the database works great. The only thing that isn’t working for me is changing the database from the frontend.

    Each tool is brought in to the frontend and displays the important details in a list. The value I would like to be able to change is the quantity of tool we have – so I have two buttons, Take Tool and Return Tool. Ideally when each is pressed, it would subtract or add one to the Qty. in Stock field. The buttons are there, but pressing them doesn’t update them like I’d like. Here is my code:

    <?php /* Template Name: SMWTools */
    
    $query = new AirpressQuery();
    
    $query->setConfig("SMW");
    
    $query->table("Tools")->view("Master");
    
    /*~~~~~~~~~~~~~~~~~~~~~~~ FILTER BY MACHINE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    
    if($_POST['machine'] == "lathe"){
    	$query->addFilter("{Machine}='Lathe'");
    }
    /*~~~~~~~~~~~~~~~~~~~~~~ FILTER BY VENDOR ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    
    if($_POST['vendor'] == "AB Tools"){
    	$query->addFilter("{Vendor}='AB Tools'");
    }
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ FILTER BY FLUTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    
    if($_POST['flutes'] == "1fl"){
    	$query->addFilter("{Flutes}='1'");
    }
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ FILTER BY DIAMETER ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    
    if($_POST['diameter'] == "1/8"){
    	$query->addFilter("{Cutting Diameter}<'.125'");
    }
    
    $tools = new AirpressCollection($query);
    ?>
    
    <html>
    	<header>
    	</header>
    	<body>
    		
    		<form>
    			<input type="text" name="search" placeholder="Search Tools">
    		</form>
    		
    		<form method = "POST" action="">
    			<input type="radio" name="machine" value="mill">Mill<br>
    			<input type="radio" name="machine" value="lathe">Lathe<br><br>
    			<select name="vendor">
    				<option value="Vendor">Filter by Vendor</option>
    				<option value="AB Tools">AB Tools</option>
    				<option value="AccuPro">AccuPro</option>
    				<option value="Allied Machine">Allied Machine</option>
    				<option value="Amana Tools">Amana Tools</option>
    				<option value="Bantam Tools">Bantam Tools</option>
    				<option value="Brush Research">Brush Research</option>
    				<option value="CJT">CJT</option>
    				<option value="CK">CK</option>
    				<option value="Datron">Datron</option>
    				<option value="Dijet">Dijet</option>
    				<option value="Emuge">Emuge</option>
    				<option value="Garr">Garr</option>
    				<option value="Generic">Generic</option>
    				<option value="Gorilla Mill">Gorilla Mill</option>
    				<option value="Guhring">Guhring</option>
    				<option value="Harvey Tool">Harvey Tool</option>
    				<option value="Helical Tool">Helical Tools</option>
    				<option value="HTC">HTC</option>
    				<option value="JJ Tools">JJ Tools</option>
    				<option value="Korloy">Korloy</option>
    				<option value="Lakeshore Carbide">Lakeshore Carbide</option>
    				<option value="Maritool">Maritool</option>
    				<option value="Mitsubishi">Mitsubishi</option>
    				<option value="Moon Cutter Co.">Moon Cutter Co.</option>
    				<option value="Morse">Morse</option>
    				<option value="Niagara">Niagara</option>
    				<option value="Nine9/Everede">Nine9/Everede</option>
    				<option value="OSG">OSG</option>
    				<option value="Precision Tools">Precision Tools</option>
    				<option value="Precision Twist Drills">Precision Twist Drills</option>
    				<option value="Sandvik">Sandvik</option>
    				<option value="Scientific Cutting Tools">Scientific Cutting Tools</option>
    				<option value="Seco">Seco</option>
    				<option value="Shars">Shars</option>
    				<option value="Sumitomo">Sumitomo</option>
    				<option value="Tormach">Tormach</option>
    				<option value="Ultra-Tool">Ultra-Tool</option>
    				<option value="Union Tool">Union Tool</option>
    				<option value="Whiteside">Whiteside</option>
    				<option value="YG-1">YG-1</option>
    			</select>
    			<select name="flutes">
    				<option value="# of Flutes">Filter by # of Flutes</option>
    				<option value="1fl">1</option>
    				<option value="2fl">2</option>
    				<option value="3fl">3</option>
    				<option value="4fl">4</option>
    				<option value="5fl">5</option>
    				<option value="6fl">6</option>
    				<option value="7fl">7</option>
    				<option value="8fl">8</option>
    				<option value="9fl">9</option>
    				<option value="10fl">10</option>
    				<option value="11fl">11</option>
    				<option value="12fl">12</option>
    			</select>
    			<select name="diameter">
    				<option value="Cutting Diameter">Cutting Diameter</option>
    				<option value="1/8"><1/8</option>
    				<option value="3/16">1/8 - .1874</option>
    				<option value="1/4">3/16 - .249</option>
    				<option value="3/8">1/4 - .374</option>
    				<option value="1/2">3/8 - .499</option>
    				<option value="halfinch">1/2</option>
    				<option value="big">>1/2</option>
    			</select>
    			<input type="submit" value="Filter">
    		</form>
    	
    	</body>
    </html>
    
    <?php
    	
    echo "<table style='width:100%'>
    		<tr>
    			<th width='125'>Vendor</th>
    			<th width='100'>P/N</th>
    			<th width='100'>Diameter</th>
    			<th width='50'>Flutes</th>
    			<th width='100'>OAL</th>
    			<th width='100'>Image</th>
    			<th width='50'>Drawer</th>
    			<th width='50'>Bin</th>
    			<th width='50'>Qty</th>
    			<th width='100'>Remove</th>
    			<th width='100'>Add</th>
    		</tr>
    	</table>";
    foreach($tools as $t){
    	echo "<table style='width:100%'>
    			<tr>
    				<td width='125' align='center'>".$t["Vendor"]."</td>
    				<td width='100' align='center'>".$t["Name"]."</td>
    				<td width='100' align='center'>".$t["Cutting Diameter"]."</td>
    				<td width='50' align='center'>".$t["Flutes"]."</td>
    				<td width='100' align='center'>".$t["OAL"]."</td>
    				<td width='100' align='center'><img src=".$t["Image Link"]." style='width:100px;'></td>
    				<td width='50' align='center' style='color:blue;'>".$t["Drawer #"]."</td>
    				<td width='50' align='center' style='color:blue;'>".$t["Bin #"]."</td>
    				<td width='50' align='center' style='color:black;'>".$t["Qty. in Stock"]."</td>
    				<td width='100' align='center'><form method='GET' action=''><input type='button' name='take' value='Take Tool'></td>
    				<td width='100' align='center'><form method='GET' action=''><input type='button' name='return' value='Return Tool'></td>
    			</tr>
    		</table>";
    		$fields_to_update = array();
    		if($_GET['take'] == "Take Tool"){
    			$fields_to_update["Qty. in Stock"] = ($t["Qty. in Stock"] + 1);
    			$t->update($fields_to_update);
    		}
    		if($_POST['return'] == "Return Tool"){
    				$fields_to_update["Qty. in Stock"] = ($t["Qty. in Stock"] - 1);
    				$t->update($fields_to_update);
    			}
    	}
    
    ?>

    The important bit that should change the field is at the very end. I also omitted some of the filtering code (I didn’t show every brand, just code for one individual) because it gets pretty repetitive. I tried to follow the AirPress tutorial that explains how to change fields within WordPress but my code doesn’t work.

Viewing 1 replies (of 1 total)
  • Thread Starter alexpinson

    (@alexpinson)

    Ignore the fact that one if statement is POST and one is GET, I copied this while changing things around and testing. Regardless, the code does not work.

Viewing 1 replies (of 1 total)
  • The topic ‘Changing an Airtable Field from AirPress’ is closed to new replies.