doutui9606 2012-10-07 08:15
浏览 201
已采纳

Phpmyadmin默认值

My database have a profile table with image column. I want to set the default image using default value if the user are not inserting his image URL in the web form.

The column definition:

profile_image varchar(255) NOT NULL DEFAULT 'http://i.imgur.com/U.png'

I already inserted the default image URL in phpmyadmin but everytime I click submit at web form, every key in data at webform is stored in the profile table but the image column is empty.

I know I can do this with form by adding the image URL in web form but I want to do this via mysql.

This problem is solved because I insert the image at php insert but how do I use the null value to let mysql know I'm sending NULL? Is this the correct way of using Null?

$nl = NULL; 
$query = "INSERT INTO `student` (
`name` ,
`emel` ,
`address` ,
`profile_image` 
)
VALUES (
'$name', '$emel', '$add', '$nl');";

The above code won't work. The profile_image column is still empty.

  • 写回答

1条回答 默认 最新

  • duanhan9479 2012-10-07 08:24
    关注

    You need to do 2 things:

    1.) specify a default value of the column for the image, maybe something like default.jpg
    (I believe you've already done this)

    2.) ensure your php script provides a null value (not empty) or don't specify the value at all during insertion/modification, so your mysql knows it needs to use the default value. Or you can handle the logic in your php script to provide the url for the default image. I'd avoid some complicated functions in SQL, because your logic may change over time and SQL server really shouldn't be responsible for handling application logic.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试