SNidhi Posted April 16, 2020 Share Posted April 16, 2020 <?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? Quote Link to comment https://forums.phpfreaks.com/topic/310595-i-do-not-what-to-do-because-i-think-my-coding-is-right-please-help-me/ Share on other sites More sharing options...
Barand Posted April 16, 2020 Share Posted April 16, 2020 Because $result is not equal to 1. You can check what it contains with var_dump($result); Quote Link to comment https://forums.phpfreaks.com/topic/310595-i-do-not-what-to-do-because-i-think-my-coding-is-right-please-help-me/#findComment-1576934 Share on other sites More sharing options...
TrueMember Posted April 16, 2020 Share Posted April 16, 2020 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> 1 Quote Link to comment https://forums.phpfreaks.com/topic/310595-i-do-not-what-to-do-because-i-think-my-coding-is-right-please-help-me/#findComment-1576935 Share on other sites More sharing options...
Barand Posted April 16, 2020 Share Posted April 16, 2020 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'); . . . 1 Quote Link to comment https://forums.phpfreaks.com/topic/310595-i-do-not-what-to-do-because-i-think-my-coding-is-right-please-help-me/#findComment-1576936 Share on other sites More sharing options...
TrueMember Posted April 16, 2020 Share Posted April 16, 2020 44 minutes ago, Barand said: That suggestion will report some php errors. For all errors the settings need to be in the php.ini file, not the code. Yes, of course. 😃 Quote Link to comment https://forums.phpfreaks.com/topic/310595-i-do-not-what-to-do-because-i-think-my-coding-is-right-please-help-me/#findComment-1576937 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.