0

我正在开发一个汽车租赁系统,用户可以在其中将汽车添加到 php 脚本中,从而将值添加到后端 dababase 中。我有一个名为“ID”的字段,它是自动递增的 INT 字段。每次我添加汽车时,该值都会自动添加为递增的 Integer。现在,我实际上希望它是不同的,例如,如果要添加的汽车的名称是“MERCEDES”,则 ID 值应该是 MER001。如果随后添加的下一辆车是 VAUXHALL,则 ID 值应为 VAU002,依此类推。我知道如何使用 php 中的子字符串函数从梅赛德斯中获取“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>

此表单将您带到包含以下代码的添加.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 counter concatenation 上搜索类似的问题,但没有发现任何用处。

4

1 回答 1

0

花了一些时间想通了。不过,您确实应该使用 PDO。您的数据库也将如下所示:

ID、CARNAME、FUELTYPE、TRANSMISSION、ENGINE_SIZE、DOORS、TOTAL、AVAILABLE 和 DATEADDED(这是一个日期时间数据类型。其余都是 VARCHARS)。

还有一件事。而不是使用 CURDATE() 您使用 NOW() 它给出了插入数据库的日期和时间。

<?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);


?>
于 2012-12-16T00:51:58.807 回答