gpt4 book ai didi

php - 使用 PHP 获取数据 : Pull data

转载 作者:行者123 更新时间:2023-11-29 17:59:43 25 4
gpt4 key购买 nike

我是 PHP 编程的新手。我正在尝试让我的代码正常工作。我是一名教师,我正在尝试创建一个数据库来帮助跟踪我的学生损坏的笔记本电脑。我有两个不同的网页 index.php 和 connect.php (设置与我的“sterling”数据库的连接。我希望能够输入学生的 ID# 并获取学生的信息,例如:要在页面上填充的名字/姓氏/学生 ID/年级/照片。我猜它类似于Excel中的vlookup,将从“学生”表中提取学生数据。到目前为止,我还没有在数据库中输入任何图片,但稍后会输入。我已经看过在线教程,但我似乎无法将我所看到的内容与我的编程结合起来。非常感谢任何帮助。

index.php

<html>
<head>

<link rel="shortcut icon" href="img/favicon.ico"/>
<style>
form {font-family: fantasy !important; position:fixed; top:60%; left:25%;}
label{display:inline-block;width: 400px; margin-bottom: 50px; font-size: 30px; color: green;}
input[type=text] {width: 50%; font-size: 220%; color: black; box-sizing: border-box;
border: 3px solid #555; background-color: white; -webkit-transition: 0.5s; transition: 0.5s; outline: none;}

input[type=text]:focus {border: 3px solid #F2694C; background-color: #F2694C; background-image: url('/img/searchicon.png'); background-position: 330px 10px; background-repeat: no-repeat;}
input[type=submit]:focus {top: 30px;}
h1 {font-family: century gothic; text-align: center; font-size: 100px; color: red; position: relative; top: 50px}
img {position: relative; top: -50px; left: 870px;}
</style>


<title>Student Damage Report</title>
</head>
<body background="/img/back.jpg">
<h1>STUDENT DAMAGE REPORT</h1>

<br><br><br>

<img src="/img/id.png" />
<form method="post" action="connect.php">
<label>Please Enter Student ID# </label><input type="text" name="student_id" />
<br>

<center>
<input type="submit" value="SUBMIT">
</form>


</center>


</body>
</html>

Database Structure

更新这是我正在使用的 php 代码。这是来 self 的 Connect.php。

<?php
$connection=mysqli_connect('localhost','username','','sterling');
$db = mysqli_select_db("sterling", $connection); // Selecting Database


if(mysqli_connect_errno($connection))
{
echo 'Failed to connect';
}



//MySQL Query to read data
$query = mysqli_query("select * from students", $connection);
while ($row = mysqli_fetch_array($query)) {
echo "<b>{$row['first_name']}</b>";
echo "<br />";
}


?>


Database Definition
Database: `sterling`
--

-- --------------------------------------------------------

--
-- Table structure for table `damage`
--

CREATE TABLE `damage` (
`id` int(11) NOT NULL,
`first_name` text NOT NULL,
`last_name` text NOT NULL,
`department` text NOT NULL,
`email` varchar(320) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `employee`
--

CREATE TABLE `employee` (
`id` int(11) NOT NULL,
`first_name` varchar(30) NOT NULL,
`last_name` varchar(11) NOT NULL,
`email` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `employee`
--

INSERT INTO `employee` (`id`, `first_name`, `last_name`, `email`) VALUES
(12345, 'Henry Smith', '300', ''),
(12345, 'James', 'Maackey', 'james_mackey@gmail.com');

-- --------------------------------------------------------

--
-- Table structure for table `students`
--

CREATE TABLE `students` (
`FIRST_NAME` varchar(50) DEFAULT NULL,
`LAST_NAME` varchar(50) DEFAULT NULL,
`STUDENT_ID` int(7) NOT NULL,
`GRADE` int(2) DEFAULT NULL,
`ASSET` varchar(7) DEFAULT NULL,
`SERIAL` int(11) DEFAULT NULL,
`PIC` blob
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `students`
--

INSERT INTO `students` (`FIRST_NAME`, `LAST_NAME`, `STUDENT_ID`, `GRADE`, `ASSET`, `SERIAL`, `PIC`) VALUES
('James', 'Smith', 123456, 9, '126785', 90210, NULL),
('Tim', 'Robbins', 789654, 12, '456985', 3578951, NULL);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `damage`
--
ALTER TABLE `damage`
ADD PRIMARY KEY (`id`);

--
-- Indexes for table `students`
--
ALTER TABLE `students`
ADD PRIMARY KEY (`STUDENT_ID`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `damage`
--
ALTER TABLE `damage`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

这是图片 Question

<html>
<head>
<link rel="shortcut icon" href="img/favicon.ico"/>
<style>
form {font-family: fantasy !important; position:fixed; top:60%; left:25%;}
label{display:inline-block;width: 400px; margin-bottom: 50px; font-size: 30px; color: green;}
input[type=text] {width: 50%; font-size: 220%; color: black; box-sizing: border-box;
border: 3px solid #555; background-color: white; -webkit-transition: 0.5s; transition: 0.5s; outline: none;}

input[type=text]:focus {border: 3px solid #F2694C; background-color: #F2694C; background-image: url('/img/searchicon.png'); background-position: 330px 10px; background-repeat: no-repeat;}
input[type=submit]:focus {top: 30px;}
h1 {font-family: century gothic; text-align: center; font-size: 100px; color: red; position: relative; top: 50px}
img {position: relative; top: -50px; left: 870px;}
</style>


<title>Student Damage Report</title>
</head>
<body background="/img/back.jpg">
<h1>STUDENT DAMAGE REPORT</h1>

<br><br><br>

<img src="/img/id.png" />
<form method="post" action="index.php"
<label>Please Enter Student ID# </label><input type="text" name="student_id" />
<br>

<center>
<input type="submit" value="SUBMIT">
</form>






</center>




<?php
$pdo = null;
$studentId = $_POST['student_id'] ?? null; // handle case where we don't get a student id
try {
$host = 'localhost';
$db = 'sterling';
$dsn = "mysql:dbname={$db};host={$host}";
$user = 'user';
$pass = '';
$pdo = new PDO($dsn, $user, $pass);

}
catch(PDOException $e) {
// Connection failed
echo $e->getMessage();
exit();
}

// if we have a student id
if ( $studentId ) {
// add a where clause
$sql = 'SELECT * FROM `students` WHERE `studentId` = ?';
$stmt = $pdo->prepare($sql);
$stmt->bindParam(1, $studentId, PDO::PARAM_INT);
$stmt->execute();
$student = $stmt->fetch(PDO::FETCH_ASSOC);

} else {
$student = null;
}
?>

<?php if ( $student ) :?>
<div>

<div>Student Id <?= $student['studentId']?></div>
<div>First Name <?= $student['first_name']?></div>

</div>

<?php else : ?>
<div>Student <?= $studentId ?> not found</div>
<?php endif?>











</body>










</html>

最佳答案

我没有完全实现您的所有要求。这是一个工作示例,您可以使用它来让您的解决方案正常工作。

假设下表:

CREATE TABLE `student` (
`studentId` int(11) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) DEFAULT NULL,
`last_name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`studentId`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

有了这些数据:

INSERT INTO `student` (`studentId`, `first_name`, `last_name`)
VALUES
(1, 'Spencer', 'Emison'),
(2, 'Brian', 'Caldwell'),
(3, 'Jamie', 'Scott');

这里是根据在表单上输入学生 ID 来按 ID 查找学生的代码。我使用 PDO 是因为我认为它可以使代码更简洁。

<?php
$pdo = null;
$studentId = $_POST['student_id'] ?? null; // handle case where we don't get a student id
try {
$host = '127.0.0.1';
$db = 'sss';
$dsn = "mysql:dbname={$db};host={$host}";
$user = 'app';
$pass = 'aaaa';
$pdo = new PDO($dsn, $user, $pass);

}
catch(PDOException $e) {
// Connection failed
echo $e->getMessage();
exit();
}

// if we have a student id
if ( $studentId ) {
$isSearch = true;
// add a where clause
$sql = 'SELECT * FROM `student` WHERE `studentId` = ?';
$stmt = $pdo->prepare($sql);
$stmt->bindParam(1, $studentId, PDO::PARAM_INT);
$stmt->execute();
$student = $stmt->fetch(PDO::FETCH_ASSOC);

} else {
$student = null;
$isSearch = false;
}
?>

<?php if ( $student ) :?>
<div>

<div>Student Id <?= $student['studentId']?></div>
<div>First Name <?= $student['first_name']?></div>

</div>
<?php elseif ( $isSearch ) : ?>
<div>Student <?= $studentId ?> not found</div>
<?php endif?>

以下是列出所有学生的方法:

<div><h2>All Students</h2></div>
<?php
$sql = 'SELECT * FROM `student` ';
$stmt = $pdo->prepare($sql);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) : ?>
<div><?=$row['studentId']?> <?=$row['first_name']?></div>
<?php endwhile?>

如果您需要 PHP 信息:

信息.php

<?php
phpinfo();

然后转到http://localhost/info.php

关于php - 使用 PHP 获取数据 : Pull data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48490842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com