Html / Php表单不会添加到SQL数据库中 [英] Html / Php form not adding to SQL database
问题描述
我现在一直在为此工作数小时,包括重建我的整个代码,我无法弄清楚什么是错的。 HTML表单应该填充患者SQL数据库,但它不起作用。我有一个类似的表单,我在网站的另一部分使用完美的工作,但这似乎并没有找出原因。
感谢您的帮助
以下是数据库字段的图片:
数据库字段
继承人代码:
<?php // session_start(); // if(isset()() $ _SESSION ['user_id'])){// header(Location:/); //} require'connection.php'; $ message =''; if(!empty($ _ POST ['name']) &&!empty($ _ POST ['dob'])&&!empty($ _ POST ['nok'])&&!empty($ _ POST ['nok_add'])&&!空($ _ POST ['nok_phone'])&&!empty($ _ POST ['doa'])&&!empty($ _ POST ['allergies']))://输入新用户数据库$ sql =INSERT INTO患者(名称,dob,nok,nok_add,nok_phone,doa,过敏)VALUES(:name,:dob,:nok,:no k_add,:nok_phone,:doa,:allergies); $ stmt = $ conn-> prepare($ sql); $ stmt-> bindParam(':name',$ _POST ['name']); $ stmt-> bindParam(':dob',$ _POST ['dob']); $ stmt-> bindParam(':nok',$ _POST ['nok']); $ stmt-> bindParam(':nok_add',$ _POST ['nok_add']); $ stmt-> bindParam(':nok_phone',$ _POST ['nok_phone']); $ stmt-> bindParam(':doa',$ _POST ['doa']); $ stmt-> bindParam(':allergies',$ _POST ['allergies']); if($ stmt-> execute()):$ message ='成功创建新用户';其他:$ message ='抱歉,创建您的账户一定是个问题'; ENDIF; ENDIF;?>< HTML> < HEAD> < title> Patient Add< / title> < meta name =viewportcontent =width = device-width,initial-scale = 1> < link rel =stylesheettype =text / csshref =assets / css / style.css> < link href =// fonts.googleapis.com/css?fami=Roboto:400,300,200,100&subset=latin,cyrillic =stylesheet>< style type =text / css> #headline {padding:0.8em;白颜色; font-family:Roboto,helvetica,arial,sans-serif;背景颜色:黑色; background-image:url(backgroundimage.jpg); background-size:cover; }< / style> < /头> <身体GT; < div id =headline> < div class =container> <报头GT; < h1> Patient Add< / h1> < p为H.;< / p为H. < /报头> < button onClick =window.location ='manager_select.php';类= _按钮 >返回< /按钮> < form method =POSTid =add> < h2>输入患者详情< / h2> < label for =name>名称:< / label> < input type =textname =name> < label for =dob>出生日期:< / label> < input type =textname =dob> < label for =nok>下一个Kin:< / label> < input type =textname =nok> < label for =nok_add>地址:< / label> < input type =textname =nok_add> < label for =nok_phone>电话号码:< / label> < input type =textname =nok_phone> < label for =doa>准入日期:< / label> < input type =textname =dob> < label for =allergies>过敏:< / label> < input type =textname =allergies> <?php if(!empty($ message)):?> < p><?= $ message?>< / p> <?php endif; ?> < input type =submit> < /形式> <峰; br> < / DIV> < / DIV> <! - // [START页脚] - > <页脚> < div class =container> < p>版权Ghyll Court Residental Home 2017< / p> < / DIV> < /页脚> <! - // [END页脚] - > < /脚本> < script type =text / javascript>函数init(){window.matchMedia((max-width:600px))。addListener(hitMQ); } function hitMQ(evt){sampleCompleted(GettingStarted-ContentWithStyles); } 在里面(); < /脚本> < / body>< / html>
正如您的重复帖子所回答的(也回答这里是为了完整性),如果语句不存在,则检查中的doa字段。您的表单定义了两次dob:一次为出生日期,一次为入院日期:
< label for = dob>出生日期:< / label>
< input type =textname =dob>
...
< label for =doa>准入日期:< / label>
< input type =textname =dob>
I've been working on this for hours now and including rebuilding my whole code I cannot figure out what is wrong. The HTML form is supposed to populate the patients SQL database but it is not working. I have a similar form that I am using on another part of the website that works flawlessly but this on doesn't seem to and a can't figure out why.
thanks for your help
Here is a picture of thedatabase fields : Database fields
Heres the code:
<?php
//session_start();
//if( isset($_SESSION['user_id']) ){
// header("Location: /");
//}
require 'connection.php';
$message = '';
if(!empty($_POST['name']) && !empty($_POST['dob']) && !empty($_POST['nok']) && !empty($_POST['nok_add']) && !empty($_POST['nok_phone'])&& !empty($_POST['doa'])&& !empty($_POST['allergies'])):
// Enter the new user in the database
$sql = "INSERT INTO patients (name, dob, nok, nok_add, nok_phone, doa, allergies) VALUES (:name, :dob, :nok, :nok_add, :nok_phone, :doa, :allergies)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':name', $_POST['name']);
$stmt->bindParam(':dob', $_POST['dob']);
$stmt->bindParam(':nok', $_POST['nok']);
$stmt->bindParam(':nok_add', $_POST['nok_add']);
$stmt->bindParam(':nok_phone', $_POST['nok_phone']);
$stmt->bindParam(':doa', $_POST['doa']);
$stmt->bindParam(':allergies', $_POST['allergies']);
if( $stmt->execute() ):
$message = 'Successfully created new user';
else:
$message = 'Sorry there must have been an issue creating your account';
endif;
endif;
?>
<html>
<head>
<title>Patient Add</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="assets/css/style.css">
<link href="//fonts.googleapis.com/css?fami=Roboto:400,300,200,100&subset=latin,cyrillic" rel="stylesheet">
<style type="text/css">
#headline {
padding: 0.8em;
color: white;
font-family: Roboto, helvetica, arial, sans-serif;
background-color: black;
background-image: url(backgroundimage.jpg);
background-size: cover;
}
</style>
</head>
<body>
<div id="headline">
<div class="container">
<header>
<h1>Patient Add</h1>
<p></p>
</header>
<button onClick="window.location='manager_select.php';" class="_button">Back</button>
<form method="POST" id="add">
<h2>Enter the Patient's Details</h2>
<label for="name">Name:</label>
<input type="text" name="name">
<label for="dob">Date of Birth:</label>
<input type="text" name="dob">
<label for="nok">Next of Kin:</label>
<input type="text" name="nok">
<label for="nok_add">Address:</label>
<input type="text" name="nok_add">
<label for="nok_phone">Phone:</label>
<input type="text" name="nok_phone">
<label for="doa">Date of Admission:</label>
<input type="text" name="dob">
<label for="allergies">Allergies:</label>
<input type="text" name="allergies">
<?php if(!empty($message)): ?>
<p><?= $message ?></p>
<?php endif; ?>
<input type="submit">
</form>
<br>
</div>
</div>
<!-- // [START footer] -->
<footer>
<div class="container">
<p>Copyright Ghyll Court Residental Home 2017</p>
</div>
</footer>
<!-- // [END footer] -->
</script>
<script type="text/javascript">
function init() {
window.matchMedia("(max-width: 600px)").addListener(hitMQ);
}
function hitMQ(evt) {
sampleCompleted("GettingStarted-ContentWithStyles");
}
init();
</script>
</body>
</html>
As answered on your duplicate post (also answering here for completeness), you're checking for a "doa" field in your if
statement, which doesn't exist. Your form defines "dob" twice: once for Date of Birth, and once for Date of Admission:
<label for="dob">Date of Birth:</label>
<input type="text" name="dob">
...
<label for="doa">Date of Admission:</label>
<input type="text" name="dob">
这篇关于Html / Php表单不会添加到SQL数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!