Adding PHP Code
-
I’m encountering a fatal error when trying to add PHP code to my website
-
Hi, @aratc
Please tell us more. Where are you adding your code and what is that code.
I want to add it using elementor pro with plugin, is there another way to upload it?
And here is my code:<!DOCTYPE html> <html> <style type="text/css"> .input1 { background-color: #3498db; color: white; border-color: #3498db; width: 70px; } .input2 { width: 250px; } .input2::placeholder { text-align: right; } .table1, .table2 { border: 1px solid black; } .table1 { background-color: #3498db; } .table2 { background-color: #d9d9d9; text-align: center; } </style> <body> <form method="post"> <input class="input1" type="submit" name="submit" value="تحقق"> <input class="input2" type="text" name="test" placeholder="رقم الشهادة"> </form> </body> </html> <?php $con = new PDO("mysql:host=localhost;dbname=aratcnet_certificates",'root',''); if (isset($_POST["submit"])) { $int = $_POST['test']; $sth = $con->prepare("SELECT * FROM <code>new_table</code> WHERE sit_num = '$int'"); $sth->setFetchMode(PDO:: FETCH_OBJ); $sth -> execute(); if($row = $sth->fetch()) { ?> <br><br><br> <table> <tr> <th class="table1">Certificate No.</th> <th class="table1">Full Name</th> <th class="table1">Course Title</th> <th class="table1">Date Of Issue</th> <th class="table1">Trainer</th> </tr> <tr> <td class="table2"><?php echo $row->sit_num; ?></td> <td class="table2"><?php echo $row->candidate_name; ?></td> <td class="table2"><?php echo $row->course_name; ?></td> <td class="table2"><?php echo $row->date_of_issue; ?></td> <td class="table2"><?php echo $row->trainer; ?></td> </tr> </table> <?php } else { echo "رقم الشهادة غير صحيح، الرجاء التأكد من الرقم وإعادة المحاولة"; } } ?>-
This reply was modified 5 years, 3 months ago by
bcworkz. Reason: code fixed
I would suggest using https://wordpress.org/plugins/insert-php-code-snippet/ plugin.
If you feeel comfortable coding, you can create a child theme: https://developer.wordpress.org/themes/advanced-topics/child-themes/
Hope this helps!
Thank you for your reply, but I’ve already used this plugin and getting the same error, are there restrictions of php functions and can’t be used with wordpress?
Maybe the error message might help you fix that error. Errors like this are logged. Check the error log on your server. If you can’t find the log, please contact your host.
Meantime, enable wp_debug and wp_debug_log and after an error, look at wp-content/debug.log to see if anything gets logged there.
Read more here: https://wordpress.org/support/article/debugging-in-wordpress/
Your code appears to be a complete, stand alone .php webpage which does not utilize any WP resources. You could put it into its own .php file and upload to your server outside of WP. Link directly to the file instead of using WP permalinks.
In any case, the PHP output needs to occur before the closing
</body></html>tags. It’s not the cause of a fatal error, but as it is, it’s invalid HTML all the same.As written, your HTML code is incompatible with WordPress. But let’s assume you are able to rewrite the HTML to make it compatible.
Are the
PDOandPDO_mysqlextensions loaded in PHP? It makes zero sense to use PDO for a single, extremely-simple query, so if the extensions aren’t available, just use simplemysqli_functions.@bcworkz, Thank you for the reply. Do you mean uploading the file to the file manager of the cpanel? but after that how to include it in my page?
I don’t think you can include PHP code through Elementor. To include that file into a WP page, you’d need a to make a page using a custom page template which includes or requires the file. If you do that, the included file should not have html and body tags.
With that approach, upload the file (using file manager or FTP) to the WP folder of either a child theme or custom plugin. Or still external to WP if you prefer, anywhere accessible by the file system will work TBH.
-
This reply was modified 5 years, 3 months ago by
The topic ‘Adding PHP Code’ is closed to new replies.