简体   繁体   中英

undefined variables error message when submitting form data into mysql?

i am having trouble inserting my form into mysql. for some reason its not working and displaying this message:

Notice: Undefined index: company_name in C:\xampp\htdocs\hewden_spms\supplier_function\registration_complete.php on line 15

Notice: Undefined index: company_reg_number in C:\xampp\htdocs\hewden_spms\supplier_function\registration_complete.php on line 16
ERROR

can someone show me where i am going wrong please, i have 9 columns in my table but only want to insert company_name and company_reg_number at the minute. so i thought i was doing this right but clearly im missing something.

heres my html:

<form name="myForm" id="myform" action="registration_complete.php" onsubmit="return validateForm()" method="post">

<input type="text" name="cname">
<input type='text' name='creg'>

<input type="submit" id="postme" value="Submit">

php:

<?php
session_start();

$db_hostname = 'localhost';
$db_database = 'hewden1'; 
$db_username = 'root';
$db_password = '';

$db_server = mysql_connect($db_hostname, $db_username, $db_password)    
        or die("Unable to connect to MySQL: " . mysql_error());

mysql_select_db($db_database)   
or die("Unable to select database: " . mysql_error());

$cname       = $_POST['company_name'];
$creg      = $_POST['company_reg_number'];    


$sql="INSERT INTO supplier_registration (null, company_name, company_reg_number, null, null, null, null, null, null)
VALUES ('$cname', '$cname')";$result = mysql_query($sql); 

if($result){

echo "jobs a gooden";

}else {
echo "ERROR";
}
?>

database structure:

table = supplier_registration

column 1 = id
column 2 = company_name
column 3 = company_reg_number
column 4 = address
column 5 = postcode
column 6 = email
column 7 = name
column 8 = vat
column 9 = age

change this

 $cname       = $_POST['company_name'];
 $creg      = $_POST['company_reg_number'];

to

   $cname       = $_POST['cname'];
   $creg      = $_POST['creg'];

because in your html form you have cname and creg .

change also this:

 $sql="INSERT INTO supplier_registration (null, company_name, company_reg_number, null, null, null, null, null, null)
  VALUES ('$cname', '$cname')";

to

$sql="UPDATE supplier_registration SET company_name ='$cname', company_reg_number='$cname' " ;

i guess you have many columns and you want just update company_name and company_reg_number.

In your form there is

<input type="text" name="cname">
<input type='text' name='creg'>

while in your php code there is

$cname       = $_POST['company_name'];
$creg      = $_POST['company_reg_number'];  

So, change this

Or this

$cname       = $_POST['cname'];
$creg      = $_POST['creg'];
  1. Check if the POST variable is set and assign it to the variables. So that if a user does not type anything in a particular input and submit it, you will not receive any warning.
  2. Your HTML input names must match while getting it in the PHP end.

So rewrite your PHP code as,

if(isset($_POST['cnmae']))
{
  $cname=$_POST['cname'];
}
if(isset($_POST['creg']))
{
  $creg= $_POST['creg'];
}

You need to make the following changes to your code:

$cname = $_POST['cname'];
$creg = $_POST['creg'];    


$sql="INSERT INTO supplier_registration (company_name, company_reg_number)
VALUES ('$cname', '$creg')";$result = mysql_query($sql);

However keep in mind that this is extremely vulnerable to SQL injection, and you should also swap MySQL for PDO/MySQLi immediately if you plan to use this code on a live website.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM