dougan1465 2013-08-30 01:35
浏览 81
已采纳

将用户的输入存储到数据库中

I am fairly new to PHP and I am trying to save input from a user into a mysql database. I followed a tutorial online on how to do it, but every time I enter the user's info, the website tells me it failed. The only thing that I can think of is the host name(I copied and pasted it from phpadmin).Please let me know if there is something wrong.

contact.html

<section id="mid_section">
                <div id="boxes">
                    <h1>
                        Leave your information here for a quick reponse:
                    </h1>
                    <br/>
                    <form id="myform" action="userinfo.php" method="post">
                        Name:<input type="text" value="name">
                        Email:<input type="email" value="email">
                        Phone:<input type= "tel" value="phone(opt)">
                        <button id="sub">Submit</button>
                    </form>

db.php

<?php
    $conn = mysql_connect('custsql.eigbox.net','username','password');
    $db= mysql_select_db('visitors');
?>

userinfo.php

<?php
    include_once('db.php');

    $name =$_POST['name'];
    $email =$_POST['email'];
    $phone =$_POST['phone'];

    if(mysql_query("INSERT INTO users (name,email,phone) VALUES ('$name','$email','$phone')"))
    echo"successfully inserted";
    else
    echo "failed";
?>

myscript.js

$("#sub").click(function(){

    $.post($("#myform").attr("action"), $("#myform:input").serializeArray(), function(info){$("#result").html(info);});
    });

$("#myform").submit(function(){
    return false;
    });
  • 写回答

2条回答 默认 最新

  • ds2128629 2013-08-30 01:47
    关注

    As you might fairly be a newcomer to php, on one hand it is great to follow tutorials, however chosing a right source might be a frequent disasterous problem.

    When you are using functions like mysql_select_db and mysql_query it basiaclly means that you are using a deprecated mysql style.

    If you go to official php documentation and search for mysql method, it is going to tell you about its deprecation.

    Problem here, though, is not a way you interact with database, your style of coding still works and many people still do it just like that.

    I just tell you as a newcomer that instead of mysql_ functions, people tend to favor mysqli and or PDO. Consider them as your future friends.

    What about your problem, I believe all is okay, except your mysql_query functions looks odd. Try following code instead of your query statement

    if (mysql_query("INSERT INTO `users` (`name`, `email`, `phone`) VALUES ('".$name."','".$email."','".$phone."')"))
    

    or for security reasons even better

    if (mysql_query("INSERT INTO `users` (`name`, `email`, `phone`) VALUES ('".mysql_real_escape_string($name)."','".mysql_real_escape_string($email)."','".mysql_real_escape_string($phone)."')"))
    

    If it is not a case and you still get a 'Fail' error statement, you will need to do a very little debugging and people here will be able to help you out

    So, you will need to use following instead of what you have now

    if (mysql_query("INSERT INTO `users` (`name`, `email`, `phone`) VALUES ('".mysql_real_escape_string($name)."','".mysql_real_escape_string($email)."','".mysql_real_escape_string($phone)."')")) {
        echo 'Success!'
    } else {
        echo mysql_error();
        exit;
    }
    

    Let's see what happens

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

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行