将表单数据插入MySQL数据库表 [英] Insert form data into MySQL database table

查看:134
本文介绍了将表单数据插入MySQL数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将表单中的数据插入表格,但每次我点击提交没有发生任何事情,我检查和重新检查一切,但我找不到原因。



indextest1.php

 <!DOCTYPE html> 

< html>
< head>
< title>< / title>
< meta charset =UTF-8>
< meta name =authorcontent =>
< meta name =descriptioncontent =>
< meta name =keywordscontent =>
< / head>
< body>
< form action =test1.phpmethod =post>
< input type =textname =name>< br>
< input type =textname =lastname>< br>
< input type =radioname =radiovalue =onechecked>一个< br>
< input type =radioname =radiovalue =two>两个< br>
< select name =drop>
< option value =0selected> 0< / option>
< option value =1> 1< / option>
< option value =2> 2< / option>
< option value =3> 3< / option>
< option value =4> 4< / option>
< option value =5> 5< / option>
< option value =6> 6< / option>
< option value =7> 7< / option>
< option value =8> 8< / option>
< / select>
< input type =checkboxname =checkvalue =1>< br>
< input type =submitvalue =Submit>
< / form>
< / body>
< / html>

test1.php

 <?php 

$ servername =localhost;
$ username =root;
$ password =root;
$ dbname =db-test;

$ connect =($ servername,$ username,$ password,$ dbname)或die(ERROR DURING CONNECTION);

$ name = $ _POST [name];
$ lastname = $ _POST [lastname];
$ radio = $ _POST [radio];
$ drop = $ _POST [drop];
$ check = $ _POST [check];

$ sql_insert = mysql_query(INSERT INTO test-table(id,name,lastname,radio,drop,check)VALUES('','$ name','$ lastname','$ radio ','$ drop','$ check'));
header(location:indextest1.php);

?>

表值






查看MySQL如何告诉您语法错误何处开始?


您的SQL语法有错误;请检查与您的MySQL服务器版本对应的手册,以获取正确的语法使用near'drop,check)



您的SQL语法有错误,检查对应于您的MySQL服务器版本的手册,在正确的语法中使用near'check)


MySQL API与

  mysql_query(INSERT ... 

这些 mysql _ 函数不会与您的现有方法混合使用从连接到查询

  mysqli_query($ connect,INSERT ... 

$ b $使用预准备的语句:




  • a href =https://en.wikipedia.org/wiki/Prepared_statement =nofollow> https://en.wikipedia.org/wiki/Prepared_statement



您还应该根据POST数组检查空字段,例如!empty()



Sidenote:如果要插入撇号或任何其他字符,MySQL将抱怨,那么你必须逃避它们。无论哪种方式,你都应该。



另外,记住添加 exit;

  header(location:indextest1.php ); 
exit;


I want to insert data from a form into a table but everytime i hit submit nothing happens, i checked and re-checked everything but i can't find the reason.

indextest1.php

<!DOCTYPE html>

<html>    
    <head>  
        <title></title>    
        <meta charset="UTF-8">    
        <meta name="author" content="">    
        <meta name="description" content="">    
        <meta name="keywords" content="">   
    </head>    
    <body>
        <form action="test1.php" method="post">
        <input type="text" name="name"><br>
        <input type="text" name="lastname"><br>
        <input type="radio" name="radio" value="one" checked> one<br>   
        <input type="radio" name="radio" value="two" > two<br>
        <select name="drop">   
            <option value="0" selected>0</option>    
            <option value="1">1</option>    
            <option value="2">2</option>    
            <option value="3">3</option>    
            <option value="4">4</option>    
            <option value="5">5</option>   
            <option value="6">6</option>    
            <option value="7">7</option>    
            <option value="8">8</option>    
        </select>
        <input type="checkbox" name="check" value="1"><br>    
        <input type="submit" value="Submit">   
        </form>    
    </body>   
</html>

test1.php

<?php

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "db-test";

$connect = ("$servername, $username, $password, $dbname") or die ("ERROR DURING CONNECTION");

$name = $_POST["name"];
$lastname = $_POST["lastname"];
$radio = $_POST["radio"];
$drop = $_POST["drop"];
$check = $_POST["check"];

$sql_insert = mysql_query("INSERT INTO test-table (id, name, lastname, radio, drop, check) VALUES ('', '$name', '$lastname', '$radio', '$drop', '$check')");
        header("location: indextest1.php");

?>

Table values

Thanks in advance, i hope i provided all the info needed.

解决方案

Besides the other answer,

Firstly, this part of your query:

INSERT INTO test-table (id, name, lastname, radio, drop, check)

should read as

INSERT INTO `test-table` (id, name, lastname, radio, `drop`, `check`)

MySQL is interpreting your table as "test minus table". Use ticks, or rename it using an underscore test_table which is a safe seperator.

Then you're using two MySQL reserved words, "drop" and "check". Use ticks for those also.

Reference:

See how MySQL is telling you where the syntax error begins?

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'drop, check)

and it would have shown you another one after that being

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'check)

Just to clarify, you are mixing MySQL APIs with

mysql_query("INSERT ...

which those mysql_ functions do not intermix with your present method. Use the same from connection to query

mysqli_query($connect, "INSERT ...

and of course the fact that you're open to SQL injection. Use a prepared statement:

You should also check for empty fields against your POST arrays such as !empty().

Sidenote: If an apostrophe or any other character is to be inserted that MySQL will complain about, then you must escape them. Either way, you should.

Plus, remember to add exit; after header. Otherwise and if you have more code below that, it may want to continue and execute.

header("location: indextest1.php");
exit;

这篇关于将表单数据插入MySQL数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆