douhongxie5436 2014-08-14 05: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 05: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部