Php计数器和子串连接

时间:2012-12-15 23:12:28

标签: php concatenation

我正在开发汽车租赁系统,用户可以将汽车添加到php脚本中,从而将值添加到后端数据库中。我有一个名为'ID'的字段,它是自动递增的INT字段。每次添加汽车时,该值都会自动添加为递增的整数。现在,我实际上想要的是不同的,例如,如果要添加的汽车名称是“MERCEDES”,则ID值应为MER001。如果添加的下一辆车是VAUXHALL,则ID值应为VAU002,依此类推。我知道如何使用php中的substring函数从mercedes中获取'MER',但我不知道如何使用循环计数器并将其与提取的子字符串连接起来。我相信我的数据库表中的ID字段必须是VARCHAR但我不知道VARCHAR是否可以自动递增(没有意义)。这是添加汽车的表格。            

将新车添加到数据库

     

请填写以下详细信息以注册

  <label> Car Name
  <span class="small">Enter car name</span>
  </label>
  <input type="text" name="CARNAME" id="CARNAME" />

  <label>Fuel Type
  <span class="small">Eg: Petrol</span>
  </label>
  <input type="text" name="FUELTYPE" id="FUELTYPE" />

  <label>Transmission
  <span class="small">Eg: Manuel</span>
  </label>
  <input type="text" name="TRANSMISSION" id="TRANSMISSION" />

  <label>Engine Size
  <span class="small">Eg: 2.4</span>
  </label>
  <input type="text" name="ENGINE_SIZE" id="ENGINE_SIZE" />

  <label>Doors
  <span class="small">Eg: 4</span>
  </label>
  <input type="text" name="DOORS" id="DOORS" />

  <label>Total
  <span class="small">Eg: 40</span>
  </label>
  <input type="text" name="TOTAL" id="TOTAL" />

  <label>Available
  <span class="small">Eg: 40</span>
  </label>
  <input type="text" name="AVAILABLE" id="AVAILABLE" />


  <input type="submit" name="submit" value="Add Car">
  <div class="spacer"></div>

  </form>

此表单将您带到add.php,其中包含以下代码:

<?php
$link = mysql_connect ("xxxxx", "xxxxx", "xxxxx");
mysql_select_db ("xxxxx");

$ID = $_POST['ID'];
$CARNAME = $_POST['CARNAME'];
$FUELTYPE = $_POST['FUELTYPE'];
$TRANSMISSION = $_POST['TRANSMISSION'];
$ENGINE_SIZE = $_POST['ENGINE_SIZE'];
$DOORS = $_POST['DOORS'];
$TOTAL = $_POST['TOTAL'];
$AVAILABLE = $_POST['AVAILABLE'];
$DATEADDED = $_POST['DATEADDED'];
$test = substr($CARNAME,0,3); //tried to use this test variable and it works to get the substring!

if($TOTAL>=$AVAILABLE)
{     
$query = "insert into car (ID,CARNAME,FUELTYPE,TRANSMISSION,ENGINE_SIZE,DOORS,TOTAL,AVAILABLE,DATEADDED) values ('$_POST[ID]','$_POST[CARNAME]','$_POST[FUELTYPE]','$_POST[TRANSMISSION]','$_POST[ENGINE_SIZE]','$_POST[DOORS]','$_POST[TOTAL]','$_POST[AVAILABLE]',CURDATE())";
$result = mysql_query($query);
header("location: list_logged.php");
}
else
{
echo "<script>alert('The Total number of cars cannot be less than Available number!'); location.href='add.php';</script>"; 
}
mysql_close ($link);

?>

有关如何将内容发布到ID以使其像MER001,MAC002等等的任何建议吗?现在它只是设置为自动递增值,它会自动添加。我知道我必须将数据库中的字段从ID更改为VARCHAR,但我不知道下一步该怎么做。我尝试在substr和php计数器连接上搜索类似的问题,但没有发现任何有用的东西。

1 个答案:

答案 0 :(得分:0)

花了一些时间才弄明白。你应该真的使用PDO。您的数据库也将如下所示:

ID,CARNAME,FUELTYPE,TRANSMISSION,ENGINE_SIZE,DOORS,TOTAL,AVAILABLE和DATEADDED(这是一种日期数据类型。休息是所有变量)。

还有一件事。您可以使用NOW()代替使用CURDATE(),它提供插入数据库的日期和时间。

<?php
$link = mysql_connect ("***", "****", "*******");
mysql_select_db ("*****");

$CARNAME = $_POST['CARNAME'];
$FUELTYPE = $_POST['FUELTYPE'];
$TRANSMISSION = $_POST['TRANSMISSION'];
$ENGINE_SIZE = $_POST['ENGINE_SIZE'];
$DOORS = $_POST['DOORS'];
$TOTAL = $_POST['TOTAL'];
$AVAILABLE = $_POST['AVAILABLE'];

    //FUNCTION TO GET LAST ID FROM DB
    function getLastID(){ //Function to get last ID created and return
        $query = "SELECT ID FROM car ORDER BY DATEADDED DESC LIMIT 1;";
        $lastID = mysql_query($query);
        $results = mysql_fetch_assoc($lastID);
        return $results; //returns last id
    }

    //SWITCH CASE TO CREATE PREPENDING CARNAME
    switch ($CARNAME) { //PreString based off of form input
        case 'MERCEDES':
        $preString = 'MER';
            break;
        case 'VAUXHALL':
        $preString = 'VAU';
            break;
        default:
        $preString = 'NONE';
            break;
    }
    //CREATING NEW INDEX
    $newID = getLastID(); // Get Newest ID from the database
    $castNewID = (String) $newID['ID']; //Cast ID to String
    $last3chars = substr($castNewID, -3);  //Get last three characters of ID
    $newIndexNumber = (int) $last3chars + 1; // Cast last3chars to int and add one
    $castToString = str_pad((String) $newIndexNumber, 3, "0", STR_PAD_LEFT); //Append zeros to string if we haven't reached 100 yet and cast back into a string.
    $newCarID = (String) $preString . (String) $castToString; //Finally create new ID concatenation

    //var_dump($newCarID); //For debugging

    //INSERT INTO DB
    if($TOTAL>=$AVAILABLE)
    {     
        $query = "insert into car (ID,CARNAME,FUELTYPE,TRANSMISSION,ENGINE_SIZE,DOORS,TOTAL,AVAILABLE,DATEADDED) 

                    values ('$newCarID', '$CARNAME', '$FUELTYPE','$TRANSMISSION','$ENGINE_SIZE','$DOORS','$TOTAL','$AVAILABLE',NOW())";

        $result = mysql_query($query);

        header("location: list_logged.php");
    }
    else
    {
        echo "<script>alert('The Total number of cars cannot be less than Available number!'); location.href='add.php';</script>"; 
    }
    mysql_close ($link);


?>