gpt4 book ai didi

php - 我的基于 php + mysql 的表单需要一些帮助

转载 作者:行者123 更新时间:2023-11-29 14:45:50 24 4
gpt4 key购买 nike

现在已经在这个基于 PHP 的表单上工作了一段时间了,基本上它是一个注册表单,我想将表单中的详细信息放入 MySQL 数据库(我已经构建了)。问题是似乎没有值被插入到数据库的各个行中,我想知道是否有比我更有经验的人可以浏览它并让我知道出了什么问题?提前致谢!

我将在此处显示表单本身的代码、数据库连接的包含内容以及 MySQL 代码(出于显而易见的原因,我更改了 database.php include 的真实用户详细信息):

表单本身(将其放入“register.php”,其中显示“//在此处插入表单的 HTML”

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<fieldset>
<legend>Registration Form</legend>
<form method="post" action="<?php echo ($_SERVER['PHP_SELF']) ?>" enctype="application/x-www-form-urlencoded">

<fieldset class="userdetails">
<legend>User Credentials</legend>
<ul>
<li><label for="user_email">Email Address:</label><input name="user_email" maxlength="50" id="user_email" size="40" type="text"></li>
<li><label for="user_pass">Your Password:</label><input name="user_pass" maxlength="50" id="user_pass" size="30" type="password"></li>
<li><label for="user_pass_confirm">Password Again:</label><input name="user_pass_confirm" maxlength="50" id="user_pass_confirm" size="30" type="password"><br></li>
<li><label for="user_username">Your Username:</label><input name="user_username" maxlength="50" id="user_username" size="30" type="text"></li>
</ul>
</fieldset>

<fieldset>
<legend>Contact Details</legend>
<ul>
<li><label for="firstname">First Name:</label><input name="firstname" type="text" id="firstname" size="25" maxlength="200" /></li>
<li><label for="lastname">Last Name:</label><input name="lastname" type="text" id="lastname" size="25" maxlength="200" /></li>
<li><label for="gender">Gender:</label>
<select name="gender" id="gender">
<option value="male" selected="selected">Male</option>
<option value="female">Female</option>
<option value="unspecified">Unspecified</option>
</select></li>
<li><label for"dateOfBirth">Date of birth:</label>
<select name="birthdate_day" id="birthday_date">
<option selected="selected" value="">Day</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>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>

</select>
<select name="birthdate_month" id="birthdate_month">
<option selected="selected" value="">Month</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>

</select>
<input name="birthdate_year" maxlength="4" id="birthdate_year" size="4" type="text" value="Year">
</li>

<li>
<label for="phoneNumber">Home Phone:</label>
<input name="phoneNumber" maxlength="9" id="phoneNumber" size="9" type="text"><span class="informational">e.g: 033456789</span></li>

<li><label for="mobileNumber">Mobile Number:</label>
<input name="mobileNum" maxlength="11" id="mobileNum" size="11" type="text"><span class="informational">e.g: 0276123456</span></li>

<li><label for="addressline1">Street Address:</label>
<input name="addressLine1" maxlength="100" id="addressLine1" size="40" type="text"></li>
<li><label for="addressline2">Suburb:</label>
<input name="addressLine2" maxlength="100" id="addressLine2" size="40" type="text"></li>
<li><label for="addressline3">City:</label>
<input name="addressLine3" maxlength="100" id="addressLine2" size="40" type="text"></li>
<li><label for="stateprovince">State/Province:</label>
<select name="stateprovince" id="stateprovince">
<option selected="selected" value=""></option>
<option value="northland">Northland</option>
<option value="auckland">Auckland</option>
<option value="waikato">Waikato</option>
<option value="bayofplenty">Bay of Plenty</option>
<option value="eastcoast">East Coast</option>
<option value="hawkesbay">Hawkes Bay</option>
<option value="taranaki">Taranaki</option>
<option value="kingcountry">King Country</option>
<option value="wanganui">Wanganui</option>
<option value="manawatu">Manawatu</option>
<option value="wairarapa">Wairarapa</option>
<option value="wellington">Wellington</option>
<option value="nelsonbays">Nelson Bays</option>
<option value="marlborough">Marlborough</option>
<option value="buller">Buller</option>
<option value="westland">Westland</option>
<option value="northcanterbury">North Canterbury</option>
<option value="canterbury">Canterbury</option>
<option value="midcanterbury">Mid Canterbury</option>
<option value="southcanterbury">South Canterbury</option>
<option value="northotago">North Otago</option>
<option value="otago">Otago</option>
<option value="southland">Southland</option>
<option value="chathamisl">Chatham Islands</option>
</select></li>

<li><label for="postcode">Post Code:</label>
<input name="postcode" maxlength="4" id="postcode" size="4" type="text"><span class="informational"><a href="http://www.nzpost.co.nz/nzpost/control/business/postcode_finder#post_code_finder" target="_blank" title="Find your post code (only for New Zealand)">Find your post code</a></span></li>

<li><label for="closest_town">Closest Town:</label>
<select name="closest_town" id="closest_town">
<option selected="selected" value="0">Please choose...</option>
<option value="dargaville">Northland - Dargaville</option>
<option value="kaikohe">Northland - Kaikohe</option>
<option value="kaitaia">Northland - Kaitaia</option>
<option value="kawakawa">Northland - Kawakawa</option>
<option value="kerikeri">Northland - Kerikeri</option>
<option value="maungaturoto">Northland - Maungaturoto</option>
<option value="paihia">Northland - Paihia</option>
<option value="whangarei">Northland - Whangarei</option>
<option value="0"> </option>
<option value="aucklandcity">Auckland - Auckland City</option>
<option value="franklin">Auckland - Franklin</option>
<option value="greatbarrierisland">Auckland - Great Barrier Island</option>
<option value="helensville">Auckland - Helensville</option>
<option value="hibiscuscoast">Auckland - Hibiscus Coast</option>
<option value="manukaucity">Auckland - Manukau City</option>
<option value="northshore">Auckland - North Shore</option>
<option value="papakuracity">Auckland - Papakura City</option>
<option value="waihekeisl">Auckland - Waiheke Island</option>
<option value="waitakerecity">Auckland - Waitakere City</option>
<option value="warkworth">Auckland - Warkworth</option>
<option value="wellsford">Auckland - Wellsford</option>
<option value="0"> </option>
<option value="cambridge">Waikato - Cambridge</option>
<option value="coromandel">Waikato - Coromandel</option>
<option value="hamilton">Waikato - Hamilton</option>
<option value="huntly">Waikato - Huntly</option>
<option value="matamata">Waikato - Matamata</option>
<option value="morrinsville">Waikato - Morrinsville</option>
<option value="otorohanga">Waikato - Otorohanga</option>
<option value="paeroa">Waikato - Paeroa</option>
<option value="raglan">Waikato - Raglan</option>
<option value="taumarunui">Waikato - Taumarunui</option>
<option value="teawamutu">Waikato - Te Awamutu</option>
<option value="tekuiti">Waikato - Te Kuiti</option>
<option value="thames">Waikato - Thames</option>
<option value="tokoroa_putaruru">Waikato - Tokoroa/Putaruru</option>
<option value="waihi">Waikato - Waihi</option>
<option value="waihi_beach">Waikato - Waihi Beach</option>
<option value="whangamata">Waikato - Whangamata</option>
<option value="0"> </option>
<option value="katikati">Bay of Plenty - Katikati</option>
<option value="mt_maunganui">Bay of Plenty - Mt. Maunganui</option>
<option value="opotiki">Bay of Plenty - Opotiki</option>
<option value="rotorua">Bay of Plenty - Rotorua</option>
<option value="taupo">Bay of Plenty - Taupo</option>
<option value="tauranga">Bay of Plenty - Tauranga</option>
<option value="te_puke">Bay of Plenty - Te Puke</option>
<option value="turangi">Bay of Plenty - Turangi</option>
<option value="whakatane">Bay of Plenty - Whakatane</option>
<option value="0"> </option>
<option value="gisborne">Gisborne - Gisborne</option>
<option value="ruatoria">Gisborne - Ruatoria</option>
<option value="0"> </option>
<option value="dannevirke">Hawkes Bay - Dannevirke</option>
<option value="hastings">Hawkes Bay - Hastings</option>
<option value="napier">Hawkes Bay - Napier</option>
<option value="waipukurau">Hawkes Bay - Waipukurau</option>
<option value="wairoa">Hawkes Bay - Wairoa</option>
<option value="0"> </option>
<option value="hawera">Taranaki - Hawera</option>
<option value="mokau">Taranaki - Mokau</option>
<option value="new_plymouth">Taranaki - New Plymouth</option>
<option value="opunake">Taranaki - Opunake</option>
<option value="stratford">Taranaki - Stratford</option>
<option value="0"> </option>
<option value="ohakune">Wanganui - Ohakune</option>
<option value="taihape">Wanganui - Taihape</option>
<option value="waiouru">Wanganui - Waiouru</option>
<option value="wanganui">Wanganui - Wanganui</option>
<option value="0"> </option>
<option value="bulls">Manawatu - Bulls</option>
<option value="fielding">Manawatu - Feilding</option>
<option value="levin">Manawatu - Levin</option>
<option value="manawatu">Manawatu - Manawatu</option>
<option value="marton">Manawatu - Marton</option>
<option value="palmerston_north">Manawatu - Palmerston North</option>
<option value="0"> </option>
<option value="carterton">Wairarapa - Carterton</option>
<option value="featherston">Wairarapa - Featherston</option>
<option value="greytown">Wairarapa - Greytown</option>
<option value="martinborough">Wairarapa - Martinborough</option>
<option value="masterton">Wairarapa - Masterton</option>
<option value="paihiatua">Wairarapa - Pahiatua</option>
<option value="woodville">Wairarapa - Woodville</option>
<option value="0"> </option>
<option value="kapiti">Wellington - Kapiti</option>
<option value="lower_hutt">Wellington - Lower Hutt City</option>
<option value="porirua">Wellington - Porirua</option>
<option value="upper_hutt">Wellington - Upper Hutt City</option>
<option value="wellington">Wellington - Wellington City</option>
<option value="0"> </option>
<option value="golden_bay">Nelson Bays - Golden Bay</option>
<option value="motueka">Nelson Bays - Motueka</option>
<option value="murchison">Nelson Bays - Murchison</option>
<option value="nelson">Nelson Bays - Nelson</option>
<option value="picton">Nelson Bays - Picton</option>
<option value="0"> </option>
<option value="blenheim">Marlborough - Blenheim</option>
<option value="marlborough_sounds">Marlborough - Marlborough Sounds</option>
<option value="0"> </option>
<option value="greymouth">West Coast - Greymouth</option>
<option value="hokitika">West Coast - Hokitika</option>
<option value="westport">West Coast - Westport</option>
<option value="0"> </option>
<option value="akaroa">Canterbury - Akaroa</option>
<option value="amberley">Canterbury - Amberley</option>
<option value="ashburton">Canterbury - Ashburton</option>
<option value="cheviot">Canterbury - Cheviot</option>
<option value="christchurch">Canterbury - Christchurch City</option>
<option value="darfield">Canterbury - Darfield</option>
<option value="fairlie">Canterbury - Fairlie</option>
<option value="geraldine">Canterbury - Geraldine</option>
<option value="hamnersprings">Canterbury - Hanmer Springs</option>
<option value="kaiapoi">Canterbury - Kaiapoi</option>
<option value="kaikoura">Canterbury - Kaikoura</option>
<option value="mtcook">Canterbury - Mt Cook</option>
<option value="rangiora">Canterbury - Rangiora</option>
<option value="0"> </option>
<option value="timaru_oamaru_kurow">Timaru - Oamaru - Kurow</option>
<option value="timaru_oamaru_oamaru">Timaru - Oamaru - Oamaru</option>
<option value="timaru_oamaru_timaru">Timaru - Oamaru - Timaru</option>
<option value="timaru_oamaru_twizel">Timaru - Oamaru - Twizel</option>
<option value="timaru_oamaru_waimate">Timaru - Oamaru - Waimate</option>
<option value="0"> </option>
<option value="alexandra">Otago - Alexandra</option>
<option value="balclutha">Otago - Balclutha</option>
<option value="cromwell">Otago - Cromwell</option>
<option value="dunedin">Otago - Dunedin</option>
<option value="lawrence">Otago - Lawrence</option>
<option value="milton">Otago - Milton</option>
<option value="palmerston">Otago - Palmerston</option>
<option value="queenstown">Otago - Queenstown</option>
<option value="ranfurly">Otago - Ranfurly</option>
<option value="roxburgh">Otago - Roxburgh</option>
<option value="wanaka">Otago - Wanaka</option>
<option value="0"> </option>
<option value="bluff">Southland - Bluff</option>
<option value="edendale">Southland - Edendale</option>
<option value="gore">Southland - Gore</option>
<option value="invercargill">Southland - Invercargill</option>
<option value="lumsden">Southland - Lumsden</option>
<option value="otautau">Southland - Otautau</option>
<option value="riverton">Southland - Riverton</option>
<option value="stewart_island">Southland - Stewart Island</option>
<option value="te_anau">Southland - Te Anau</option>
<option value="tokanui">Southland - Tokanui</option>
<option value="winton">Southland - Winton</option>
<option value="0"> </option>
<option value="chathamisl">Chatham Islands</option>
</select></li>

<li><label for="u18conf">I am over 18...</label> <input id="u18conf" name="u18conf" type="checkbox"></li></ul>

<input name="SubmitButton" class="joinitrade" value="Join iTrade" id="SubmitButton" type="submit">
</form>
</fieldset>

register.php :表单的一些 php 处理

<?php
require('includes/database.php'); //Include DB connection information
?>

//INSERT HTML FOR FORM IN HERE

<?php

if (isset($_POST['SubmitButton'])) {

//Execute the following if form is submitted
$user_email = mysql_real_escape_string($_POST['user_email']); //Post user email
$user_pass = mysql_real_escape_string(sha1(md5($_POST['user_pass']))); //Post password from form and encrypt
$user_pass_confirm = mysql_real_escape_string(sha1(md5($_POST['user_pass_confirm']))); //Post password confirmation from form and encrypt
$user_username = mysql_real_escape_string($_POST['user_username']);
$firstname = mysql_real_escape_string($_POST['firstname']);
$lastname = mysql_real_escape_string($_POST['lastname']);
$gender = mysql_real_escape_string($_POST['gender']);
$birthday_date = mysql_real_escape_string($_POST['birthday_date']);
$birthdate_month = mysql_real_escape_string($_POST['birthdate_month']);
$birthdate_year = mysql_real_escape_string($_POST['birthdate_year']);
$phoneNumber = mysql_real_escape_string($_POST['phoneNumber']);
$mobileNumber = mysql_real_escape_string($_POST['mobileNumber']);
$addressline1 = mysql_real_escape_string($_POST['addressline1']);
$addressline2 = mysql_real_escape_string($_POST['addressline2']);
$addressline3 = mysql_real_escape_string($_POST['addressline3']);
$stateprovince = mysql_real_escape_string($_POST['stateprovince']);
$postcode = mysql_real_escape_string($_POST['postcode']);
$closest_town = mysql_real_escape_string($_POST['closest_town']);

if(empty($user_email) || empty($user_pass) || empty($user_pass_confirm) || empty($firstname) || empty($firstname) || empty($lastname) || empty($user_username) || empty($gender) || empty($birthday_date) || empty($birthdate_month) || empty($birthdate_year) || empty($phoneNumber) || empty($addressline1) || empty($addressline2) || empty($addressline3) || empty($stateprovince) || empty($postcode) || empty($closest_town)) { //Check for empty fields
die("<b>Error:</b> All fields are required in order to register an account.");
}

if(strlen($user_username) > 25) { //Check if username exceeds limit of 25 characters
die("<b>Error:</b> Your username may not exceed 25 characters.");
}

if(strlen($user_pass) < 8 || strlen($user_pass) > 15) { //Check if username exceeds limit of 25 characters
die("<b>Error:</b> Your password may not exceed 15 characters, or not be less that 8 characters.");
}

if (!preg_match("/[a-zA-Z0-9_-]/", $user_username)) { //Check for unallowed characters in username
die("<b>Error:</b> Your username may only contain letters (a-z), numbers (0-9), underscores (_) and hypens (-).");
}

if ($user_pass != $user_pass_confirm) { //Check if passwords match
die("<b>Error:</b> Your passwords do not match.");
}

$check = mysql_query("SELECT * FROM loginsystem2 WHERE user_username = '$user_username'") or die(mysql_error());
$check2 = mysql_num_rows($check);
if ($check2 != 0) { //Check if account already exists witih this username
die("<b>Error:</b> There is already an existing account with the username <b>$username</b>.");
}
mysql_query("INSERT INTO `loginsystem2` (`user_email`, `user_pass`, `user_pass_confirm`, `user_username`, `firstname`, `lastname`, `gender`, `birthday_date`, `birthdate_month`, `birthdate_year`, `phoneNumber`, `mobileNumber`, `addressline1`, `addressline2`, `addressline3`, `stateprovince`, `postcode`, `closest_town`)VALUES ('$user_email', '$user_pass', '$user_pass_confirm', '$user_username', '$firstname', '$lastname', '$gender', '$birthday_date', '$birthdate_month', '$birthdate_year', '$phoneNumber', '$mobileNumber', '$addressline1', '$addressline2', '$addressline3', '$stateprovince', '$postcode', '$closest_town')") OR die(mysql_error());

echo "Account created! You may now <a href=\"login.php\">login</a>.";
}
?>
</body>
</html>

database.php :连接数据库所需

<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("loginsystem2") or die(mysql_error());
?>

loginsystem2.sql:表单的数据库

DROP TABLE IF EXISTS users;

CREATE TABLE users(

user_id int(11) NOT NULL AUTO_INCREMENT,

user_email varchar(50) NOT NULL,

user_pass varchar(25) NOT NULL,

user_pass_confirm varchar(11) NOT NULL,

firstname varchar(25) NOT NULL,

lastname varchar(25) NOT NULL,

gender varchar(11) NOT NULL,

birthday_date int(2) NOT NULL,

birthday_month int(2) NOT NULL,

birthday_year int(4) NOT NULL,

phoneNumber int(9) NOT NULL,

mobileNumber int(11) NOT NULL,

addressline1 varchar(100) NOT NULL,

addressline2 varchar(100) NOT NULL,

addressline3 varchar(100) NOT NULL,

stateprovince varchar(40) NOT NULL,

postcode tinyint(4) NOT NULL,

closest_town varchar(40) NOT NULL,

user_level tinyint(4) DEFAULT '0',

user_hash varchar(32) DEFAULT NULL,

user_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

PRIMARY KEY (user_id)
);
<小时/>

感谢大家的帮助...所以我已经解决了所描述的问题,并且现在更新了代码,如下所示:因为将有问题的代码文件复制并粘贴回此处将使字符限制超过 30,000限制,我已将文件上传到 MediaFire.com here

无论如何,修复此代码后,我可以用一些数据填充表单并成功填充我的数据库(稍后将修复重复用户等问题)。这样做的缺陷是,如果我按下提交按钮而没有在任何字段中输入任何内容,我仍然会收到一条消息说“我的注册已成功”,并且我的数据库会填充默认值或根本没有任何内容。我在下面列出了这些详细信息:

  • 用户 ID:1
  • 用户电子邮件:空白
  • user_pass:使用 md5 和 sha 组合的一些哈希值加密
  • user_pass_confirm:同上
  • 用户用户名:空白
  • 名字:空白
  • 姓氏:空白
  • 性别:男
  • 生日:1 日
  • 出生日期_月份:一月
  • 出生日期:0
  • 电话号码:0
  • 手机号码:0
  • 地址行1:空白
  • 地址行1:空白
  • 地址行1:空白
  • 州省:空白
  • 邮政编码:0
  • 最近的城镇:0

我应该看到我在“registerprocessing.php”的 php 代码中指定的错误消息,而不是插入任何内容。我想要的是,如果用户尝试提交表单而不自己填写任何字段,错误警告将显示在“registerprocessing.php”页面上,然后用户可以返回表单并填写。也许我检查错误或检查非输入字段的方式有问题,但我已经尝试和测试了几个版本来尝试修复所有问题,但没有任何效果。任何帮助将不胜感激:)

最佳答案

问题是:您的 SQL 创建了一个名为“users”的表,但您正在向名为“loginsystem2”的表插入数据。

此外,尽量不要混淆数据库架构名称和表名称。

关于php - 我的基于 php + mysql 的表单需要一些帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6977140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com