Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieve auto increment
    primarykey
    data
    text
    <p>last entered auto increments data. In my case the owner_ID of the previously entered record into the mySQL database.</p> <pre><code>&lt;?php require "formfunctions.inc.php"; require "connect.inc.php"; startPage("Add details"); connectAndSelect(); //Uses my include php script functions to logon to mySQL using my credentials or die if(isset($_POST['addDetails'])) //isset means Is Set, so this makes clicking the submit button true { errorCheckDetails(); } else { showAddFormDetails(); } endPage(); ?&gt; </code></pre> <p>Here is the functions include file: </p> <pre><code>&lt;?php /*This is a PHP script containing the majority of my forms, which I have made into functions. It keeps all the back-end data together, meaning the other PHP files are a lot simpler, as I only need to call the functions if I need to use any of them, less code repitition meaning maximum efficency. Again it is a required script so files relying on this will have it being required and will not parse if this file cannot be parsed by the server.*/ //******************************THIS FUNCTION IS CALLED AT THE BEGINNING OF EVERY PAGE, IT SETS UP THE DOCTYPE, DEFAULT COMMENT, NAVBAR, AND DIV TAGS TO BE DISPLAYED*************** function startPage($title = '') { echo "&lt;!doctype html&gt; &lt;!--NICK LITTLE WEB 2 ASSIGNMENT 2- THE MOTOR VEHICLE REGISTER DATABASE--&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset=utf-8&gt; &lt;title&gt;$title&lt;/title&gt; &lt;link href='css.css' rel='stylesheet' type='text/css'&gt; &lt;/head&gt; &lt;body&gt; &lt;div class='header'&gt; &lt;img src='header.jpg'/&gt; &lt;ul id='list-nav'&gt; &lt;li&gt;&lt;a href='frontend.php'&gt;Home&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href='search.php'&gt;Search/Delete&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href='addowner.php'&gt;Add New Owner &amp; Vehicle&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href='addvehicle.php'&gt;Add Vehicle&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;div class='container'&gt; &lt;div class='content'&gt;"; } //**********SIMILAR TO THE START PAGE, CLOSES OFF DIV DAGS AND HTML BODY AND ENDING********************************************************************************** function endPage() { echo " &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt;"; } //****************THIS FORM IS DISPLAYED ON THE ADD OWNER/VEHICLE PAGE. IT JUST DISPLAYS THE FORM FOR USER INPUT************************************************************************** function showAddFormDetails() { $self=$_SERVER['PHP_SELF']; //making the form self referring //start form display - EVERYTHING IS DISPLAYED IN A NICE LOOKING FORM echo "&lt;form action='$self' method='POST'&gt; &lt;center&gt; &lt;table id='searchForm'&gt; &lt;thead&gt; &lt;tr&gt; &lt;th colspan ='2'&gt;Owner&lt;/th&gt; &lt;th colspan ='2'&gt; Vehicle&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tfoot&gt; &lt;tr&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='submit' name='addDetails' value='Add Details'/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tfoot&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;Title:&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='title' size='5px'/&gt;&lt;/td&gt; &lt;td&gt;Make:&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='make' size='20px'/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Name:&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='name' size='25px'/&gt;&lt;/td&gt; &lt;td&gt;Model&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='model' size='20px'/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Address:&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='address' size='48px'/&gt;&lt;/td&gt; &lt;td&gt;Year:&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='year' size='10px'/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Phone:&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='phonenumber' size='10px'/&gt;&lt;/td&gt; &lt;td&gt;Registration:&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='rego' size='10px'/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;Kilometres:&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='kms' size='10px'/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/form&gt;"; } //**********FUNCTION TO DISPLAY FORM TO ADD A VEHICLE TO AN EXISTING OWNER******************************************************************************************************* function showAddFormVehicle() //SELECT NAME FROM THE OWNER ID TO PERFORM AN SQL QUERY ON { $self=$_SERVER['PHP_SELF']; //making the form self referring //start form display - EVERYTHING IS DISPLAYED IN A NICE LOOKING FORM echo"Please fill out the required fields to add a vehicle to the system&lt;br /&gt;&lt;br /&gt; Please note: A vehicle must be added to an &lt;strong&gt;existing owner&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt; Click here to &lt;a href='addowner.php'&gt; add a new owner and vehicle&lt;/a&gt; &lt;form action='$self' method='POST'&gt; &lt;center&gt; &lt;table id='searchForm'&gt; &lt;thead&gt; &lt;tr&gt; &lt;th colspan ='2'&gt; Vehicle&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tfoot&gt; &lt;tr&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='submit' name='addVehicle' align='right' value='Add Vehicle'/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tfoot&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;Owner:&lt;/td&gt; &lt;td&gt;&lt;select name='owner_ID'&gt;&lt;option value='-1' selected='selected'&gt;Please select an owner...&lt;/option&gt;"; $selectString ="SELECT DISTINCT owner_ID, name FROM tblOwner"; $result=mysql_query($selectString); while($row = mysql_fetch_assoc($result)) { $owner_ID = $row['owner_ID']; $owner_name = $row['name']; echo "&lt;option value='$owner_ID'&gt;$owner_name&lt;/option&gt;"; } echo"&lt;/select&gt; &lt;tr&gt; &lt;td&gt;Make:&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='make' size='20px'/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Model:&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='model' size='20px'/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; `&lt;td&gt;Year:&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='year' size='10px'/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Registration:&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='rego' size='10px'/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Kilometres:&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='kms' size='10px'/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/form&gt;"; } //************FUNCTION TO ERROR CHECK THE INPUT ON THE ADDING A VEHICLE PAGE - SLIGHT VARIATION ON ADDING NEW VEHICLE AND OWNER SIMULTANEOUSLY************************************************** function delete() { /*foreach($_POST as $field =&gt; $value) { /*echo "$value&lt;br&gt; $field&lt;br&gt;"; }*/ $vehicleArray = $_POST['deleteButton']; $size = sizeOf( $vehicleArray); /*echo "SIZE is: $size&lt;br&gt;"; //SIZE returns a '1' echo "ARRAY is: $vehicleArray"; echo" $vehicleArray[0]"; //First index of array is a 'D'*/ for ($i = 0; $i &lt; $size; $i++) //LOOP THROUGH THE SIZE AND ARRAY { $num = $vehicleArray[$i]; $query = "DELETE FROM tblVehicle WHERE vehicle_ID='$num'"; //echo"$query"; $result = mysql_query($query); echo "Thank you, the selected vehicle(s) were removed from the system &lt;br /&gt; &lt;i&gt; Please note the OWNER of the vehicle will remain in the system &lt;/i&gt;"; } } //*****************************THIS FUNCTION ERROR CHECKS USER INPUT WHEN ATTEMPTING TO INSERT AN OWNER************************************************************************** function errorCheckDetails() { //assigning variables to the fields filled in, creates variables and assigns to 'NAME' form input value------ //----------owner variables----------------// $ownerTitle = mysql_real_escape_string($_POST['title']); $ownerName = mysql_real_escape_string($_POST['name']); $ownerAddress = mysql_real_escape_string($_POST['address']); $ownerPhone = mysql_real_escape_string($_POST['phonenumber']); //--------vehicle variables------------// $vehicleMake = mysql_real_escape_string($_POST['make']); $vehicleModel = mysql_real_escape_string($_POST['model']); $vehicleYear = mysql_real_escape_string($_POST['year']); $vehicleRego = mysql_real_escape_string($_POST['rego']); $vehicleKms = mysql_real_escape_string($_POST['kms']); $allFilled = true; //checking to see that all individual fields are filled in: if (empty($_POST['title'])==0) $allFilled = false; //If a specefic form field is empty, it is set to true, or else it is false if (empty($_POST['name'])==0) $allFilled = false; if (empty($_POST['address'])==0) $allFilled = false; if (empty($_POST['phonenumber'])==0) $allFilled = false; if (empty($_POST['make'])==0) $allFilled = false; if (empty($_POST['model'])==0) $allFilled = false; if (empty($_POST['year'])==0) $allFilled = false; if (empty($_POST['rego'])==0) $allFilled = false; if (empty($_POST['kms'])==0) $allFilled = false; //providing if all of the fields are filled in, insert user's data into owner table, all required fields if ($allFilled) { //*********************************************mySQL queries********************************************************** $insertOwnerQuery="INSERT INTO tblOwner(title,name,address,phone) VALUES ('$ownerTitle','$ownerName','$ownerAddress','$ownerPhone')"; $result=mysql_query($insertOwnerQuery); $aOwner = mysql_insert_id(); //Assign variable to mySQL function, returns last known user id input, so as to determine auto_inc for owner_ID $insertVehicleQuery="INSERT INTO tblVehicle(owner_ID,make,model,year,rego,kms) VALUES ('$aOwner','$vehicleMake','$vehicleModel','$vehicleYear','$vehicleRego','$vehicleKms')"; $result=mysql_query($insertVehicleQuery); echo "Thank you, your entry has been added to the system"; //Echo to screen to inform user owner has been added successfully. } else { //error messages that appear for each individual field that is not filled in: if (empty($_POST["title"])) echo"&lt;p&gt;The 'Owner Title' field must be filled in.&lt;/p&gt;"; if (empty($_POST["name"])) echo "&lt;p&gt;The 'Name' field must be filled in.&lt;/p&gt;"; if (empty($_POST["address"])) echo "&lt;p&gt;The 'Address' field must be filled in.&lt;/p&gt;"; if (empty($_POST["phonenumber"])) echo "&lt;p&gt;The 'Phone Number' field must be filled in.&lt;/p&gt;"; if (empty($_POST["make"])) echo "&lt;p&gt;The 'Vehicle Make' field must be filled in.&lt;/p&gt;"; if (empty($_POST["model"])) echo "&lt;p&gt;The 'Vehicle Model' field must be filled in.&lt;/p&gt;"; if (empty($_POST["year"])) echo "&lt;p&gt;The 'Vehicle Year' field must be filled in.&lt;/p&gt;"; if (empty($_POST["rego"])) echo "&lt;p&gt;The 'Vehicle Registration' field must be filled in.&lt;/p&gt;"; if (empty($_POST["kms"])) echo "&lt;p&gt;The 'Vehicle Kilometers' field must be filled in.&lt;/p&gt;"; } /*echo '&lt;form action = '$self' method='POST'&gt; &lt;input type='submit' name='returnAddOwner' value='Return to Adding an Owner'&gt; &lt;/form&gt;';*/ } //************FUNCTION TO ERROR CHECK THE INPUT ON THE ADDING A VEHICLE PAGE - SLIGHT VARIATION ON ADDING NEW VEHICLE AND OWNER SIMULTANEOUSLY************************************************** function errorCheckVehicle() { //assigning variables to the fields filled in, creates variables and assigns to 'NAME' form input value------ //----------owner variables----------------// $owner_ID = $_POST['owner_ID']; //NEED 2 FIGURE OUT HOW TO DETECT OPTION VALUE FOR OWNER NAME //--------vehicle variables------------// $vehicleMake = $_POST['make']; $vehicleModel = $_POST['model']; $vehicleYear = $_POST['year']; $vehicleRego = $_POST['rego']; $vehicleKms = $_POST['kms']; $allFilled = true; //checking to see that all individual fields are filled in: if ($vehicleMake == "") $allFilled = false; if ($vehicleModel == "") $allFilled = false; if ($vehicleYear == "") $allFilled = false; if ($vehicleRego == "") $allFilled = false; if ($vehicleKms == "") $allFilled = false; //providing if all of the fields are filled in, insert user's data into owner table, all required fields if ($allFilled) { $insertVehicleQuery="INSERT INTO tblVehicle(owner_ID,make,model,year,rego,kms) VALUES ('$owner_ID','$vehicleMake','$vehicleModel','$vehicleYear','$vehicleRego','$vehicleKms')"; $result=mysql_query($insertVehicleQuery); echo "Thank you, the vehicle has been added to the system"; //Echo to screen to inform user owner has been added successfully. } //error messages that appear for each individual field that is not filled in: else { if (empty($_POST["make"])) echo "&lt;p&gt;The 'Vehicle Make' field must be filled in.&lt;/p&gt;"; if (empty($_POST["model"])) echo "&lt;p&gt;The 'Vehicle Model' field must be filled in.&lt;/p&gt;"; if (empty($_POST["year"])) echo "&lt;p&gt;The 'Vehicle Year' field must be filled in.&lt;/p&gt;"; if (empty($_POST["rego"])) echo "&lt;p&gt;The 'Vehicle Registration' field must be filled in.&lt;/p&gt;"; if (empty($_POST["kms"])) echo "&lt;p&gt;The 'Vehicle Kilometers' field must be filled in.&lt;/p&gt;"; } } //*********************************************************DISPLAY SEARCH FORM ON PAGE************************************************************************ function showSearchForm() { $self=$_SERVER['PHP_SELF']; //making the form self referring echo " &lt;form action='$self' method='POST'&gt; &lt;center&gt; &lt;table id='searchForm'&gt; &lt;thead&gt; &lt;tr&gt; &lt;th colspan='2'&gt;Vehicle&lt;/th&gt; &lt;th colspan='2'&gt;Owner&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tfoot&gt; &lt;tr&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td class='searchSubmitButtons'&gt;&lt;input type='submit' name='search' value='Search Records' /&gt;&lt;/td&gt; &lt;td class='searchSubmitButtons'&gt;&lt;input type='submit' name='search' value='List all database entries' /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tfoot&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;Make:&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='vehiclemake' size='20' /&gt;&lt;/td&gt; &lt;td&gt;Name:&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='ownername' size='25' /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Model:&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='vehiclemodel' size='20' /&gt;&lt;/td&gt; &lt;td&gt;Address:&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='owneraddress' size='48' /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;label for='vehicleyear'&gt;Year:&lt;/label&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='vehicleyear' id='vehicleyear' size='10' /&gt;&lt;/td&gt; &lt;td&gt;Phone:&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='ownerphone'/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Registration:&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='vehiclerego' size='10' /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Km's:&lt;/td&gt; &lt;td&gt;&lt;input type = 'text' name='vehiclekms' size='10'/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/center&gt; &lt;/form&gt;"; } function showRecords() { $self=$_SERVER['PHP_SELF']; //making the form self referring //assigning variables to the fields filled in: //$owner_ID = $_POST['ownerID']; //$title = $_POST['ownertitle']; $name = $_POST['ownername']; $address = $_POST['owneraddress']; $phone = $_POST['ownerphone']; $make = $_POST['vehiclemake']; $model = $_POST['vehiclemodel']; $year = $_POST['vehicleyear']; $rego = $_POST['vehiclerego']; $kms = $_POST['vehiclekms']; //print search results from both tables - patients and owners (unnecessary fields or duplicates excluded) % is the like function, so could put 'at' to get cat: $selectString = "SELECT vehicle_ID,make,model,year,rego,kms,name,phone FROM tblVehicle,tblOwner WHERE (tblVehicle.owner_ID = tblOwner.owner_ID AND make LIKE '%$make%' AND model LIKE '%$model%' AND rego LIKE '%$rego%' AND kms LIKE '%$kms%' AND name LIKE '%$name%' AND address LIKE '%$address%' AND phone LIKE '%$phone%')"; $result = mysql_query($selectString); //vehicle_ID, tblOwner.owner_ID, echo"&lt;form action = '$self' method='POST'&gt; &lt;table border='1' cellpadding='8'&gt; &lt;tr&gt; &lt;th&gt;Vehicle ID&lt;/th&gt; &lt;th&gt;Vehicle Make&lt;/th&gt; &lt;th&gt;Vehicle Model&lt;/th&gt; &lt;th&gt;Vehicle Year&lt;/th&gt; &lt;th&gt;Vehicle Registration&lt;/th&gt; &lt;th&gt;Vehicle KM&lt;/th&gt; &lt;th&gt;Owner Name&lt;/th&gt; &lt;th&gt;Owner Phone&lt;/th&gt; &lt;th&gt;Delete&lt;/th&gt; &lt;/tr&gt;"; while($row = mysql_fetch_row($result)) { echo '&lt;tr&gt;'; foreach($row as $field =&gt; $value) { if( $field == 0) $vehicle_ID=$value; echo "&lt;td&gt;$value&lt;/td&gt;"; } echo"&lt;td&gt;&lt;input type ='checkbox' name = deleteButton[] value='$vehicle_ID'&gt;&lt;/td&gt;"; echo '&lt;/tr&gt;'; } echo "&lt;/table&gt; &lt;br&gt;&lt;br&gt; &lt;input type='submit' name='returnSearch' value='Return to Searching Records'&gt; &lt;input type='submit' name='del' align='right' value='Delete Selected Records'&gt; &lt;/form&gt;"; } ?&gt; </code></pre> <p>Sorry they are big files. Im new here :( Don't know how to post big amounts of code.</p> <p>Cheers</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload