PHP获得HTML表单数据无法写入MySQL数据库
mysql 数据库 表单
PHP入门,在尝试编写一个图书数据库,数据库写入有一些问题总界面模块
<!DOCTYPE html><html> <head> <title> 图书录入数据处理 - 藏书 </title> <meta charset="utf-8"> <link rel="stylesheet" href="add.css"> </head> <body> <div id="header"> <h2 id="核准预览数据"> 核准预览数据 </h2> </div> <div id="container"> <?php /*载入预定义页面*/ include 'definevar.php'; /*全局变量*/ include 'isbn10to13.php'; /*ISBN-10与ISBN-13转换*/ include 'code2loc.php'; include 'code2lang.php'; include 'code2clssys.php'; include 'code2punit.php'; /*代码??信息转换函数组*/ echo 'ISBN-13 国际标准书号:' . $isbn . '<br />'; echo 'CIP 图书在版编目:' . $cip . '</p>' . '<p>正书名:' . $title . '<br />副书名:' . $subtitle . '<br />丛书名:' . $series . '</p>'; $creator1loc = code2loc($creator_loc_id1); echo '<p>作者1:[' . $creator1loc . ']' . $creator_1 . '<br />'; $creator2loc = code2loc($creator_loc_id2); echo '作者2:[' . $creator2loc . ']' . $creator_2 . '</p>'; $publoc = code2loc($publoc_id); echo '<p>由' . $publoc . '的' . $publisher . '出版<br />'; $language = code2lang($lang_id); echo '此' . $language . '发行版本为' . $edit_date . '第' . $edit_no . '版 ' . $version_date . '第' . $version_no . '次印刷</p>'; echo '<p>关键词组1:' . $subj_key_1 . '?' . $subj_key_2 . '?' . $subj_key_3 . '<br />'; $clssys1 = code2clssys($clssys_id_1); echo '关键词组2:' . $subj_key_4 . '?' . $subj_key_5 . '?' . $subj_key_6 . '<br />'; $clssys1 = code2clssys($clssys_id_1); echo $clssys1 . '分类号:' . $class_no_1 . '<br />'; $clssys2 = code2clssys($clssys_id_2); echo $clssys2 . '分类号:' . $class_no_2 . '</p>'; $priceunit = code2punit($punit_id); echo '<p>价格 ' . $price . $priceunit . ' · 藏本数量 ' . $amount . ' · 相关图书ISBN ' . $rlt_isbn . '</p>'; echo '<p>备注信息' . $noteinfo . '</p>'; echo '<h3 id="数据库写入信息">数据库写入信息</h3>'; include 'insert.php'; /*载入写入数据库模块*/ ?> <object type='image/svg+xml' data='bcgen/getbc.php?code=<?php echo $isbn; ?>'></object><br /> <ul> <li><a href="http://book.douban.com/isbn/<?php echo $isbn; ?>/">《<?php echo $title ?>》在豆瓣上的页面</a></li> <li><a href="http://www.amazon.cn/s/field-keywords=<?php echo $isbn; ?>">《<?php echo $title ?>》在亚马逊(中国网站)上的页面</a></li> </ul> </div> <div id="bottom" name="bottom"> <a href="insert.php"><button>确认添加</button></a> <input type="button" value="后退" onclick="javascript:history.back(1)" /> <p id="contact">Developed by SJHSTONE in 2014.</p> </div> </body></html>
数据库连接模块(dbconnect.php)
<?php $dbc = mysqli_connect('localhost', 'mylibr', 'v3rubaGbdfSSTz88', 'libr') or die('无法链接到MySQL数据库'); mysqli_set_charset($dbc,'utf8');
数据写入模块(insert.php)
<?php /*初始连接数据库,连接变量$dbc*/ include 'dbconnect.php'; /*开始写入请求*/ $query1 = "INSERT INTO libr_metacode (meta_isbn, meta_cip) VALUES ('$isbn', '$cip')"; $result = mysqli_query($dbc,$query1) or die('无法写入数据块1'); $query2 = "INSERT INTO libr_title (meta_isbn, title, subtitle, series) VALUES ('$isbn', '$title', '$subtitle', $'series')"; $result = mysqli_query($dbc,$query2) or die('无法写入数据块2'); $query3 = "INSERT INTO libr_author (meta_isbn, creator, creator_id, creatorloc_id) VALUES ('$isbn', '$creator_1', '$creator_loc_id1')"; $result = mysqli_query($dbc,$query3) or die('无法写入数据块3'); /*作者2是否为空*/ if (!empty($creator_2)) { $query4 = "INSERT INTO libr_author (meta_isbn, creator, creator_id, creatorloc_id) VALUES ('$isbn', '$creator_2', '$creator_loc_id2')"; $result = mysqli_query($dbc,$query4) or die('无法写入数据块4'); } else { echo '无写入的作者2信息,不写入数据块4'; } mysqli_close($dbc);
所有从$POST_[]全局变量引用的值都能在echo时正确显示,
能够正常连接并写入libr_metacode表中的13位ISBN、10位CIP数据,
但是无法写入接下来其他表中的数据。
先期涉及的三个表结构:
libr_metacode->meta_isbn char(13) 主键->meta_cip int(10)libr_title->meta_isbn char(13) 主键->title varchar(70)->subtitle varchar(50)->series varchar(30)libr_author (一对多)->rec_id int(5) 自动编号主键->creator varchar(30)->creatorloc_id int(3)
有兴趣的高手麻烦可以到 http://sjhstone.name/book/sandbox.rar看看源码提提建议,密码forcsdn(运行环境为本地Windows上的WampServer)
回复讨论(解决方案)
VALUES ('$isbn', '$cip')";
全都加转义
addslashes($isbn)
或者
mysql_real_escape_string($isbn)
把形如
or die('无法写入数据块1');
的改为形如
or die('无法写入数据块1' . mysqli_error($dbc));
的,就可知道问题在哪里了
VALUES ('$isbn', '$cip')";
全都加转义
addslashes($isbn)
或者
mysql_real_escape_string($isbn)
都是不需要转义的字符,而且只是在本地测试,暂时还用不着防注入……
不过谢啦!
把形如
or die('无法写入数据块1');
的改为形如
or die('无法写入数据块1' . mysqli_error($dbc));
的,就可知道问题在哪里了
这个纠错功能之前没看到过呢
顺带问一下哪里有参考性质的命令大全呢
一切都在 PHP手册 之中

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.
