2013-11-03 17:50
浏览 25


I am new to PHP. I tried to insert form data into SQL data into local DB. I always getting failed error.

Here I is my two files from same. Please guide me to solve my query for same.


$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "my_db";

$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");

$sql_insert = "INSERT INTO company ('company_no','name', 'address', 'model','fabno', 'startdate', 'enddate',
               'InvoiceDate', 'contatctPerson','phoneNumber', 'mailId', 'ccsNO', 'ElgiRegion', 'kmreading') 
     VALUES (NULL,'$_POST[compnay_name]','$_POST[address]','$_POST[fabno]','$_POST[startdate]','$_POST[enddate]',

echo "$sql_insert";

$result = mysql_query($sql_insert,$bd);

if ($result) {
    echo("<br>Input data is succeed");
} else {
    echo("<br>Input data is fail");



Here I am getting always Input data is fail.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dre93205 2013-11-03 17:57

    First off, you've not escaped any of your data. If I were to post to your form "Let's have a party" your SQL breaks because my apostrophe makes your SQL look like

    VALUES(NULL, 'Let's have a party')

    You can resolve this by passing your data through mysql_real_escape_string (I only show a snippet for an example)

    $sql = "VALUES (NULL,'" . mysql_real_escape_string($_POST['company_name']) . "')";

    Which makes your SQL look like this

    VALUES(NULL, 'Let\'s have a party')

    This brings me to my last point. If you clicked my link above, you saw the big, scary red block warning you mysql_ is depreciated and may be removed from future versions of PHP. Try using mysqli

    解决 无用
    打赏 举报
  • doutang6130 2013-11-03 17:56

    It seems like there are a lot of typos in your query. For example 'contatctPerson'. I'm guessing that is supposed to read as 'contactPerson'. Those typos will cause the errors if the table you are working with does not have the same misspellings for the field names. I would suggest going word by word and proofreading the spelling of all your values.

    解决 无用
    打赏 举报

相关推荐 更多相似问题