如何在列中插入多个出价值

时间:2014-04-26 04:22:57

标签: php mysql

我尝试在列'bidamount'中插入多个出价,例如来自-to。我已经为数据库编写了一些代码,并为多个出价做了一些代码,但是每当我插入3.1到8.1之类的值时,我会在这里找到。它插入一个值8.1和0.这个不是在数据库表中的bidamount列中插入3.1到8.1的所有值。

我在这里的新朋友,所以我没有弄清楚这些代码有什么问题。请帮助我。

我的代码:

<?php

$con = mysql_connect("localhost","root","");
if(!$con) {
    die('Could not connect: ' . mysql_error());
 }
 mysql_select_db("gunjanbid", $con) or DIE('Database name is not available!'); 

 if(isset($_POST['submit'])) {
     $m=$_POST['bidamount'];
     $n=$_POST['bidamount'];
     for($bidd=$m;$bidd<=$n;$bidd++)   
         $bidds=array($bidd);   
         $username=$_SESSION['userName'];
         $productid=$_GET['id'];

         $sql1="INSERT INTO bid(productid,description,closing_date,bidamount,userName)      values('$productid','$r',Now(),'$bidds','$username')";

         $result1=mysql_query($sql1);
         if($result1!=1) {
             echo "failure!";
          }
       }

   ?>

<form action="" name="auction1"   method="post" >
    <input type="hidden" name="description" value="">
    <input type="hidden" name="closing_date" value="">
    <input  type="text"  name="bidamount" value="" size="5">&nbsp;&nbsp;to&nbsp;&nbsp;
    <input  type="text" name="bidamount" value="" size="5" >
    <input type="submit" name="submit" class="button" value="Bid Now">
</form>

请帮帮我。我是php的新手。

2 个答案:

答案 0 :(得分:0)

I建议您使用此代码 $bid = explode("to",$_POST['bidamount']); $m = $bid[0]; $n = $bid[1]; 代替     $ m = $ _POST ['bidamount']; $ n = $ _POST ['bidamount'];

答案 1 :(得分:0)

首先:如果要提交多个具有相同名称的值,可以在名称后加上括号,如name="bidamount[]",PHP会将它们组合成一个数组。

其次,MySQL并不了解数组。它不喜欢在列中存储多个值。而且坦率地说,无论如何你都不想这样做。认真。它造成的麻烦比它值得多。

  • 离散值很难获得。因为没有&#34;数组&#34;在MySQL中键入,您最终必须解析字符串和其他此类垃圾以获取您的个人值。另一方面,从两列获取两个值是很多更简单。
  • MySQL无法帮助您保持数据有效。它所看到的只是一大堆字符/字节。对于个别作品,它无法做到这一点。例如,它不能强制执行唯一性。
  • 它使索引无用。一旦你必须解析每个字符串以便在其中找到东西,你几乎杀死了MySQL能够使用索引加速的任何机会query.P

如果您的两个值代表&#34;低&#34;和&#34;高&#34;,然后调用它们,并将它们存储为单独的字段。

如果他们只是两个任意数量,另一方面 - 特别是如果你预计有两个以上 - 那么他们应该成为另一个表中他们自己行的一部分。


至于代码:虽然它不是问题的一部分,但还有其他一些问题。

  • mysql_query已被弃用。(阅读:PHP的作者认为您不应该使用它。)停止放屁。有更好的方式与数据库交谈。

  • 您过分信任用户方式

    打开页面,然后进入浏览器的开发工具。找到隐藏的字段,并将描述更改为&#34; Joe的项目&#34;。提交表格,它应该打破。原因是SQL使用'作为引号。你的字符串中的一个会抛出引号并破坏SQL。

    这很糟糕,这可以意外地完成 - 但是有些人会故意 ,并且可以提供恰当的数据来诱骗服务器运行SQL它应该& #39;吨。这称为&#34; SQL注入&#34;,这可能是一个相当严重的安全问题。

    可以通过简单地从您的输入中删除'来解决此问题。但坦率地说,这几乎就像说“不要使用撇号一样半!”11!11&#34;。字符串中至少还有一个其他特殊字符。

如果您使用更现代的数据库扩展(如PDO),则可以一次解决前两个问题。 观看:

    <?php

    if ($_POST['submit']) {
        // By the way, you don't need to create the DB connection if you don't need to
        // mess with the DB.  :)
        $con = new PDO('mysql:host=localhost;dbname=gunjanbid', 'root', '');

        $low = $_POST['low'];
        $high = $_POST['high'];
        $id = $_GET['id'];
        $description = $_POST['description'];
        $user = $_SESSION['username'];

        // PDO has a `query` method that works much like `mysql_query`.  But that does
        // absolutely nothing to fix the SQL injection issue.
        //
        // Instead, use a prepared statement.  You can insert placeholders (?) for data,
        // and when you run the statement later with the real data, PDO and MySQL know which
        // stuff is data and which is SQL.  Since they're kept separate, the data won't have
        // a chance to be interpreted as part of the SQL.
        $stmt = $con->prepare('
            INSERT INTO bid (productid, description, closing_date, userName, low, high)
            VALUES (?, ?, NOW(), ?, ?, ?)
        ');

        if (!$stmt->execute([$id, $description, $user, $low, $high])) {
            echo 'Failure!';
        }
    }

    ?>
相关问题