# Form not working... where is my code in error?

**URL:** <https://www.phphelp.com/t/form-not-working-where-is-my-code-in-error/26822>\
**Category:** General Database (Oracle, SQL Server, etc)\
**Created:** [July 31, 2018, 12:04am UTC](https://www.phphelp.com/t/form-not-working-where-is-my-code-in-error/26822 "2018-07-31T00:04:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![CliffRich](https://www.phphelp.com/letter_avatar_proxy/v4/letter/c/3bc359/32.png) [@CliffRich](https://www.phphelp.com/u/CliffRich)\
**Post date:** [July 31, 2018, 12:04am UTC](https://www.phphelp.com/t/form-not-working-where-is-my-code-in-error/26822/1 "2018-07-31T00:04:54Z")

</div>

## Ok so i have this form and it’s just returning a random 500 error… HELP! this is the submission form

> Form Test label,a { font-family : Arial, Helvetica, sans-serif; font-size : 12px; } 
> 
> Business Name:
> 
> Name:
> 
> Phone Number:
> 
> Email Address:
> 
> Website:
> 
> Which Service(s) are you interested in?:  
> Review Audit/Management Logo Creation/Editing Website Creation/Editing
> 
> Additional Comments:

* * *

```
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

        $bname=$_POST['fname'];
        $name=$_POST['rname'];
        $email=$_POST['email'];
        $pnum=$_POST['pnum'];
        $website=$_POST['website'];
        $service = implode(',', $_POST['service']);
	$comment=$_POST['comment'];

	$sql = "INSERT INTO brp_requests (bname, rname, email, pnum, website, service, comment)
		VALUES ($bname, $name, $email, $pnum, $website, $service, $comment)";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
	} 
else {
    echo "Error: " . $sql . "<br>" . $conn->error;
	}

$conn->close();
?>

//done. redirect to thank-you page.
header('Location: thank-you.html');

// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}
   
?>

```

---

<div class="post-metadata">

**Author:** ![CliffRich](https://www.phphelp.com/letter_avatar_proxy/v4/letter/c/3bc359/32.png) [@CliffRich](https://www.phphelp.com/u/CliffRich)\
**Post date:** [July 31, 2018, 12:07am UTC](https://www.phphelp.com/t/form-not-working-where-is-my-code-in-error/26822/2 "2018-07-31T00:07:17Z")

</div>

how do i share code without it getting processed on here?

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [July 31, 2018, 12:13am UTC](https://www.phphelp.com/t/form-not-working-where-is-my-code-in-error/26822/3 "2018-07-31T00:13:25Z")

</div>

> Blockquote

```
indent preformatted text by 4 spaces

```

Options on the tool bar.

As far as your code, you close php for no reason right after the connection close. If you had error reporting turned on you would get all kinds of errors.

Additionally, you need to use Prepared Statements. Do not create variables for nothing. You have a function floating around that is not used.

Do not output internal system errors to the user. That info is only good to hackers.

This line is redundant:  
`if ($conn->query($sql) === TRUE)`

if already checks for true/false. The === TRUE needs to go.

There is no need to manually close the DB connection. Php will do it automatically.

You really should be using PDO. Here is a tutorial to get you going. [https://phpdelusions.net/pdo](https://phpdelusions.net/pdo)

---

<div class="post-metadata">

**Author:** ![CliffRich](https://www.phphelp.com/letter_avatar_proxy/v4/letter/c/3bc359/32.png) [@CliffRich](https://www.phphelp.com/u/CliffRich)\
**Post date:** [July 31, 2018, 12:33am UTC](https://www.phphelp.com/t/form-not-working-where-is-my-code-in-error/26822/4 "2018-07-31T00:33:36Z")

</div>

Ok so I fixed the close php, have no clue what you mean about prepared statements, removed the true and the db close, and no clue about pdo… and i’m still getting the same error…

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [July 31, 2018, 12:46am UTC](https://www.phphelp.com/t/form-not-working-where-is-my-code-in-error/26822/5 "2018-07-31T00:46:20Z")

</div>

Here is where basic debugging comes in.

1. Turn on error reporting
2. Look in the error log file and see what it says.

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [August 1, 2018, 1:45am UTC](https://www.phphelp.com/t/form-not-working-where-is-my-code-in-error/26822/6 "2018-08-01T01:45:56Z")

</div>

Also, does the file named “thank-you.html” spelled exactly that way exist on the same place as your code is?  
AND, the last section of your code is OUTSIDE of PHP… Right after $conn-\>close(); you close PHP…  
The rest is not in your code but is HTML and would throw an error because you have no HTML set up…

---

<div class="post-metadata">

**Author:** ![CliffRich](https://www.phphelp.com/letter_avatar_proxy/v4/letter/c/3bc359/32.png) [@CliffRich](https://www.phphelp.com/u/CliffRich)\
**Post date:** [August 13, 2018, 4:58am UTC](https://www.phphelp.com/t/form-not-working-where-is-my-code-in-error/26822/7 "2018-08-13T04:58:53Z")

</div>

so i decided to take it down to basic levels to see if it could be the host that is causing me issues… i used this code to run a test`\<?php

# Fill our vars and run on cli

# $ php -f db-connect-test.php

$dbname = ‘name’;  
$dbuser = ‘user’;  
$dbpass = ‘pass’;  
$dbhost = ‘host’;  
$connect = mysql\_connect($dbhost, $dbuser, $dbpass) or die(“Unable to Connect to ‘$dbhost’”);  
mysql\_select\_db($dbname) or die(“Could not open the db ‘$dbname’”);  
$test\_query = “SHOW TABLES FROM $dbname”;  
$result = mysql\_query($test\_query);  
$tblCnt = 0;  
while($tbl = mysql\_fetch\_array($result)) {  
$tblCnt++;  
#echo $tbl[0].“  
\n”;  
}  
if (!$tblCnt) {  
echo “There are no tables  
\n”;  
} else {  
echo “There are $tblCnt tables  
\n”;  
}`

and it ran fine… but then I added an insert which i did like this  
\<?php

# Fill our vars and run on cli

# $ php -f db-connect-test.php

$dbname = ‘name’;  
$dbuser = ‘user’;  
$dbpass = ‘pass’;  
$dbhost = ‘host’;

$connect = mysql\_connect($dbhost, $dbuser, $dbpass) or die(“Unable to Connect to ‘$dbhost’”);  
mysql\_select\_db($dbname) or die(“Could not open the db ‘$dbname’”);

insert into resorts(Name, Color)  
values(‘Clifford Richmond’,‘Blue’);

SELECT  
\*  
FROM  
resorts;

and it couldn’t handle it…

 ![error](https://www.phphelp.com/uploads/default/original/2X/5/58224ef3c8cd0b47705abe7642a07a8a43e90c8e.png)

what am i missing…

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [August 13, 2018, 4:14pm UTC](https://www.phphelp.com/t/form-not-working-where-is-my-code-in-error/26822/8 "2018-08-13T16:14:29Z")

</div>

Your code uses mysqli but you want to run a test with Obsolete and dangerous MySQL code that has been removed from PHP?

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [August 14, 2018, 6:43pm UTC](https://www.phphelp.com/t/form-not-working-where-is-my-code-in-error/26822/9 "2018-08-14T18:43:00Z")

</div>

Well, we do not support using MySQL here as Benanamen stated. Please update your code at least to MySLQi code.

Next, there is no command such as INSERT. That is for a QUERY. Therefore, you need to execute the query in the correct format.

Read up on this using the “improved” version of MySQLi here: [W3-Schools insert data](https://www.w3schools.com/php/php_mysql_insert.asp)  
That site has a ton of beginner examples for you to look at. Then, fix up your code and come back with your next question(s)… Good luck!
