Jump to content

rocky48

Members
  • Posts

    261
  • Joined

  • Last visited

Everything posted by rocky48

  1. Hi Ginerjm Here is the form part of the senior_data3.php: <form method="post"> <label><b>First Name:</b><br><input type="text" name="fname" size="20" maxlength="40" value="<?=htmlentities($_POST['fname']??'',ENT_QUOTES)?>"></label> <br> <label><b>Surname:</b><br><input type="text" name="sname" size="20" maxlength="40" value="<?=htmlentities($_POST['sname']??'',ENT_QUOTES)?>"></label> <br> <label><b>Email:</b><br><input type="text" name="email" size="20" maxlength="40" value="<?=htmlentities($_POST['email']??'',ENT_QUOTES)?>"></label> <br><br> <input type="submit" value="submit"> </form> Hope this helps to solve y problem!
  2. Right! The POST array is empty! So why is the data I type into the form not being POST'ed. When I previously used php to post data to the database I used to have the form on a different file I am not familiar with the method that has been suggested with this code. So, why does the code I have not POST properly. Most of it I have copied from suggestions in previous posts? I have just started an online PDO course with Udemy, so perhaps I may begin to understand PDO mush better!
  3. You have got my change I mentioned the wrong way around? I changed first_name to frame etc. I will look at your post in detail later!
  4. Yes of course I do! Is these a critic clue? I’m not very good at Crosswords! I assumed that the name=‘frame’ and the htmlentities code dealt with the output from the form?
  5. I'm still chasing errors! This is back to my old favourite: Notice: Undefined index: fname in /homepages/30/d593365489/htdocs/MFC1066/senior_data3.php on line 19 Notice: Undefined index: sname in /homepages/30/d593365489/htdocs/MFC1066/senior_data3.php on line 20 Incidentally, why does the email line not have the same error? As I said previously do I need 'isset', if so can you show me the correct syntax when a Post variable is involved?
  6. I think I see where I am wrong: I changed the $POST entries to frame, but the form name entry is first_name etc. Am just going to watch F1 qualifying, so will look at it tomorrow!
  7. These are the notices: Notice: Undefined index: fname in /homepages/30/d593365489/htdocs/MFC1066/senior_data3.php on line 19 Notice: Undefined index: sname in /homepages/30/d593365489/htdocs/MFC1066/senior_data3.php on line 20 How do you index these variables? Do you use isset? not sure of the syntax ? OR should I use Fetch? Can you show e some examples?
  8. Hi ginerjm Since the last post I have managed to get rid of the errors, but although there are no errors, nothing is input into the database. Some of the field names did not match my database so have changed them. Was getting an undefined index notice but am ignoring that with ^ E_NOTICE. Why does it not report that the INSERT has not worked as in the If Else statement? Here is the code: <!DOCTYPE html> <html> <?php // initialization session_start(); error_reporting (E_ALL ^ E_NOTICE); ini_set('display_errors','1'); require "conn/connect_seniorform2.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 = ""; if($_SERVER["REQUEST_METHOD"] == "POST") { $fname = $_POST['fname']; $sname = $_POST['sname']; $email = $_POST['email']; if(strlen($fname)>= 255 || !preg_match("/^a-zA-Z-'\s+$/", $fname)) $errors[] = "Please enter a valid name"; if (count($errors) > 0) exit(); // handle the errors by resending the form back to the user and exit //*************************** // process the data now $sql = "INSERT INTO senior_dat(fname,sname,email) VALUES (:fname, :sname, :email)"; $stmt = $pdo->prepare($sql); $parms = array( 'fname'=>$fname, 'sname'=>$sname, 'email'=>$email ); if (!$stmt->execute($parms)) { echo "Insert query did not run"; exit(); } else echo "Your entries have been accepted"; } ?> <h1>SENIOR RENEWAL FORM</h1> <?php // display any errors if(!empty($errors)) { echo '<p>'; echo implode('<br>',$errors); echo '</p>'; } ?> <form method="post"> <label><b>First Name:</b><br><input type="text" name="first_name" size="20" maxlength="40" value="<?=htmlentities($_POST['fname']??'',ENT_QUOTES)?>"></label> <br> <label><b>Surname:</b><br><input type="text" name="last_name" size="20" maxlength="40" value="<?=htmlentities($_POST['sname']??'',ENT_QUOTES)?>"></label> <br> <label><b>Email:</b><br><input type="text" name="email" size="20" maxlength="40" value="<?=htmlentities($_POST['email']??'',ENT_QUOTES)?>"></label> <br><br> <input type="submit" value="submit"> </form> </html> Any ideas why this is not posting this into the database?
  9. Better have a whip round and buy me some new glasses! I always miss the most obvious mistake!
  10. Thought the log file might help! php_error.txt
  11. I have just tried adding the 'extension=php_pdo_mysql ' to php.ini I restarted the services, but still not working?
  12. Is the dll supposed to be in this folder: C:\wamp64\bin\php\php8.1.0 ? I am running 8.10. Still not working??
  13. I am using PHP Version 8.1.0 The PDO versions as shown in picture. Looks like its running pdo_mysql? According to all the stuff I am reading on the internet, I should have an extension: php_pod_mysql How does that help?
  14. I am using PHP Version 8.1.0 The PDO versions as shown in picture. Looks like its running pdo_mysql? According to all the stuff I am reading on the internet, I should have an extension: php_pod_mysql How does that help?
  15. I am trying to use my WAMP server to test code that is using PDO. Most sites tell me to open the php.ini file and find the extension extension=php_pdo_mysql and remove the semi colon from the begining. The only extension I can find is: extension=pdo_mysql, which has had the semicolon removed. i have tried installing the driver and install this: instantclient-basic-windows.x64-21.7.0.0.0dbru.zip and add the location to my environment path, which I have done. I've run out of ideas, so can someone tell me where I am going wrong?
  16. Ginerjm Tried your array example, but now get an error on the echo: $sql = "INSERT INTO senior_dat(fname,lname,email) VALUES (:fname, :lname, :email)"; $stmt = $pdo->prepare($sql); $parms = array( 'fname'=>$fname, 'lname'=>$lname, 'email'=>$email ); if (!$stmt->execute($parms) { echo "Insert query did not run"; exit(); } else echo "Your entries have been accepted"; What are we testing with the if statement? Is the fact that the execute either runs or not sufficient to provide a Boolean result?
  17. Sorry but I can't see what you are saying?? All I did was remove the other variables as I had not got those in my form yet. I was taking Mac_gyvers advice and just getting one field working first! That was a very flippant remark Barand!
  18. I have the connection code working now but have an error in the PDO code for saving to the database. its on this line: if (!$stmt->execute(['fname']=>$fname)) The error: Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ')' in .... As far as I can see the format is correct. Why am I getting this error?
  19. Barand - So should I change the connection code to PDO instead of mysqli? As I keep saying I have not used PDO before and previous databases I have written were non pdo. I have had a go at changing the connection code, but get this error: Parse error: syntax error, unexpected ':', expecting ')' in /homepages/30/d593365489/htdocs/MFC1066/conn/connect_seniorform.php on line 7 Line 7: $pdo = new PDO(mysql:host='dbxxxxxxx.hosting-data.io;dbname=dbsxxxxxxx', $user, $pass); I have used this example from the PHP documentation: $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass); I can't see that there is anything wrong with my modification? It suggests that the colon should be a closing bracket?? Could it be caused elsewhere in the code. I have attached the full code: <?php $host_name = 'dbxxxxxxx.hosting-data.io'; $dbname = 'dbsxxxxxx'; $user = 'dbuxxxxx'; $pass = 'xxxxxxxxx'; $pdo = new PDO(mysql:host='db5010240626.hosting-data.io;dbname=dbs8678517', $user, $pass); if ($pdo->connect_error) { die('<p>Failed to connect to MySQL: '. $pdo->connect_error .'</p>'); } else { echo '<p>Connection to MySQL server successfully established.</p>'; } ?>
  20. I have now corrected the problems in the form, but have a problem in the prepare statement: Fatal error: Uncaught Error: Call to a member function prepare() on null in ... Here is the line: $stmt = $pdo->prepare($sql); My research on the web says that the pdo is not initialised, but the example initialises it when it logs in to the database. I have connected to the database using require using the following code: <?php $host_name = 'xxxxxxxx'; $database = 'xxxxxx'; $user_name = 'xxxxxxxxxx'; $password = 'xxxxxxxxx'; $link = new mysqli($host_name, $user_name, $password, $database); if ($link->connect_error) { die('<p>Failed to connect to MySQL: '. $link->connect_error .'</p>'); } else { echo '<p>Connection to MySQL server successfully established.</p>'; } ?> But I have used mysqli, so is the the reason that it fails with this error? I have not used PDO before this project, so am not sure if I need to change the connection code. Will it work if I change the connection code that I require at the beginning of the script or is it better to put the PDO connection code at the top of main code? Here is the complete code for my form entry to the database: <!DOCTYPE html> <html> <?php // initialization session_start(); error_reporting (E_ALL ^ E_NOTICE); 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 = ""; if($_SERVER["REQUEST_METHOD"] == "POST") { $fname = $_POST['fname']; $lname = $_POST['lname']; $email = $_POST['email']; 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"; } ?> <h1>SENIOR RENEWAL FORM</h1> <?php // display any errors if(!empty($errors)) { echo '<p>'; echo implode('<br>',$errors); echo '</p>'; } ?> <form method="post"> <label><b>First Name:</b><br><input type="text" name="first_name" size="20" maxlength="40" value="<?=htmlentities($_POST['first_name']??'',ENT_QUOTES)?>"></label> <br> <input type="submit" value="submit"> </form> </html>
  21. I know that you use semi colons at the of some statements. I was not sure if it was part of an if statement that these lines were meant to have comas. I had tried adding semi colons but it produced more errors. However, I persisted and made several changes and I have sort of got it working, but something is wrong as it puts a 'input>' in the first name box. As far as I can see the syntax is correct! This is the form section: ?> <h1> SENIOR RENEWAL FORM</h1> <?php if(!empty($errors)) { echo implode('<br>',$errors);echo''; } ?> <form method="post" <label <b>First Name: </b><br></label><input type="text" name="fname" size="20" maxlength="40" value=<?php "htmlentities"($post['fname']?? ENT_QUOTES)?> <input type="submit" value="Submit"> </form> </html> The ?> is after the last line that you sent me. I tried entering my name in the input box and got a string of errors: I got rid of the notices, but were left with the preg_match and the Fatal Error. Can you offer any more help?
  22. Hi ginerjm Tried your code but it fails on this line: $fname = $_POST['fname'], Error: Parse error: syntax error, unexpected ',' in /homepages/30/d593365489/htdocs/MFC1066/senior_data2.php on line 17 I wondered if the comma's should have been semi colons, but changing that caused other errors. Did the code run for you on your computer?
  23. Jinergm - Please explain! I do not have the knowledge to know how it is wrong! I must admit the single double quote looks wrong, I only copied Mac_gyver's earlier post back on the 9th October. Barand - Don't understand? it has closing quote, but doesn't have an opening quote? Tried putting a quote after the bracket, didn't think that was right! It moved the error to line 30 but I'm sure that's not the problem?
  24. mac_gyver I have gone back to your post of 7th October and tried to follow your example. I get this error in line 30: $errors['fname']= "First Name is required"; What am I doing wrong?
  25. They have been there all the time?? They are the php variables for the three form inputs I am trying to put into the database. In the array the $fname are created from the $_POST[fname] e.g. $fname=>$_POST['fname'] Is the syntax wrong?
×
×
  • 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.