我在将表格插入mysql时遇到问题。由于某种原因,它无法正常工作并显示以下消息:

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


有人可以告诉我我要去哪里了吗,我的表中有9列,但只想在分钟内插入company_name和company_reg_number。所以我以为我做对了,但显然我错过了一些东西。

这是我的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";
}
?>


数据库结构:

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

最佳答案

改变这个

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




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


因为在您的html表单中,您具有cname和creg。

也改变这个:

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




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


我猜您有很多列,您只想更新company_name和company_reg_number。

关于php - 将表单数据提交到mysql时出现 undefined variable 错误消息?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22992989/

10-11 03:16