-5

好的,所以我正在尝试创建一个可用列以在输入相同的字段后递增,但它似乎不起作用,我已经尝试了很长时间。我知道代码一团糟,但请有人帮忙吗?

  $make = strtolower($_POST['make']);
  $fuel = $_POST['fuel'];
  $transmission = $_POST['transmission'];
  $size = $_POST['size'];
  $doors = $_POST['doors'];
  /*$date = time();*/

  date_default_timezone_set('Europe/London');
  $date = date('y-m-d');

  $connect = mysql_connect ("localhost", "root", "**********");
  mysql_select_db ("car");

  $query = "SELECT * FROM type WHERE make = '$make'";
  $result = mysql_query($query);
  $row = mysql_num_rows($result);
  $amount = $row+1;
  $id = $make."[".$amount."]";

  if (isset($_POST['submit'])){

      if ($make&&$fuel&&$transmission&&$size&&$doors)
      {
          /*if ($fuel!== $fueltype['0','1','2','3'])*/
          if ($fuel=='text')
          {
                $msg = ("Please enter a valid fuel type: Petrol, Diesel, LPG or Electric");
          }
          else
          if ($transmission=='text')
          {
              $msg = ('Please enter a valid transmission type: Manual, Auto or Semi-auto');

          }
          else
          if(strpos($size,".") == false){
             $msg = ('Please enter the valid engine size: 1.0, 1.4, 1.6 etc');
          }
          else
          if(!preg_match("/^[0-9]+$/",$doors)){
            $msg = ('Please enter the number of doors. 3, 5 etc');
          }
          else{
          //Creates the id
            $query = mysql_query("SELECT * FROM type");
      $numrows = mysql_num_rows($query);

      if ($numrows!=0){

          while ($row = mysql_fetch_assoc($query)){
            $dbid = $row["id"];
            $dbmake = $row["make"];
            $dbfuel = $row["fuel"];
            $dbtransmission = $row["transmission"];
            $dbsize = $row["size"];
            $dbdoors = $row["doors"];
          }

          if  ($make==$dbmake&&$fuel==$dbfuel&&$transmission==$dbtransmission&&$size==$dbsize&&$doors==$dbdoors){

            print_r($dbid);
            print_r($dbmake);
            print_r($dbfuel);
            print_r($dbtransmission);
            print_r($dbsize);
            print_r($dbdoors);
                    mysql_query("UPDATE type SET available=available+1 ON DUPLICATE KEY id = id ");
          }
          else{

            //increments the available column '$make'+'[#]'
          $query = "SELECT * FROM type WHERE id = '$id'";
          $result = mysql_query($query);
          $numrows = mysql_num_rows($result);
          $inc = $numrows+1;
          $available = $inc;


          $query = mysql_query ("INSERT INTO type VALUES ('$id', '$make', '$fuel', '$transmission', '$size', '$doors', '$date', '$available')");
          $msg = ("Car has been added to database. <a href='database.php'>View database?</a>");
          }
          }
          }
          } 
4

2 回答 2

2

这并没有解决您的问题,因为您的代码中有太多内容无法确定您的问题所在。

但是,您的代码容易受到 SQL 注入的攻击。如果您计划将其上传到人们可能使用的某种网站,请使用 PHP PDO 参数化或以其他方式清理您的所有输入。

于 2012-12-07T21:51:57.477 回答
0

如何将 SQL 语句更改为:

"UPDATE type SET available=available+1 where id='$dbid'"

我不确定查询中使用的 ON Duplicate 是否有效,您使用的是 id=id

于 2012-12-07T22:04:19.543 回答