Having php issues

143
December 12, 2018, at 01:50 AM

Having issues with a PHP i copied out of the book for an assignment.

<?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'); ?

which is giving me the error

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

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

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

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

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

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

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

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

Answer 1

$dbc would appear to be created by the include('../mysqli_connect.php'); on line 23.

Since the first usage of $dbc on line 26 is being reported as null by the runtime warning, then that include script is either failing to actually connect to the database, or perhaps the variable holding the database connection resource is not named $dbc.

Maybe add this to the end of mysqli_connect.php to help identify what goes wrong with the connection attempt:

if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
READ ALSO
PHP create xml file with a given XML format by dynamically inserting data to open in excel

PHP create xml file with a given XML format by dynamically inserting data to open in excel

I'm trying to create a XML file from PHP in a specified formatThe XML format given to me is not allowed to be posted on public pages so I'll explain how it is

105
Wordpress site loads in more then one minute and wpadmin panel works fine

Wordpress site loads in more then one minute and wpadmin panel works fine

I have got an issue where nginx is taking 2 minutes to load php files where admin panel works perfectlyEarlier it was showing 504 but i increase timeout limit so it started to load in 2 min

118
docker-CodeIgnitor file_get_contents failed to open stream: Connection refused

docker-CodeIgnitor file_get_contents failed to open stream: Connection refused

I'm new to the docker-code ignitor worldIssue is I cannot use file_get_contents inside the app as it keeps throwing the error:

115
Problem with file_get_contents or curl - blank page

Problem with file_get_contents or curl - blank page

Can anyone help me with fetching data from an external website?

99