无法更新行mysql数据库

时间:2012-12-07 21:47:09

标签: php mysql

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

  $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>");
          }
          }
          }
          } 

2 个答案:

答案 0 :(得分:2)

这不是解决您的问题,因为您的代码中有太多的内容可以确定您的问题所在。

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

答案 1 :(得分:0)

如何将SQL语句更改为:

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

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