Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 04/16/2020 in all areas

  1. 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 point
  2. 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 point
  3. Excellent question, with great answer from Kicken. As a rule of thumb this is a way to allow for optional parameters, so to directly answer your question, typically you just have the parameter and not a default value. More importantly PHP now has support for scalar type hinting, which is an important upgrade in 7.1. It had class type hinting since php 5. //PHP5 class typehint class Student { private firstName; private lastName; public function __construct($firstName, $lastName) { $this->firstName = $firstName; $this->lastName = $lastName; } public function getFirstName() { return $this->firstName; } public function getLastName() { return $this->lastName; } } class SchoolClass { private $name; private $year; private $students = array(); public function __construct($name, $year) { $this->name = $name; $this->year = $year; } public function enrollStudent(Student $student) { $this->students[] = $student; } public function listStudents() { $output = ''; foreach($this->students as $student) { $output .= $student->getLastName() . ', ' . $student->getFirstName() . PHP_EOL; } return $output; } } // Example $historyClass = new SchoolClass('AP History', 2020); $student = new Student('Mike', 'Smith'); $historyClass->enrollStudent($student); // This will throw a Catchable fatal error $historyClass->enrollStudent(array('Bob', 'Jones')); With PHP 7, you actually have scalar type hinting. So for example <?php declare(strict_types=1); // has to be 1st line of file class SchoolClass { private $name; private $year; private $students = array(); public function __construct(string $name, integer $year) { $this->name = $name; $this->year = $year; } } // Ok $geometry = new SchoolClass('Geometry 1', 2020); // This produces an error because $year must be an integer value and not a string $bio = new SchoolClass('Biology 1', '2020-02-01'); The declare(strict_types=1) call turns on strict mode, so that type coercion won't happen on parameters. Without it, passing parameters like this won't cause an error. It's up to you as to whether you want the types to be strictly checked or not. Without strict_mode, this would work. $bio = new SchoolClass('Biology 1', '2020'); This is a nice feature as well: class SchoolClass { /* assume previous definition */ public function listStudents(): string { $output = ''; foreach($this->students as $student) { $output .= $student->getLastName() . ', ' . $student->getFirstName() . PHP_EOL; } return $output; } } This hints that the return value of listStudents() has to be string, which can help with autosense in editors, and will throw a runtime error if something malfunctions such that $obj->listStudents() either doesn't return a string as it is suppossed to, or if the function is used as a input to a function that expects a parameter that is NOT a string. These seem like simple examples in a small project, but as a project grows in size and complexity, these small improvements can help you catch logic errors sooner than later and also make smart editors better at their job of helping you code using an extensive class library.
    1 point
  4. Defining a value in the parameter list makes that parameter optional. If it's not provided when the function is called, the it takes on the value assigned to it. Your specific example doesn't really make use of the feature effectively. Take something like this for example though: function findFiles($directory, $includeHidden = false){ $iter = new DirectoryIterator($directory); $list = []; foreach ($iter as $item){ if ($item->isFile()){ $isHidden = $item->getFilename()[0] === '.'; if ($includeHidden || !$isHidden){ $list[] = $item->getPathname(); } } } return $list; } That function requires at least one parameter when it's called, the directory to search. So you end up with the following options for calling it $files = findFiles('/home/kicken'); /* executes with $directory = '/home/kicken', $includeHidden = false */ $files = findFiles('/home/aoeex', true); /* executes with $directory = '/home/aoeex', $includeHidden = true */
    1 point
  5. You could do in exactly the same way that I showed you in this earlier post (if you actually read it)
    1 point
This leaderboard is set to New York/GMT-04:00
×
×
  • 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.