duanhui7329 2014-05-06 02:13
浏览 25
已采纳

php - Insert不会输入要形成的值

Hi guys i dont know what im doing wrong but my tables are correct, php error is on and it doesnt insert

enter image description here

I can get both first name and email echoed

 <?php 
 if (isset($_POST['subs'])) {


 function html_escape($html_escape) {
    $html_escape =  htmlspecialchars($html_escape, ENT_QUOTES | ENT_HTML5, 'UTF-8');
    return $html_escape;
    }   


    $name=html_escape($_POST['name']); 
    $email=html_escape($_POST['email']);


   if (empty($name) || empty($email)) {echo"<div class='alert alert-danger'>Please enter both name and email address</div>";}

   else {

    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
   echo"<div class='alert alert-danger'>Invalid email address, please enter a correct email address!</div>";
    }

        else {

 echo "INSERT into subs (first_name, email) VALUES ('$name','$email')";
 $insert=mysql_query("INSERT into subs (first_name, email) VALUES ('$name','$email')");
  if ($insert) {echo"<div class='alert alert-success'>Thank you for subscribing with us</div>";}
     }

 }}
 ?>

展开全部

  • 写回答

3条回答 默认 最新

  • dongzz4545 2014-05-06 02:35
    关注

    first of all, are you connected to mysql before running your query?

    $conn=mysql_connect('localhost', 'your_db_username', 'your_db_password');
    if(!$conn){
       die('Cannot connect to mysql');
    }
    mysql_select_db('your_db_name');
    

    Then, when you're sure you're connected to the db and your query is still not working, add or die(mysql_error()) after your query like this, this will help you know what's going wrong with your insert:

    $insert=mysql_query("INSERT into subs (first_name, email) 
            VALUES ('$name','$email')")
            or die(mysql_error());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部