为什么我无法将数据传递给数据库

时间:2016-01-22 19:22:08

标签: php mysql html5

我一直在打破这个问题。我认为这应该是一个无法找到的简单错误,我正在寻找新的眼睛来帮助解决。

我想通过PHP将表单内容(“myForm”)更新为mysql数据库(“dbcerberus”),出于某种原因,我不想这样做。

这是我的HTML

    <div id="form">
        <select></select>
        <form id="postForm" method="POST" action="userForm.php">
            <p>Nome:</p>
            <input id="_nome">
            <p>CPF:</p>
            <input id="_cpf">
            <p>Telefone:</p>
            <input id="_tel">
            <p>Endereço:</p>
            <input id="_adress">
            <button id="submit">Save</button>
       </form>
       <button id="openVideoButton">LOAD</button>
   </div>

这是mais postForm.php和connect.php

connect.php
<?php

// Create connection
$conn = mysql_connect('localhost', 'root', '');
$db = mysql_select_db(dbcerberus);

?>






postForm.php
<?php

    include_once('connect.php');

    $name = $_POST['_nome'];
    $cpf = $_POST['_cpf'];
    $tel = $_POST['_tel'];
    $address = $_POST['_adress'];

    if(mysql_query("INSERT INTO tbvisitante VALUES('$cpf', '$name', '$address', '$tel')"))
        echo "Succes"
    else
        echo "Error"

?>

提前致谢。 P。:抱歉我的英语不好。

2 个答案:

答案 0 :(得分:3)

更改此内容:

<div id="form">
    <select></select>
    <form id="postForm" method="POST" action="userForm.php">
        <p>Nome:</p>
        <input id="_nome">
        <p>CPF:</p>
        <input id="_cpf">
        <p>Telefone:</p>
        <input id="_tel">
        <p>Endereço:</p>
        <input id="_adress">
        <button id="submit">Save</button>
   </form>
   <button id="openVideoButton">LOAD</button>
</div>

对此:

<div id="form">
    <select></select>
    <form id="postForm" method="POST" action="userForm.php">
        <p>Nome:</p>
        <input name="_nome">
        <p>CPF:</p>
        <input id="_cpf" name="_cpf">
        <p>Telefone:</p>
        <input name="_tel">
        <p>Endereço:</p>
        <input name="_adress">
        <button id="submit">Save</button>
   </form>
   <button id="openVideoButton">LOAD</button>
</div>

注意:未使用id。它没有伤害,但它没有在表格中使用。另一方面,name成为变量名称,字段内容是变量的值。

另外,将mysql_query语句更正为:

mysql_query("INSERT INTO tbvisitante (`cpf`,`name`,`address`,`tel`) VALUES('$cpf', '$nome', '$adress', '$tel') ")

注意:变量$ names的拼写也必须与HTML元素上的name=属性完全相同。

注意2:我假设我已经正确猜到了MySQL数据库的字段名称。如有必要,请更正。

强制性免责声明:您还应该使用mysqli_PDO,而不是弃用的mysql_命令。

答案 1 :(得分:1)

表单控件的name属性确定在将其发送到服务器时如何标记其值。

没有name的表单控件不会成功,也不会被发送。

您只提供用于客户端方面互动的id个属性(例如与for <label>属性相结合(你should be using))。

相关问题