douhongxie5436 2014-08-14 13:04 采纳率: 100%
浏览 32
已采纳

从URL使用PHP到$ _GET [ID]

i am using simple code to get some data from DB based on some unique ID called VIN. i wrote a script which work fine if somebody insert it in form, but now i need to edit to work more automaticly, and use $_GET['vin'] from URL and just display results based on that.

My try of code looks like: public $vin = null;

 public function __construct( $data = array() ) {
     if( isset( $data['vin'] ) ) $this->vin = stripslashes( strip_tags( $data['vin'] ) );
 }

 public function storeFormValues( $params ) {
    $this->__construct( $params ); 
 }

 public function fetchByVinEvidence($vin) {
     $success = false;
     try{
        $con = new PDO( DB_HOST, DB_USER, DB_PASS ); 
        $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        $sql = "SELECT * FROM evidence_vin WHERE vin = :vin LIMIT 1";
        $stmt = $con->prepare( $sql );
        $stmt->bindValue( "vin", $this->vin, PDO::PARAM_STR );
        $stmt->execute();
            echo "<table>";
            echo "<th>First Registration</th>";
            echo "<th>Validity Until</th>";
            echo "<th>Rpm</th>";
            echo "<th>Max-Speed</th>";
            echo "<th>Action</th>";
            while ($row = $stmt->fetch()){
                echo "<tr>";
                echo "<td>24</td>";
                echo "<td>".$row['claim_number']."</td>";
                echo "<td>".$row['license']."</td>";
                echo "<td>".$row['country']."</td>";
                echo "<td>".$row['vin']."</td>";
                echo "</tr>";
            }
            echo "</table>" ;
            }catch(PDOExeption $e){
            echo $e->getMessage();
            echo $con->errorInfo();
            }

         return $success;
     }

and call the function:

$vin = $_GET['vin'];
echo $vin;
$data = new Data;
$data->fetchByVinEvidence($vin);

Can somebody help me with that?

  • 写回答

2条回答 默认 最新

  • doudun5009 2014-08-14 13:06
    关注

    You pass a variable $vin to the function fetchByVinEvidence but then use the class level variable $this->vin instead of the passed one.

    $stmt->bindValue( "vin", $this->vin, PDO::PARAM_STR );
    

    should be

    $stmt->bindValue( "vin", $vin, PDO::PARAM_STR );
    

    OR set the class level variable to the passed one at the start of the function if you need to use it elsehwere:

    public function fetchByVinEvidence($vin) {
         $this->vin = $vin;
         ....
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)