Trouble subimitting data with a form

Hi everybody

I’m trying to make a form to submit data to my PHPMyAdmin database.
The php to connect seems to work fine but when I try to submit data from the form it gives me the error “error inserting new record”. Is there anybody out there who can tell me what I’m doing wrong?

Here’s the html for the form:
Quote

Insert Data into the Customers table

Add your company
to the database

CustId

Company

Country

Postal Code

City

Street and Nr

General Telephone number

General Email

Website

Domain of Email



Unquote

And here is the php:<?php
include(‘connect-mysql.php’);
$CustId=$_POST[‘CustId’];
$Company=$_POST[‘Company’];
$Country=$_POST[‘Country’];
$PostalCode=$_POST[‘PostalCode’];
$City=$_POST[‘City’];
$StreetAndNr=$_POST[‘StreetAndNr’];
$GenTel=$_POST[‘GenTel’];
$GenMail=$_POST[‘GenMail’];
$Website=$_POST[‘Website’];
$Domain=$_POST[‘Domain’];
$sql=“INSERT INTO Customers (CustId, Company, Country, PostalCode, City, StreetAndNr, GenTel, GenMail, Website, Domain) VALUES (’$CustId’, ‘$Company’, ‘$Country’, ‘$PostalCode’, ‘$City’, ‘$StreetAndNr’, ‘$GenTel’, ‘$GenMail’, ‘$Website’, ‘$Domain’)”;

if (!mysqli_query($dbcon, $sql)) {
die (‘error inserting new record’);
}
$answer = “1 record added to the database”;
?>
Unquote

This is the connect file in php:
Quote<?php

DEFINE (‘DB_USER’, ‘Any userr’);
DEFINE (‘DB_PSWD’, ‘Any password’);
DEFINE (‘DB_HOST’, ‘localhost’);
DEFINE (‘DB_NAME’, ‘Any DB’);

$dbcon = mysqli_connect (DB_HOST, DB_USER, DB_PSWD, DB_NAME);
if (!$dbcon){
die (‘error connecting to database’);
}
//echo ‘you have connected succesfully’;
?>
The names of user, password and db have been replaced by dummy values.

Much appreciated.

Sven

A) Whatever forum you pasted that code from onto this forum, isn’t compatible with the forum formatting used here. Paste your original code in this forum and use the forum’s preformatted text </> menu button.
B) Please read the recent ‘form’ submit topics on this forum to find out what your code should be doing so that it is secure, provides a good user experience, and will either work or it will display/log the reason why it doesn’t work.

Several problems with your code. The most obvious the raw data from the form is being handled. You should look into the “filter_input()” function to protect from users hacking your database. Or, upgrade to PDO which is now the preferred use of databases.
Here is a link that shows one example that might help you. This site is a learning site and has examples of all languages, forms, code, etc that might help you get started. Also, please make sure you use the tags so we can copy your code easier.
filter_input Example
Simple Form Example (Use POST version)
Hope this helps you get started…

Sponsor our Newsletter | Privacy Policy | Terms of Service