douzhong3038 2013-11-03 17:50
浏览 26
已采纳

PHP简单插入表格数据到数据库不工作[关闭]

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.

  <?php

$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]',
        '$_POST[InvoiceDate]','$_POST[contatctPerson]','$_POST[phoneNumber]',
          '$_POST[mailId]','$_POST[ccsNO]','$_POST[ElgiRegion]','$_POST[kmreading]')";

echo "$sql_insert";

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

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

mysql_close($bd);

?>

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名