Jump to content

Saving to mysql database not working!


rocky48

Recommended Posts

Some corrections to the entities code and removal of the horrible use of those single quotes to wrap your values.  Don't know why you didn't see them as bad coding.

$fname = htmlentities($_POST['fname'],ENT_QUOTES);
$sname = htmlentities($_POST['sname'],ENT_QUOTES);
$email = htmlentities($_POST['email'],ENT_QUOTES);
echo "
	<form method='post'>
	<label>
	<b>First Name:</b> 
	<input type='text' name='fname' size='20' maxlength='40' value='$fname'>
	</label>
	
	<br>
	<label>
	<b>Surname:</b>  
	<input type='text' name='sname' size='20' maxlength='40' value='$sname'>
	</label>
	
	<br>
	<label>	<b>Email:</b> 
	<input type='text' name='email' size='20' maxlength='40' value='$email'>
	</label>
	<br>
	<br>
	<input type='submit' name='btn' value='Submit'>
	</form>
";

And the addition of a name attribute to your submit.

Now - can you see what you have to use to 'grab' the input values?

Don't know why you are using the htmlentities but there must be a reason.

Link to comment
Share on other sites

I've got a couple of questions?

The Entities part does this part go in the $params array? Or where I have written //THIS.

What is the echo " just after the entities in your ost?

 

if($_SERVER["REQUEST_METHOD"] == "POST") 
{
		$fname = $_POST['fname'];		//THIS
        $sname = $_POST['sname'];		//THIS
        $email = $_POST['email'];		//THIS
	
	if(strlen($fname)>= 255 || !preg_match("/^a-zA-Z-'\s+$/", $fname))  // Do I need to do this for all inputs?
		$errors[] = "Please enter a valid name";
		if (count($errors) > 0)
                exit();

 

Also why is there this after the form: ";

Here is the complete code:

<!DOCTYPE html>
<html>
<?php
// initialization
session_start();
error_reporting (E_ALL); 
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 = htmlentities($_POST['fname'],ENT_QUOTES);
        $sname = htmlentities($_POST['sname'],ENT_QUOTES);
        $email = htmlentities($_POST['email'],ENT_QUOTES);  
	
	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="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>
</html>

I have tried the above, but although I submit the entries nothing appears in the database?

I added the lines to show the array and the items I input are correct?

Quote

POST array is

Array ( [fname] => Tomy [sname] => Hudson => tonyedwardhudson@gmail.com

Now what?

Link to comment
Share on other sites

I don't know why you are repeating your posts.  I fixed some code from your past post and gave it to you.  I don't know why you are using the htmlentities but you were using it wrong and I changed that.  

If you can ask a direct pointed question about some one thing that is giving you an error, then please do that because this is a long conversation that is going nowhere

Link to comment
Share on other sites

Ginerrm

You have NOT answered the questions I made in my last post. There are 2 odd entries at the end of the 2 corrections you suggested.

The code worked but did not enter anything into the database.

 I am going to check the database by writing a direct insert in phpmyadmin.

Cant think of any other way of checking it.

Link to comment
Share on other sites

The code that I provided can be used EXACTLY AS WRITTEN.

The "echo" is the beginning of a long statement that writes the html to the client.  Follow the double quotes.

So it seems that your phpadmin is working and showing you table names.  But you didn't tell me if your 'missing' tablename showed up when you showed the database names.

So - is the problem here that you script cannot connect to the db server?

Link to comment
Share on other sites

When you have an issue with code it helps TREMENDOUSLY if you post the code

From the look of it I think you altered my code by exiting php mode when there was no php tag in the code I gave you.  I told you it worked EXACTLY as written.

Edited by ginerjm
Link to comment
Share on other sites

I skimmed this thread from the beginning, and while I am not nearly as experienced as those who've been assisting you, I have encountered many issues in coding.

My suggestion to you would be to create a NEW simple, form with ONLY one field.

<form>

<input type="text" name="first">

<input type="submit">

</form>

Add a very basic PHP script to create a table with the one field. Load the code with ERROR detection to give you clues to every potential defect.

Once you are able to successfully populate the database table with a piece of data from the form, you can THEN expand the code to include additional fields, validation, etc.

Create a new version with each effort so that you always have a working template to return to and then just continue to build on the foundation until you have what you wanted.

It may take longer, but you will learn and get a better understanding for the future.

Good luck.

 

 

 

 

Edited by phppup
Forgot item
Link to comment
Share on other sites

To answer the new coders posts I have got php tags! (see attached code)

ginerjm

I did post the code as shown, but I may have mis understood your post.

I separated the 3 fields with the htmlentities to just after the if.......==$POST statement.

Is this what you intended?

Here is the whole code so far:

<!DOCTYPE html>
<html>
<?php
// initialization
session_start();
error_reporting (E_ALL); 
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 = htmlentities($_POST['fname'],ENT_QUOTES);
        $sname = htmlentities($_POST['sname'],ENT_QUOTES);
        $email = htmlentities($_POST['email'],ENT_QUOTES);  
	
	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>';
}

echo "
	<form method='post'>
	<label>
	<b>First Name:</b> 
	<input type='text' name='fname' size='20' maxlength='40' value='$fname'>
	</label>
	
	<br>
	<label>
	<b>Surname:</b>  
	<input type='text' name='sname' size='20' maxlength='40' value='$sname'>
	</label>
	
	<br>
	<label>	<b>Email:</b> 
	<input type='text' name='email' size='20' maxlength='40' value='$email'>
	</label>
	<br>
	<br>
	<input type='submit' name='btn' value='Submit'>
	</form>
";
?>
</html>

 

Link to comment
Share on other sites

That is not the code that caused the last post to be incorrect.  

the word 'echo' is still in php mode here but in the last the only way it would have shown would be if you turned off php mode.

 

BTW - as my normal practice I NEVER exit php mode in my scripts.  I begin with the first line of every script as <?php and never use the ?> tag EVER.  To output my js or my html or my css I use the Heredocs construct.  Read up on it in the manual.  Either that or a simple echo for a small amount of non-PHP code.  In your last you left and re-entered and then left at the end all for no reason other than lack of knowledge.

Link to comment
Share on other sites

Based upon the topic we have to assume something since one who has utterly no knowledge probably should not be getting in too deep.

As for this post I don't get it.   You showed us some output from the code I posted days ago that could not have occurred unless you altered it.  Then you showed me your code that would not have produced what you showed me.

Link to comment
Share on other sites

Sorry about my previous Rant! I'm getting frustrated with keep chasing errors! I obviously missed the quotes.

I can't understand why I am getting errors in the html form saying:

Quote

Parse error: syntax error, unexpected 'fname' (T_STRING), expecting ';' or ',' in /homepages/30/d593365489/htdocs/MFC1066/senior_data4.php on line 59

This is your code that you posted in the earlier post, which I pasted in. I assume I would get the same error for the other 2 name variables?

I have checked the html manual and the entries look OK to me.

Here is the code that you posted an I pasted in:

echo "
	<form method='post'>
	<label>
	<b>First Name:</b> 
	<input type='text' name='fname' size='20' maxlength='40' value='$fname'>
	</label>
	
	<br>
	<label>
	<b>Surname:</b>  
	<input type='text' name='sname' size='20' maxlength='40' value='$sname'>
	</label>
	
	<br>
	<label>	<b>Email:</b> 
	<input type='text' name='email' size='20' maxlength='40' value='$email'>
	</label>
	<br>
	<br>
	<input type='submit' name='btn' value='Submit'>
	</form>
";
?>

 

Link to comment
Share on other sites

Yes that is the code I posted.  It is not the code you tried to run because the word 'echo' would not have come out of MY code.  As for the  error message:

Parse error: syntax error, unexpected 'fname' (T_STRING), expecting ';' or ',' in /homepages/30/d593365489/htdocs/MFC1066/senior_data4.php on line 59

You have to locate line 59 and show it to us.  You have an issue with something on it so let us see it.  That's how WE solve these problems.  Do not change a THING!

:13 minutes later:   I think I know the line.  Waiting to see if YOU can locate it too.

Edited by ginerjm
Link to comment
Share on other sites

Tired of waiting so I re-wrote your supposed code that you posted.  This is how I would do it even though I'm not sure about some of what you are doing.  PLEASE read this and try to understand it before you CHANGE A THING.  This is a learning experience so please try instead of just changing things.

<?php
// THIS IS HOW YOU WRITE A SCRIPT
// THIS IS HOW YOU WRITE A SCRIPT
// THIS IS HOW YOU WRITE A SCRIPT
//
// start your php code here.
session_start();
error_reporting (E_ALL);
ini_set('display_errors','1');
$errors = array();
require "conn/connect_seniorform2.php";
// MORE PHP CODE IF ANY
// MORE PHP CODE IF ANY
// MORE PHP CODE IF ANY
// DO WE HAVE A FORM TO PROCESS??
if($_SERVER["REQUEST_METHOD"] == "POST")
{	// read the inputs
	$fname = htmlentities($_POST['fname'],ENT_QUOTES);
	$sname = htmlentities($_POST['sname'],ENT_QUOTES);
	$email = htmlentities($_POST['email'],ENT_QUOTES);
	if(strlen($fname)>= 255 || !preg_match("/^a-zA-Z-'\s+$/", $fname))
		$errors[] = "Please enter a valid first name";
	// whey are you not checking the other inputs here?

	//***************************
	//  process the data now
	if (count($errors) == 0)
	{
		$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))
			$errors[] = "Insert query did not run";
		else
			$errors[] =  "Your entries have been accepted";
	}
}
else	// no inputs yet
{
	$fname = '';
	$sname = '';
	$email = '';
}
//   Done with the inputs - redisplay the form screen with the messages we produced.
//
//  implode the errrors array to show the result message or the errors
$errmsg = implode('<br>', $errors);
//
//  BEGIN the HTML output HERE
//  BEGIN the HTML output HERE
//  BEGIN the HTML output HERE
echo "
	<!DOCTYPE html>
	<html lang='en'>
	<head>
	<style type='text/css'>
	#form_box
	{
		position:relative;
		float:left;
		margin:3% 1%;
		padding:5px;
		border:2px solid black;
	}
	.red{color:red;}
	</style>
	</head>
	<body>
	";
echo "
	<div id='form_box'>
	<center>
	<h1>SENIOR RENEWAL FORM</h1>
	</center>
	";
 if(!empty($errmsg))
	echo "<p class='red'>$errmsg</p>";
echo "
	<form method='POST'>
	<label><b>First Name: </b>
	<br>
	<input type='text' name='fname' size='20' maxlength='40' value='$fname'>
	</label>
	<br>
	<label><b>Surname: </b>
	<br>
	<input type='text' name='sname' size='20' maxlength='40' value='$sname'>
	</label>
	<br>
	<label><b>Email: </b>
	<br>
	<input type='text' name='email' size='20' maxlength='40' value='$email'>
	</label>
	<br><br>
	<center>
	<input type='submit' value='Submit'>
	</center>
	</form>
	</div>
	";
	// any more html code....
	//  Done
	echo "
		</body>
		</html>
		";
exit();

Please study this.  I am attempting to show you something and hoping it gives you something to understand and grow from.  I do think your edit of the fname field is incorrect.  Don't use that code myself so I can't help you with it.  Plus if you are going to edit one input why not all the inputs to be sure the user is doing his part correctly.

And I am dropping out.  Any topic that goes to 4 pages is way over-done.  If this doesn't get you on the right path then I'll let someone else pick up and see if they can do it.

PS - Note how one can insert PHP vars into the html code rather than trying to produce that output while writing the html.  Build the vars before you start the html and just insert the vars into the portion of the html code that you want that data to appear.  See how I did the errors array.

Edited by ginerjm
Link to comment
Share on other sites

Thank you for your help! only problem is that all variables are being rejected by preg_match?

I did add the checks for sname and email like so:

if(strlen($sname)>= 255 || !preg_match("/^a-zA-Z-'\s+$/", $sname))
		$errors[] = "Please enter a valid surname";
        if (strlen($email)>= 255 ||!preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $email))
		$errors[] ="Please enter a valid email address!";

The preg_match for the email I researched earlier.

Can't be anything I have done as fname is rejected as well?

 

Link to comment
Share on other sites

Why do you care so much about a name?  If they can't spell their name correctly, why bother checking it?

You could learn something by reading up on "filter_var" in the PHP Manual.

And - while you are probably using a varchar for your table columns for the name fields, do you really want them to run 255 chars in length?  You're specifying a size of 20 in your html (which means nothing) and a max length of 40 (which does) so why are you accepting input up to 255?

Edited by ginerjm
Link to comment
Share on other sites

Ginerjm

Have removed all the preg_match and that has cleared that problem, but now are getting several notice errors in the form saying undefined variables in some in the 3 inputs that I tested the form on, which previously worked and also 4 other lines that I since added. see below:

Quote

Notice: Undefined variable: fname in /homepages/30/d593365489/htdocs/MFC1066/senior_data5.php on line 119

Notice: Undefined variable: sname in /homepages/30/d593365489/htdocs/MFC1066/senior_data5.php on line 124

Notice: Undefined variable: email in /homepages/30/d593365489/htdocs/MFC1066/senior_data5.php on line 129

Notice: Undefined variable: addr in /homepages/30/d593365489/htdocs/MFC1066/senior_data5.php on line 134

Notice: Undefined variable: phone in /homepages/30/d593365489/htdocs/MFC1066/senior_data5.php on line 138

Notice: Undefined variable: mob in /homepages/30/d593365489/htdocs/MFC1066/senior_data5.php on line 142

Notice: Undefined variable: bmfa_no in /homepages/30/d593365489/htdocs/MFC1066/senior_data5.php on line 146

Notice: Undefined variable: caa_no in /homepages/30/d593365489/htdocs/MFC1066/senior_data5.php on line 150

I will add the complete file as it is different than before.  I have added isset in the $_POST lines which cleared the same type of error on those lines.

I really thought I was getting somewhere when I got the initial test entries to work!

Link to comment
Share on other sites

It sounds like you are having the same problems that you and us have already gone over multiple times.  If you had simply pasted in a line of code to go with one of those errors, we could condescendingly tell you once again what to change.  And at the same time maybe you will begin to understand.

I have never been involved with a topic that has taken 4 pages of back and forth to finish.

AND - I don't see how you could be getting that error for fname when I replace the code that used that.  You must not be using MY code with your additions to it.

Edited by ginerjm
Link to comment
Share on other sites

What DON'T understand is the fact that I thought I had mitigated the errors in the script lines 10 -31 Viz: 

$fname = htmlentities($_POST['fname'],ENT_QUOTES); to $sign = htmlentities............

Where I had undefined variables I added an isset statement, but why am I getting errors in the form script now with some of the variables that I have used the isset statement previously in the lines 10-31?  I have now added the isset to ALL the fields in that set of code.

What I don't understand the connection between the variables in the post section and those in the form. Surely if I had set the variable with isset it surely would be true for the variables set in the form?

Regarding this going on so long, perhaps I should have started a new post for each different problem that I encountered as I have bee working through this project?

The only difference to the script I posted in my last post is the if(isset.. line for each variable.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.