search
HomeBackend DevelopmentPHP TutorialPHP获得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 . ' &middot; 藏本数量 ' . $amount . ' &middot; 相关图书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手册 之中

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software