dongza3124 2015-05-08 20:04
浏览 23
已采纳

插入表时出现MySQL语法错误[关闭]

I am trying to make a simple invoicing program. I have 1 form page where I can input customers and it saves that data to the table "customers" I then have a page where I can input invoice details. It pulls the customer information from the "customers" table and then saves that information along with the added information such as "cost" to the "invoice" table. It works, but as soon as I input any new information such as "cost" I get a syntax error.

This is the code that pulls the info from the "customers" table.

$gresult = ''; //declare global variable
if(isset($_POST["action"]) and $_POST["action"]=="edit"){
$id = (isset($_POST["ci"])? $_POST["ci"] : '');
$sql = "select contact_id, first_name, last_name,
        contact_no, address, 
        company, email, cost from customers 
        where contact_id = $id";


$result = mysqli_query($link, $sql);

if(!$result)
{
    echo mysqli_error($link);
    exit();
}


$gresult = mysqli_fetch_array($result);

include 'invoiceupdate.php';
exit();

Which is then passed to invoiceupdate.php which is just a simple web form.

The results are posted to invoiceadd.php which contains this code.

$sql = "insert into invoice set 

                first_name = '$fname',
                last_name = '$lname',
                contact_no = '$contact_no',
                address = '$ResAddress',
                company = '$Company',
                email = '$CompAddress'
                cost = '$cost'";

Everything works fine if I take out cost = '$cost' but if its in there I get "Error Saving Data. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'cost = '5'' at line 9"

  • 写回答

2条回答 默认 最新

  • doukang8949 2015-05-08 20:08
    关注

    you're missing a comma, right there ...

    enter image description here

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

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊