Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PHP and SQL / for this code below please, I need to add code on it that checks for the database table, if exist add

PHP and SQL / for this code below please, I need to add code on it that checks for the database table, if exist add the data and if it doesn't exist, echo an error and die or exit.

Book Tilile

ini_set ( 'display_errors', 1 );

error_reporting ( E_ALL | E_STRICT );

?>

//PDO database connection

$host='localhost';

$db = 'userinfo';

$username = 'root';

$password = '';

$dsn= "mysql:host=$host;dbname=$db";

try{

// create a PDO connection with the configuration data

$conn = new PDO($dsn, $username, $password);

//check for the database connection established

if($conn){

//echo "Connected to the $db database successfully!";

$bookTitle = $describtion = $price = $category = "";

$bookTitleError = $describtionError = $priceError = $categoryError = "";

$okay = true;

//submit button clicked

if (isset ( $_GET ['btnSubmit'] )) {

//bookTitle

if (empty ( $_GET ['bookTitle'] )) {

$bookTitleError = 'Please, Book title should be filled!';

$okay = FALSE;

} else {

$bookTitle = strip_tags ( ucfirst ( trim ( $_GET ['bookTitle'] ) ) );

}

//describtion

if (empty ( $_GET ['describtion'] )) {

$describtionError = 'Please provide a description!';

$okay = FALSE;

} else {

$describtion = strip_tags ( ucfirst ( trim ( $_GET ['describtion'] ) ) );

}

//price

$price = strip_tags ( ucfirst ( trim ( $_GET ['price']) ) );

if (! is_numeric ( $price )) {

$priceError = "Price should be a positive number please, " . $price . ", is not valid.";

$okay = FALSE;

}

//category

if (empty ( $_GET ['category'] )) {

$categoryError = 'Please provide a category!';

$okay = FALSE;

} else {

$category = strip_tags ( ucfirst ( trim ( $_GET ['category'] ) ) );

}

if($_GET['price'] <= 0) {

$priceError = "Price should be a positive number please, " . $price . ", is not valid.";

$okay = FALSE;

}

if (! $okay) { //check for all the value enter correctly

print "

*** You have errors in your data ***

";

// exit;

}

else{

//set attribute

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line

//sql query to

$sql = "INSERT INTO book (Title, Description, Price, Category) VALUES ('$bookTitle', '$describtion', $price, '$category')";

if ($conn->query($sql)) { //query to insert the data to the databases

echo ""; //alert data inserted successfully

//header('Location: assignment1.php');

}

else{

echo ""; //alert data not inserted

}

$dbh = null; //close the database

}

}

}

}catch (PDOException $e){

// report error message

echo $e->getMessage();

}

?>

if (isset ( $_GET ['btnSubmit'] )) {

echo ("value = \"" . $bookTitle . "\" ");

}

?> /> *

*

if (isset ( $_GET ['btnSubmit'] )) {

echo ("value = \"" . $price . "\" ");

}

?> /> *

*

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Beranek Corp has $720,000 of assets, and it uses no debt

Answered: 1 week ago