douzi2333 2011-06-09 23:00
浏览 35
已采纳

只有数字插入sql中

So i have this form

<form>Tag name:
    <input type='text' name='tagname' />
    <input type='submit' value='Add' />
    <input type='hidden' name='id' value='$id' />
</form>
<hr />

it runs this script

if ($tagname) 
{   
    mysql_query("INSERT INTO tags (id, tag) VALUES ($id, $tagname)");
    ?>
    <script type="text/javascript">
    alert("Tag added.");
    history.back();
    </script>
    <?php
}   

If i insert numbers in form it gets added to sql database nicely,but if it consist of alphabetical characters i get the alert but nothing is inserted in database. I checked phpmyadmin if the structure is wrong(text/varchar/int...) tried most of them but it is the same.

  • 写回答

5条回答 默认 最新

  • dongliang1873 2011-06-09 23:02
    关注

    You need single quotes to enclose strings within SQL queries:

     mysql_query("INSERT INTO tags (id, tag) VALUES ('$id', '$tagname')");
    

    And I'm conjecturing you also forgot to apply mysql_real_escape_string beforehand.

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

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程