dongyinzheng6572 2015-01-13 19:37
浏览 166
已采纳

使用PDO从MySQL数据库中获取单个结果

Okay, for starters, I have a Table in MySQL with the columns "barcode","description", and "quantity". I have two pages, 1 HTML and 1 PHP. The HTML has a form in it to accept a barcode scan, or manual text entry. Based on that entry, I want it to return a row, starting with the barcode that was entered in on the HTML form. So far, I can enter anything and it will return the whole table.

The HTML looks like this:

<head>
<font color="00CC00">
<title>Search</title>
</head>

<body bgcolor="000000">

<h2>Find Product</h2>

<form name="search" method="post" action="Barcode Search.php">
<input type="text" name="find" placeholder="Click to Scan" /> 


<input type="submit" name="search" value="Submit" />
</form>

</body>

</html>

Short, sweet and to the point! The .PHP look like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<title>Inventory List</title>

<body bgcolor="000000">
<font color="00cc00">

 <?php 
//Table
echo "<table style='border: solid 1px green;'>";
echo "<tr><th>Barcode</th><th>Description</th><th>Quantity</th></tr>";

class TableRows extends RecursiveIteratorIterator {
    function __construct($it) {
        parent::__construct($it, self::LEAVES_ONLY);
    }

    function current() {
        return "<td style='width:150px;border:1px solid green;'>" . parent::current(). "</td>";
    }

    function beginChildren() {
        echo "<tr>";
    }

    function endChildren() {
        echo "</tr>" . "
";
    }
} 
//Connection Info
$servername = "127.0.0.1";
$username = "XXXX";
$password = "XXXXX";
$dbname = "test";


//Connection Started, Data pulled
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT * FROM inventory ");
    $stmt->execute();

// set the resulting array to associative
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
        echo $v;
    }
}

    //Error Check

catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}
// Take Text entry and fetch the SQL Row


//Kill Connection 
$conn = null;
echo "</table>";
?> 
</center>
</body>
</html>

I'd very much appreciate a point in the right direction! Thanks in advance!

  • 写回答

2条回答 默认 最新

  • duandingcu7010 2015-01-13 19:50
    关注

    SELECT * FROM inventory selects everything from the inventory table, no refinements or limitations. I'm guessing you want something like this:

    $stmt = $conn->prepare("SELECT * FROM inventory WHERE barcode = ? LIMIT 1");
    $stmt->bindParam("s", $_POST["find"]); // 's' means it's a string
    $stmt->execute();
    

    http://php.net/manual/en/mysqli.prepare.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误