dousi1875 2015-05-14 20:19
浏览 49
已采纳

我怎么能在PHP表单中添加一个字段

I'm looking for a sql statement where I can add additoinal text to a form field when being submitted to a mysql database. For example: A field contains a file name that has been loaded to a Web server called "mydoc.pdf". I want to prepend the following text "http://www.example.com/uploads/", so when the form is submitted the field data becomes "http://www.example.com/uploads/mydoc.pdf".

Right now the unappended field "tofiles_link" uses the following mysql query statement to insert to the mysql database:

mysql_query("INSERT INTO site_tofiles (tofiles_title, tofiles_body, 
tofiles_link, tofiles_relation, tofiles_type,  tofiles_post_ip, 
tofiles_post_user) VALUES 
('".mysql_real_escape_string($_POST['tofiles_title'])."', 
'".mysql_real_escape_string($_POST['tofiles_body'])."', 
'".mysql_real_escape_string($_POST['tofiles_link'])."', 
'".mysql_real_escape_string($_POST['tofiles_relation'])."', 
'".mysql_real_escape_string($_POST['tofiles_type'])."',  
'".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."', 
'".mysql_real_escape_string($_GET['tofiles_post_user'])."')");

echo "Data was Entered Successfully!
";
mysql_close($conn);

Can this be modified to suit my puroses? THX

  • 写回答

1条回答 默认 最新

  • doukan6564 2015-05-14 20:38
    关注

    To make the code more readable I would do your escaping before creating the SQL query. Not required but makes the code easier to read.

    $title = mysql_real_escape_string($_POST['tofiles_title']);
    $body = mysql_real_escape_string($_POST['tofiles_body']);
    $link = "http://www.example.com/uploads/" . mysql_real_escape_string($_POST['tofiles_link']);
    $relation = mysql_real_escape_string($_POST['tofiles_relation']);
    $type = mysql_real_escape_string($_POST['tofiles_type']);
    $ip = $_SERVER['REMOTE_ADDR'];
    $post_user = mysql_real_escape_string($_GET['tofiles_post_user']);
    

    Notice the $link variable on the 3rd line above adds the URL to the string.

    $sql = "INSERT INTO site_tofiles (tofiles_title, tofiles_body, tofiles_link, tofiles_relation, tofiles_type,  tofiles_post_ip, tofiles_post_user) VALUES ";
    $sql .= "('$title', '$body', '$link', '$relation', '$type', '$ip', '$post_user');";
    mysql_query($sql);
    
    echo "Data was Entered Successfully!
    ";
    mysql_close($conn);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制