Jump to content

I do not what to do because i think my coding is right please help me.


SNidhi

Recommended Posts

    <?php

$Title=$_POST['title'];
$Price=$_POST['price'];
$Author=$_POST['author'];
$con=mysqli_connect('localhost','root') or die("not connected");
mysqli_select_db($con,'BRM_DB');
$query="INSERT INTO book(title,Price,Author)values('$Title',$Price,'$Author')";
$result=mysqli_query($con,$query);
mysqli_close($con);
    ?>
<!DOCTYPE html>
<html>
<head>
<title>Insertion</title>
</head>
<body>
<h1>Book Record Management</h1>
<p>
<?php
if($result==1)
    {
    echo ("Record inserted");
    }
    else 
    { 
    echo ("insertion failed");
    }
    ?>
</p>
<a href="insertform.php">click here</a>
</body>
</html>

why it gives "insertion failed" as a result?

 

 

Link to comment
Share on other sites

Enable errors and check what's wrong. Print the query and execute directly on database.

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$Title = $_POST['title'];
$Price = $_POST['price'];
$Author = $_POST['author'];
$con = mysqli_connect('localhost', 'root') or die("not connected");
mysqli_select_db($con, 'BRM_DB');
if (!$con) {
    echo "ERROR: " . mysqli_connect_errno() . PHP_EOL;
    echo "ERROR: " . mysqli_connect_error() . PHP_EOL;
    die;
}
$query = "INSERT INTO book(title,Price,Author)values('$Title',$Price,'$Author')";
$result = mysqli_query($con, $query);
mysqli_close($con);
?>
<!DOCTYPE html>
<html>
<head>
<title>Insertion</title>
</head>
<body>
<h1>Book Record Management</h1>
<p>
<?php
if ($result == 1) {
	echo ("Record inserted");
} else {
	echo ("insertion failed");
}
?>
</p>
<a href="insertform.php">click here</a>
</body>
</html>
  • Like 1
Link to comment
Share on other sites

That suggestion will report some php errors. For all errors the settings need to be in the php.ini file, not the code.

Also it won't inform you of mysqli errors

$Title=$_POST['title'];
$Price=$_POST['price'];
$Author=$_POST['author'];

mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);                        // ADD THIS LINE
$con=mysqli_connect('localhost','root') or die("not connected");
mysqli_select_db($con,'BRM_DB');

  . . . 

 

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.