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.

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

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)