2011-10-01 61 views
-2

上次输入的自动增量数据。在我的情况下,先前输入的mySQL数据库中的记录的owner_ID。检索自动增量

​​

这里的功能包括文件:

<?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 "<!doctype html> 
<!--NICK LITTLE 
    WEB 2 ASSIGNMENT 2- THE MOTOR VEHICLE REGISTER DATABASE--> 
<html> 
    <head> 
    <meta charset=utf-8> 
    <title>$title</title> 
    <link href='css.css' rel='stylesheet' type='text/css'> 
    </head> 
    <body> 
    <div class='header'> 
     <img src='header.jpg'/> 
     <ul id='list-nav'> 
     <li><a href='frontend.php'>Home</a></li> 
     <li><a href='search.php'>Search/Delete</a></li> 
     <li><a href='addowner.php'>Add New Owner & Vehicle</a></li> 
     <li><a href='addvehicle.php'>Add Vehicle</a></li> 
     </ul> 
    </div> 
    <div class='container'> 
     <div class='content'>"; 
} 

//**********SIMILAR TO THE START PAGE, CLOSES OFF DIV DAGS AND HTML BODY AND ENDING********************************************************************************** 

function endPage() 
{ 
    echo " 
     </div> 
     </div> 
    </body> 
</html>"; 
} 


//****************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 "<form action='$self' method='POST'> 
    <center> 
     <table id='searchForm'> 
     <thead> 
     <tr> 
      <th colspan ='2'>Owner</th> 
      <th colspan ='2'> Vehicle</th>    
     </tr> 
     </thead> 
     <tfoot> 
     <tr> 
      <td></td> 
      <td></td> 
      <td><input type='submit' name='addDetails' value='Add Details'/></td> 
     </tr> 
     </tfoot> 
     <tbody> 
     <tr> 
      <td>Title:</td> 
      <td><input type='text' name='title' size='5px'/></td> 
      <td>Make:</td> 
      <td><input type='text' name='make' size='20px'/></td> 
     </tr> 

     <tr> 
      <td>Name:</td> 
      <td><input type='text' name='name' size='25px'/></td> 
      <td>Model</td> 
      <td><input type='text' name='model' size='20px'/></td> 
     </tr> 

     <tr> 
      <td>Address:</td> 
      <td><input type='text' name='address' size='48px'/></td> 
      <td>Year:</td> 
      <td><input type='text' name='year' size='10px'/></td> 
     </tr> 

     <tr> 
      <td>Phone:</td> 
      <td><input type='text' name='phonenumber' size='10px'/></td> 
      <td>Registration:</td> 
      <td><input type='text' name='rego' size='10px'/></td> 
     </tr> 

     <tr> 
      <td></td> 
      <td></td> 
      <td>Kilometres:</td> 
      <td><input type='text' name='kms' size='10px'/></td> 
     </tr> 
     </tbody> 
    </table> 
    </form>"; 
} 

//**********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<br /><br /> 
    Please note: A vehicle must be added to an <strong>existing owner</strong><br /><br /> 
    Click here to <a href='addowner.php'> add a new owner and vehicle</a> 

    <form action='$self' method='POST'> 
    <center> 
     <table id='searchForm'> 
     <thead> 
     <tr> 
      <th colspan ='2'> Vehicle</th>    
     </tr> 
     </thead> 
     <tfoot> 
     <tr> 
      <td></td> 
      <td></td> 
      <td><input type='submit' name='addVehicle' align='right' value='Add Vehicle'/></td> 
     </tr> 
     </tfoot> 
     <tbody> 
     <tr> 
      <td>Owner:</td> 
      <td><select name='owner_ID'><option value='-1' selected='selected'>Please select an owner...</option>"; 

    $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 "<option value='$owner_ID'>$owner_name</option>"; 
    }  
     echo"</select> 

      <tr> 
      <td>Make:</td> 
      <td><input type='text' name='make' size='20px'/></td> 
      </tr> 

      <tr> 
      <td>Model:</td> 
      <td><input type='text' name='model' size='20px'/></td> 
      </tr> 

      <tr> 
      `<td>Year:</td> 
      <td><input type='text' name='year' size='10px'/></td> 
      </tr> 

      <tr> 
      <td>Registration:</td> 
      <td><input type='text' name='rego' size='10px'/></td> 
      </tr> 

      <tr> 
      <td>Kilometres:</td> 
      <td><input type='text' name='kms' size='10px'/></td> 
      </tr> 
     </tbody> 
     </table> 
    </form>"; 
    } 

//************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 => $value) 
    { 
    /*echo "$value<br> 
     $field<br>"; 
    }*/ 

    $vehicleArray = $_POST['deleteButton']; 
    $size = sizeOf($vehicleArray); 




    /*echo "SIZE is: $size<br>";   //SIZE returns a '1' 
    echo "ARRAY is: $vehicleArray"; 
    echo" $vehicleArray[0]"; 
    //First index of array is a 'D'*/ 

    for ($i = 0; $i < $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 <br /> 
     <i> Please note the OWNER of the vehicle will remain in the system </i>"; 
    } 
} 

//*****************************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"<p>The 'Owner Title' field must be filled in.</p>"; 

    if (empty($_POST["name"])) 
     echo "<p>The 'Name' field must be filled in.</p>"; 

    if (empty($_POST["address"])) 
     echo "<p>The 'Address' field must be filled in.</p>"; 

    if (empty($_POST["phonenumber"])) 
     echo "<p>The 'Phone Number' field must be filled in.</p>"; 

    if (empty($_POST["make"])) 
     echo "<p>The 'Vehicle Make' field must be filled in.</p>"; 

    if (empty($_POST["model"])) 
     echo "<p>The 'Vehicle Model' field must be filled in.</p>"; 

    if (empty($_POST["year"])) 
     echo "<p>The 'Vehicle Year' field must be filled in.</p>"; 

    if (empty($_POST["rego"])) 
     echo "<p>The 'Vehicle Registration' field must be filled in.</p>"; 

    if (empty($_POST["kms"])) 
     echo "<p>The 'Vehicle Kilometers' field must be filled in.</p>"; 
    } 

/*echo '<form action = '$self' method='POST'> 
     <input type='submit' name='returnAddOwner' value='Return to Adding an Owner'>  
    </form>';*/ 
} 

//************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 "<p>The 'Vehicle Make' field must be filled in.</p>"; 

    if (empty($_POST["model"])) 
     echo "<p>The 'Vehicle Model' field must be filled in.</p>"; 

    if (empty($_POST["year"])) 
     echo "<p>The 'Vehicle Year' field must be filled in.</p>"; 

    if (empty($_POST["rego"])) 
     echo "<p>The 'Vehicle Registration' field must be filled in.</p>"; 

    if (empty($_POST["kms"])) 
     echo "<p>The 'Vehicle Kilometers' field must be filled in.</p>"; 
    } 
} 

//*********************************************************DISPLAY SEARCH FORM ON PAGE************************************************************************ 

    function showSearchForm() 
    { 
    $self=$_SERVER['PHP_SELF'];  //making the form self referring 

    echo " 
     <form action='$self' method='POST'> 
     <center> 
      <table id='searchForm'> 
      <thead> 
       <tr> 
       <th colspan='2'>Vehicle</th> 
       <th colspan='2'>Owner</th> 
       </tr> 
      </thead> 
      <tfoot> 
       <tr> 
       <td></td> 
       <td></td> 
       <td class='searchSubmitButtons'><input type='submit' name='search' value='Search Records' /></td> 
       <td class='searchSubmitButtons'><input type='submit' name='search' value='List all database entries' /></td> 
       </tr> 
      </tfoot> 
      <tbody> 
       <tr> 
       <td>Make:</td> 
       <td><input type='text' name='vehiclemake' size='20' /></td> 
       <td>Name:</td> 
       <td><input type='text' name='ownername' size='25' /></td> 
       </tr> 

       <tr> 
       <td>Model:</td> 
       <td><input type='text' name='vehiclemodel' size='20' /></td> 
       <td>Address:</td> 
       <td><input type='text' name='owneraddress' size='48' /></td> 
       </tr> 
       <tr> 
       <td><label for='vehicleyear'>Year:</label></td> 
       <td><input type='text' name='vehicleyear' id='vehicleyear' size='10' /></td> 
       <td>Phone:</td> 
       <td><input type='text' name='ownerphone'/></td> 
       </tr> 
       <tr> 
       <td>Registration:</td> 
       <td><input type='text' name='vehiclerego' size='10' /></td> 
       </tr> 
       <tr> 
       <td>Km's:</td> 
       <td><input type = 'text' name='vehiclekms' size='10'/></td> 
       </tr> 
      </tbody> 
      </table> 
     </center> 
     </form>"; 
    } 




    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"<form action = '$self' method='POST'> 
    <table border='1' cellpadding='8'> 
      <tr> 
      <th>Vehicle ID</th> 
      <th>Vehicle Make</th> 
      <th>Vehicle Model</th> 
      <th>Vehicle Year</th> 
      <th>Vehicle Registration</th> 
      <th>Vehicle KM</th> 
      <th>Owner Name</th> 
      <th>Owner Phone</th> 
      <th>Delete</th> 
      </tr>"; 



    while($row = mysql_fetch_row($result)) 
    { 
     echo '<tr>'; 

     foreach($row as $field => $value) 
     {  
     if($field == 0) 
      $vehicle_ID=$value; 
     echo "<td>$value</td>"; 
     } 

     echo"<td><input type ='checkbox' name = deleteButton[] value='$vehicle_ID'></td>";  
     echo '</tr>'; 
    } 

    echo "</table> 
    <br><br> 

      <input type='submit' name='returnSearch' value='Return to Searching Records'>  



     <input type='submit' name='del' align='right' value='Delete Selected Records'> 

     </form>"; 

    } 
































?> 

对不起他们是大文件。林新在这里:(不知道如何发布大数量的代码。

干杯

+1

什么ü想要做的* owner_ID *删除? – diEcho

+2

你已经在函数文件中有'$ aOwner = mysql_insert_id();',你还想要什么? – Shef

+0

我真的不知道这个问题,但是如果你想获得输入到数据库的最后一个ID(它的自动增量与否),请使用sql查询以desc顺序获取数据并将其限制为一个 – ahoura

回答

1

您可以使用

SELECT LAST_INSERT_ID(); 

或PHP

mysql_query("INSERT ... "); 
$increment = mysql_insert_id(); 
-1

mysql reference

$result = mysql_query(SELECT from table_name LAST_INSERT_ID())"; 
echo $result; 

php reference

mysql_query("watever query"); 
echo mysql_insert_id(); 
+0

为什么投票下来?有没有人解释 – diEcho

2

你所回答自己的问题

在你的代码

$aOwner = mysql_insert_id(); //Assign variable to mySQL function, returns last known user id input, so as to determine auto_inc for owner_ID 

所以只打印$ aOwner

+0

正确!但是这段代码并没有将记录放在数据库中 –

+0

可以打印出$ aOwner并查看它是否显示表tblOwner的最新ID? – salahy

-1

您可以通过

$r = mysql_query("SHOW TABLE STATUS LIKE 'table_name' "); 
$row = mysql_fetch_array($r); 
$Auto_increment = $row['Auto_increment']; 
mysql_free_result($r); 
0

得到这个@genesis I相信你想显示下一个增量 owner_ID 用户知道,他/她的owner_ID是什么。

如果我是正确阅读下面的信息

1]从tblVehicle表retrive的owner_ID,
2]取最高数目,
3]递增1的最高数目,
4]然后去与插入码,

,我和上述

列出查询测试

从TABLE_NAME LAST_INSERT_ID()SELECT

SELECT LAST_INSERT_ID();

这两个查询没有解决,我只提供'0'值。

更好地尝试了代码插入

$insertVehicleQuery="INSERT INTO tblVehicle(make,model,year,rego,kms) VALUES ('$vehicleMake','$vehicleModel','$vehicleYear','$vehicleRego','$vehicleKms')"; 

刚刚从上面的查询