-
Posts
6,906 -
Joined
-
Last visited
-
Days Won
99
Everything posted by ginerjm
-
if ($dbname == null) $host="mysql:host=localhost;charset=utf8"; else $host="mysql:host=localhost;dbname=$dbname;charset=utf8"; $options = array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_FOUND_ROWS => true, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC); $pdo = new PDO($host, $uid, $pswd, $options); Where $dbname is passed into the function that I wrap all of this in. I'll leave you to figure out the rest.
-
Effectively replace the old mysql_result without drastic changes.
ginerjm replied to samet5's topic in PHP Coding Help
Agree 99% with mac_gyver. I like using the named place-holders in queries and then an array of those named values with their true values for the call to execute as in: $q = "select fld1, fld2, fld3 where id=:id and age=:age"; $qst = $pdo->prepare($q); $parms = array( ':id'=>$currid, ':age'=>$curr_age ); if(!$qst->execute($parms)) { (handle the failure of the execute) } // continue on with a valid query result Using named parms makes more sense when looking at the code weeks or months later as well as they help when modifying your code and not having to figure out those ?? later. PS - when building and using prepared queries here is the idea behind them. One writes a query statement, as in $q above, and then prepares it which readies the sql code for later execution. Now that prepared statement, represented above by $qst, can be run multiple times b simply altering the parms array and calling execute. Not something that is done all the time but when the need arises it is more efficient. Of course most of the time you are just doing it to protect your database from hackers providing bad input values and your code not properly validating them. You should do that validating but this way the prepare process goes further to ensure that you don't end up running a dangerous query statement. -
Unable to get two PHP_SELF working on the same page
ginerjm replied to anandi's topic in PHP Coding Help
Take a look at what your code REALLY looks like when you stop entering and leaving PHP mode. Stick to PHP mode and use the echo statement (or learn about heredocs) to output your non-PHP code lines. <?PHP header("Cache-Control: public"); include "header.php"; //db info for DB2 include "logl.php"; if ($_POST['submit']) { $type = $_POST["type"]; $cgid = $_POST["cgid"]; $comp = $_POST["comp"]; if ($type == "UG") { if(isset($_POST) && !empty($_POST["group2"])) { // this value does NOT get passed $group2 = $_POST["group2"]; $cgid = $_POST["cgid"]; $sqla = "UPDATE `Cgroup` SET `Group ` = '$group2' WHERE CGID = '$cgid' "; $resulta = mysqli_query($link1, $sqla); // this sql doesnt run. The value of $group2 doesn't get passed into the sql; } else { echo "<form method='POST' action='{$_SERVER['PHP_SELF']}'>"; // semi not colon $sqla = "SELECT * FROM `Cgroup` WHERE CGID = $cgid "; $resulta = mysqli_query($link1,$sqla); $myrowa=mysqli_fetch_array($resulta); echo "<table border='1' cellspacing='1' style='border-collapse: collapse;' bordercolor='#000066' width='95%' cellpadding='5'> <tr> <td colspan='3' bgcolor='#B5CBEF' height='16' width='100%' bordercolor='#FFFFFF' > <font size='1' face='Calibri'> <b> <font face='Calibri' size='2' color='#000066'> </font> </b> </td> </tr> <tr> <td colspan='4' bgcolor='#D6DFEF' height='16' width='100%' bordercolor='#FFFFFF'> <tr> <td height='30' width='55' bgcolor='#EFF3F7' bordercolor='#FFFFFF'> <font face='Calibri' size='2' color='#004080' > Old Group : <input type='text' name='group1' size=50 value='{$myrowa[\"Group\"]}> </td> </tr> <tr> <td height='30' width='55' bgcolor='#EFF3F7' bordercolor='#FFFFFF'> <font face='Calibri' size='2' color='#004080' > New Group : <input type='text' name='group2' size=50> </td> </tr> <tr> <td height='30' width='55' bgcolor='#EFF3F7' bordercolor='#FFFFFF'> <input name='submit' type='submit' value='Rename'> <input type='hidden' name='cgid' value='$cgid'> </td> </tr> <tr> <td colspan='3' bgcolor='#B5CBEF' height='25' width='737' > <p align='center'> <font face='Calibri' size='2'> </td> </tr> </table> </form>"; } } echo " <html> <head> </head> <body> <div class='Section1'> <input type='hidden' name='userid' value='$userid'> <input type=;hidden' name='sid' value='$sid'> "; } else { echo "<form method='post' action='{$_SERVER['PHP_SELF']}'> <input type='submit' name='submit' value='Select'> </span> </b> </p> <input type='hidden' name='userid' value='$userid'> </form> "; } Note the use of a single block of php code with html embedded in it. Note also the use of " quotes to wrap the long strings of code with single quotes around the attributes. This allows you to insert php variables into the html without having to enter and exit php mode which is silly and makes for difficult interpretation. Lastly - this html code is so outdated. Where did you get it? Please learn a little CSS and also the proper sequence of html tags. You are outputting an entire html table before you ever enter the html tag or the body tag. Incorrect. The style attributes you are using I don't think exist. And the font tag is deprecated as well. And the dimensions without units may or may not work, but switching to a CSS class can resolve that and avoid having to repeat all that code in every one of your html tags. You also have missing closing tags and rows inside of other rows which you may be able to see now that I have broken down your lines to show you how you have composed this block of html. I didn't try and correct anything - just present it better so you can see your syntactical and presentational mistakes. -
Effectively replace the old mysql_result without drastic changes.
ginerjm replied to samet5's topic in PHP Coding Help
As suggested - Go With PDO. Much cleaner and easier not to say more modern even. You're going to have to replace a lot of statements. PDO IMO uses fewer statements to achieve the same compatibility. -
All of the above is top-notch advice. Listen to it even if you don't understand why.
-
You'll have to show us the code where the errors are pointing. Be sure to clearly identify them to us.
-
PHP filename in sub-directory not allowed to be number w include
ginerjm replied to Niss3's topic in PHP Coding Help
What is the backslash for? What are the parens for? // determine what number to use in the name and assign it. $i = GetNextFilNum(); // call a function that returns a number $filename = "file$i.php"; include $filename; -
PHP filename in sub-directory not allowed to be number w include
ginerjm replied to Niss3's topic in PHP Coding Help
Basically it's telling you that there rules to filenaming. What's wrong with naming them 'page1.php' and 'page2.php'? Too hard to program? -
I think I'm gonna quit on you. You just don't understand that you can't post stuff without SHOWING us what it relates to . I've asked already to show us the code with the error lines indicated but you won't do it. And you are trying to do this project that you apparently are not ready to accomplish. (if you don't know where a semi goes you have a lot to learn.) Good luck on your journey.
-
The way to do this would be to generate a CSV file while you are building your html table. Save that file somewhere and give the user a button to click that can be used to cause your script to show he/she what that filename is and were it can be found. A CSV file is MUCH easier to import to excel.
-
And you really can't debug it? My bad - I altered your code and didn't replace the commas there with semis. But the fact that you can't debug this tells me that you are not very knowledgeable in PHP.... These are simple assignment statements and every php statement ends with a semi. The error message points right at the offending comma.
-
Swap in this line if (!$stmt->execute(['fname'=>$fname, 'lname'=>$lname,'email'=>$email])) into my last. The array def was incorrect as I copied it from your code.
-
// initialization session_start(); error_reporting(E_ALL); ini_set('display_errors','1'); require "conn/connect_seniorform.php"; $post = []; // array to hold a trimmed working copy of the form data $errors = []; // array to hold user/validation errors // post method form processing $status = ""; $POST = ""; // WHAT IS THIS VARIABLE FOR? if($_SERVER["REQUEST_METHOD"] == "POST") { $fname = $_POST['fname'], $lname = $_POST['lname'], $email = $_POST['email'] // AS I MENTIONED IN AN EARLIER REPLY YOU HAVE A PROBLEM WITH NEXT LINE. if(empty($fname) || empty($lname) || $email) $errors[] = "All fields are compulsory."; // WHAT ABOUT LAST NAME CHECK??? if(strlen($fname)>= 255 || !preg_match("/^[a-zA-Z-'\s+$/", $fname)) $errors[] = "Please enter a valid name"; if(!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = "Please Enter a valid email"; if (count($errors) > 0) // handle the errors by resending the form back to the user and exit //*************************** // process the data now $sql = "INSERT INTO senior_dat(fname,lname,email) VALUES (:fname, :lname, :email)"; $stmt = $pdo->prepare($sql); if (!$stmt->execute(['fname'=>$fname], ['lname'=>$lname],['email'=>$email])) { echo "Insert query did not run"; exit(); } else echo "Your entries have been accepted"; } This is the latest block of your code with my personal improvements. See if you understand it. See my comments as well.
-
Please how can I solve this PHP MYSQLI database problem?
ginerjm replied to Freddieyanc's topic in PHP Coding Help
I realize that you are a relative newbie here so you may not see what we are seeing right now. To you, the problem is right in front of you, but to us it certainly is not. Without showing us code that is close to where your own debugging has brought your, we really cannot help you. People who browse forums for problems that they think they can help with are going to skip right on by this post with no code being shown. They know. -
This line if($post['fname']===") is wrong. Please look at it. Also - this block makes no sense: if($post['fname']===") { $errors['fname']= "First Name is required"; } { $errors['lname']="Last Name is required"; } { $errors['email']="Email is required"; } You start with a test on fname and produce an error message if needed. But then you produce an error message for lname and another for email but you aren't doing anything to test those values.
-
Could you be more descriptive as to what you are looking for? Modular programming can mean so many different things.
-
How to send html formatted email with attachment
ginerjm replied to wezken's topic in PHP Coding Help
I think you should try that phpmailer install again. Never heard of someone having issues. OR contact your provider and tell them. -
How to send html formatted email with attachment
ginerjm replied to wezken's topic in PHP Coding Help
Have you tried using a very good mail tool such as PHPMailer? Makes all this so easy. -
You do realize that we have no idea where these line numbers are, yes? Braces are used to group lines of php code together. They are only needed when you have more than one line. Example: if(condition) statemtent else other statement That is how you could write a short if statement Here is a larger one: if (condition) { statement; statement; statement; } else statement; Notice how I write my code. Makes it easy to see where the braces go, yes?
-
I too am over70 and agree with your reason for programming. IMO-In My Opinion OP-Original Poster Apparently Komodo Edit doesn't highlight those un-matched parens (parentheses) or braces or brackets.
-
if($_SERVER["REQUEST_METHOD"]=="POST"){ The above is your problem. It would look better like this: if($_SERVER["REQUEST_METHOD"]=="POST") { Once again this is something you s/b able to locate for yourself. IMO, putting the braces on their own lines helps to make it clear where the open and close ones are as well as make it clear where the indented code begins and ends. If you indent, that is. Do you have an intelligent editor that highlights different types of text such as php, html, and plain text? That's how I located it for you. I think you are missing an 'empty' as well.
-
PHP Warning: Trying to access array offset on value of type bool
ginerjm replied to tryon's topic in PHP Coding Help
You have parens where you should have brackets -
This 'blob' thing. Are you saying that you are placing image files into a db table? Why? It is much better to save those types of files as their own files in a designated folder rather than having to store and retrieve them thru a db interface. Simply save the file's name in the table and use whatever your regular files path to locate it when you need it.
-
As a retired professional coder/analyst/designer I simply picked up a PHP book at the local bookstore and started reading. With previous experience with learning languages it was fairly easy since the concepts are the same just the words are different and some functions are new or slightly different. Then I started doing simple db interactions, form designs and eventually CSS inclusion. And once I got into some serious projects (personal stuff) I found these forums and asked questions about what I was doing wrong, SHOWING the faulty code for others to see and offer assistance.
-
When you make these changes why don't you go back and look at the sample you were provided and MAKE SURE that you did it right? You removed the parens from the function call and if you had done this you wouln't have had to make yet another post.