首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >有php问题

有php问题
EN

Stack Overflow用户
提问于 2018-12-11 01:16:41
回答 1查看 0关注 0票数 0

有一个PHP的问题我从书中复制的。

代码语言:javascript
复制
<?php // Script 13.7 - add_quote.php 
/* This script adds a quote. */ 

// Define a page title and include the header: 
define('TITLE', 'Add a Quote'); 
include('templates/header.html'); 

print '<h2>Add a Quotation</h2>'; 

// Restrict access to administrators only: 
if (!is_administrator()) { 
print '<h2>Access Denied!</h2><p class="error">You do not have permission to access this page.</p>'; 
include('templates/footer.html'); 
exit(); 
} 

// Check for a form submission: 
if ($_SERVER['REQUEST_METHOD'] =='POST') { // Handle the form. 

if ( !empty($_POST['quote']) && !empty($_POST['source']) ) { 

// Need the database connection: 
include('../mysqli_connect.php'); 

// Prepare the values for storing: 
$quote = mysqli_real_escape_string($dbc,trim(strip_tags($_POST['quote']))); 
$source = mysqli_real_escape_string($dbc,trim(strip_tags($_POST['source']))); 

// Create the "favorite" value: 
if (isset($_POST['favorite'])) { 
$favorite = 1; 
} else { 
$favorite = 0; 
} 

$query = "INSERT INTO quotes (quote, source, favorite) VALUES ('$quote', '$source', $favorite)"; 
mysqli_query($dbc, $query); 

if (mysqli_affected_rows($dbc) == 1){ 
// Print a message: 
print '<p>Your quotation has been stored.</p>'; 
} else { 
print '<p class="error">Could not store the quote because:<br>' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>'; 
} 

// Close the connection: 
mysqli_close($dbc); 

} else { // Failed to enter a quotation. 
print '<p class="error">Please enter a quotation and a source!</p>'; 
} 

} // End of submitted IF. 

// Leave PHP and display the form: 
?> 

<form action="add_quote.php" method="post"> 
<p><label>Quote <textarea name="quote" rows="5" cols="30"></textarea></label></p> 
<p><label>Source <input type="text" name="source"></label></p> 
<p><label>Is this a favorite? <input type="checkbox" name="favorite" value="yes"></label></p> 
<p><input type="submit" name="submit" value="Add This Quote!"></p> 
</form> 

<?php include('templates/footer.html'); ?

这给了我错误

代码语言:javascript
复制
Notice: Undefined variable: dbc in C:\xampp\htdocs\CH13\add_quote.php on line 26

警告:mysqli_real_escape_string()期望参数1为mysqli,在第26行的C:\ xampp \ htdocs \ CH13 \ add_quote.php中给出null

注意:未定义的变量:第27行的C:\ xampp \ htdocs \ CH13 \ add_quote.php中的dbc

警告:mysqli_real_escape_string()要求参数1为mysqli,在第27行的C:\ xampp \ htdocs \ CH13 \ add_quote.php中给出null

注意:未定义的变量:第37行的C:\ xampp \ htdocs \ CH13 \ add_quote.php中的dbc

警告:mysqli_query()期望参数1为mysqli,在第37行的C:\ xampp \ htdocs \ CH13 \ add_quote.php中给出null

注意:未定义的变量:第39行的C:\ xampp \ htdocs \ CH13 \ add_quote.php中的dbc

Warning: mysqli_affected_rows() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\CH13\add_quote.php on line 39

Notice: Undefined variable: dbc in C:\xampp\htdocs\CH13\add_quote.php on line 43

Warning: mysqli_error() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\CH13\add_quote.php on line 43 Could not store the quote because: .

The query being run was: INSERT INTO quotes (quote, source, favorite) VALUES ('', '', 0)

Notice: Undefined variable: dbc in C:\xampp\htdocs\CH13\add_quote.php on line 47

Warning: mysqli_close() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\CH13\add_quote.php on line 47

EDIT: seems it is a connection error. currently trying to fix the connection. atm seems nothing i try is working

EN

Stack Overflow用户

发布于 2018-12-11 10:59:03

$dbc似乎是由第include('../mysqli_connect.php');23行创建的。

由于第$dbc26行的第一次使用是null由运行时警告报告的,因此该include脚本要么无法实际连接到数据库,要么可能没有命名保存数据库连接资源的变量$dbc

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100006250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档