(Solved) : Php Sql Code Please Need Add Code Checks Database Table Exist Add Data Doesn T Exist Echo Q43997135 . . .
PHP and SQL / for this code below please, I need to add code onit that checks for the database table, if exist add the data and ifit doesn’t exist, echo an error and die or exit.
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title>Book Tilile</title>
<meta name=”description” content=”library”>
<meta name=”author” content=”5654.”>
<link rel=”stylesheet” href=”hjg.css”>
<?php
ini_set ( ‘display_errors’, 1 );
error_reporting ( E_ALL | E_STRICT );
?>
</head>
<body>
<?php
//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 <strong>$db</strong>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 “<h1><span class=”error”> *** You haveerrors in your data *** </span><h1>”;
// 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 thedatabases
echo “<script type= ‘text/javascript’>alert(‘your dataadded successfully’);</script>”; //alert data insertedsuccessfully
//header(‘Location: assignment1.php’);
}
else{
echo “<script type= ‘text/javascript’>alert(‘Data notsuccessfully Inserted.’);</script>”; //alert data notinserted
}
$dbh = null; //close the database
}
}
}
}catch (PDOException $e){
// report error message
echo $e->getMessage();
}
?>
<form method=”get” name=”frmSticky” >
<p>
<label>Book Title: </label> <input type=”text”name=”bookTitle” size=”20″
<?php
if (isset ( $_GET [‘btnSubmit’] )) {
echo (“value = “” . $bookTitle . “” “);
}
?> /><span class=”error”> * <?php echo$bookTitleError;?></span>
</p>
<p>
<label> Describtion: </label> <textareaname=”describtion”>
<?php
if (isset ( $_GET [‘btnSubmit’] )) {
echo “$describtion “;
}
?></textarea>
<span class=”error”> * <?php echo$describtionError;?> </span>
</p>
<p>
<label> Price: </label> <input name=”price”
<?php
if (isset ( $_GET [‘btnSubmit’] )) {
echo (“value = “” . $price . “” “);
}
?> /><span class=”error”> * <?php echo$priceError;?></span>
</p>
<label> Category: </label> <selectname=”category”>
<option> PHP database </option>
<option> Javascribt </option>
<option> Paython </option>
<?php
if (isset ( $_GET [‘btnSubmit’] )) {
echo (“value = “” . $category . “” “);
}
?> </select><span class=”error”> * <?php echo$categoryError;?></span>
<br><br>
<input type=”submit” name=”btnSubmit” value=”Submit”id=”submitButton”>
<button onclick=”window.location.href=’assignment1.php'”id=”reloadButton”>Reload
Form</button>
<br> <br>
</form>
Expert Answer
Answer to 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 …
OR