PHP-将表单信息添加到数据库上的现有记录中
你好,Stackflow社区!我已经写了3页了,但仍然没有弄明白。所以我有一个显示用户名的第一页(create或check存在),直到一切正常PHP-将表单信息添加到数据库上的现有记录中,php,mysql,checkbox,save,pull,Php,Mysql,Checkbox,Save,Pull,你好,Stackflow社区!我已经写了3页了,但仍然没有弄明白。所以我有一个显示用户名的第一页(create或check存在),直到一切正常 <form name="form1" action= "test3.php" method="post"> Please enter your Username: <input type = "Text" value ="" name = "username"><br /> <input name="cmd_su
<form name="form1" action= "test3.php" method="post">
Please enter your Username: <input type = "Text" value ="" name = "username"><br />
<input name="cmd_submit" type="submit" id="submit" value="Submit" />
</form>
请输入您的用户名:
在此之后,正如“action”所说,我有了下一个php,它检查记录是否存在,并提取一个表单
<?php
if(isset($_POST['mode'])) {
$mode = implode(",", $_POST['mode']);
} else {
$mode = "";
}
?>
<?php
$dbhost = 'localhost';
$dbname = 'dbname';
$dbuser = 'dbuser';
$dbpass = 'dbpass';
$con = mysql_connect($dbhost, $dbuser, $dbpass);
if($con == FALSE)
{
echo 'Cannot connect to database' . mysql_error();
}
else
{
echo 'SUCCESS !';
}
mysql_select_db($dbname, $con);
// Get values from form
$username = mysql_escape_string($_POST['username']);
if(mysql_num_rows(mysql_query("SELECT * FROM email WHERE username = '".mysql_real_escape_string($_POST['username'])."'")))
{
echo '<form name="form1" action= "test41.php" method="post">
<table width="100%" >
Please select the category that you want to receive on your moval.edu e-mail account.<br /><br />
<b>Athletics:</b><br><br />
<b>MENS</b><br />
<input type="checkbox" name="mode[]" id="mode" value="Football">Football<br>
<input type="checkbox" name="mode[]" id="mode" value="Baseball">Baseball<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Basketball">Basketball<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Cheer">Cheer<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Cross Country">Cross Country<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Golf">Golf<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Lacrosse">Lacrosse<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Rodeo">Rodeo<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Soccer">Soccer<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Tennis">Tennis<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Track">Track<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Volleyball">Volleyball<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Wrestling">Wrestling<br><br />
<b>WOMENS</b><br />
<input type="checkbox" name="mode[]" id="mode" value="Womens Basketball">Basketball<br>
<input type="checkbox" name="mode[]" id="mode" value="Womens Cheer">Cheer<br>
<input type="checkbox" name="mode[]" id="mode" value="Womens Cross Country">Cross Country<br>
<input type="checkbox" name="mode[]" id="mode" value="Dance">Dance<br>
<input type="checkbox" name="mode[]" id="mode" value="Womens Golf">Golf<br>
<input type="checkbox" name="mode[]" id="mode" value="Womens Lacrosse">Lacrosse<br>
<input type="checkbox" name="mode[]" id="mode" value="Womens Rodeo">Rodeo<br>
<input type="checkbox" name="mode[]" id="mode" value="Womens Soccer">Soccer<br>
<input type="checkbox" name="mode[]" id="mode" value="Softball">Softball<br>
<input type="checkbox" name="mode[]" id="mode" value="Womens Tennis">Tennis<br>
<input type="checkbox" name="mode[]" id="mode" value="Womens Track">Track<br>
<input type="checkbox" name="mode[]" id="mode" value="Womens Volleyball">Volleyball<br>
<input type="checkbox" name="mode[]" id="mode" value="Womens Wrestling">Wrestling<br><br />
<b>STUDENT AFFAIRS ACIVITIES</b><br><br />
<input type="checkbox" name="mode[]" id="mode" value="Student Affairs"> Sign me up for Student Affairs Activities<br /><br />
<b>INTERNAIONAL CLUB ACTIVITIES</b><br /><br />
<input type="checkbox" name="mode[]" id="mode" value="International Club"> Sign me up for International Club Activities<br /><br />
<b>HISTORICAL ACTIVITIES</b><br><br />
<input type="checkbox" name="mode[]" id="mode" value="Historical Activities"> Sign me up for Hisorical Activities<br /><br />
<b>CONTESTS</b><br><br />
<input type="checkbox" name="mode[]" id="mode" value="Contests"> Sign me up for Contests<br /><br />
<b>LIBRARY EVENTS</b><br><br />
<input type="checkbox" name="mode[]" id="mode" value="Library Events"> Sign me up for Library Evens<br /><br />
<b>VIKING EXCHANGE</b><br /><br />
<input type="checkbox" name="mode[]" id="mode" value="Viking Exchange"> Sign me up for Viking Exchange News<br /><br />
</table>
<input name="cmd_submit" type="submit" id="submit" value="Submit" />
</form>';
}
else
{
$sql="INSERT INTO email(username, mode) VALUES('$username', '$mode')";
$result=mysql_query($sql);
echo '<form name="form1" action= "test41.php" method="post">
<table width="100%" >
Please select the category that you want to receive on your moval.edu e-mail account.<br /><br />
<b>Athletics:</b><br><br />
<b>MENS</b><br />
<input type="checkbox" name="mode[]" id="mode" value="Football">Football<br>
<input type="checkbox" name="mode[]" id="mode" value="Baseball">Baseball<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Basketball">Basketball<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Cheer">Cheer<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Cross Country">Cross Country<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Golf">Golf<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Lacrosse">Lacrosse<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Rodeo">Rodeo<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Soccer">Soccer<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Tennis">Tennis<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Track">Track<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Volleyball">Volleyball<br>
<input type="checkbox" name="mode[]" id="mode" value="Mens Wrestling">Wrestling<br><br />
<b>WOMENS</b><br />
<input type="checkbox" name="mode[]" id="mode" value="Womens Basketball">Basketball<br>
<input type="checkbox" name="mode[]" id="mode" value="Womens Cheer">Cheer<br>
<input type="checkbox" name="mode[]" id="mode" value="Womens Cross Country">Cross Country<br>
<input type="checkbox" name="mode[]" id="mode" value="Dance">Dance<br>
<input type="checkbox" name="mode[]" id="mode" value="Womens Golf">Golf<br>
<input type="checkbox" name="mode[]" id="mode" value="Womens Lacrosse">Lacrosse<br>
<input type="checkbox" name="mode[]" id="mode" value="Womens Rodeo">Rodeo<br>
<input type="checkbox" name="mode[]" id="mode" value="Womens Soccer">Soccer<br>
<input type="checkbox" name="mode[]" id="mode" value="Softball">Softball<br>
<input type="checkbox" name="mode[]" id="mode" value="Womens Tennis">Tennis<br>
<input type="checkbox" name="mode[]" id="mode" value="Womens Track">Track<br>
<input type="checkbox" name="mode[]" id="mode" value="Womens Volleyball">Volleyball<br>
<input type="checkbox" name="mode[]" id="mode" value="Womens Wrestling">Wrestling<br><br />
<b>STUDENT AFFAIRS ACIVITIES</b><br><br />
<input type="checkbox" name="mode[]" id="mode" value="Student Affairs"> Sign me up for Student Affairs Activities<br /><br />
<b>INTERNAIONAL CLUB ACTIVITIES</b><br /><br />
<input type="checkbox" name="mode[]" id="mode" value="International Club"> Sign me up for International Club Activities<br /><br />
<b>HISTORICAL ACTIVITIES</b><br><br />
<input type="checkbox" name="mode[]" id="mode" value="Historical Activities"> Sign me up for Hisorical Activities<br /><br />
<b>CONTESTS</b><br><br />
<input type="checkbox" name="mode[]" id="mode" value="Contests"> Sign me up for Contests<br /><br />
<b>LIBRARY EVENTS</b><br><br />
<input type="checkbox" name="mode[]" id="mode" value="Library Events"> Sign me up for Library Evens<br /><br />
<b>VIKING EXCHANGE</b><br /><br />
<input type="checkbox" name="mode[]" id="mode" value="Viking Exchange"> Sign me up for Viking Exchange News<br /><br />
</table>
<input name="cmd_submit" type="submit" id="submit" value="Submit" />
</form>';
}
//if (!mysql_query($sql, $con))
//{
// die('Error: ' . mysql_error());
//}
echo "You username has been added.";
mysql_close($con)
?>
使用此代码,它可以向您显示错误消息,如果您所在的领域已经使用电子邮件,那么它也会以不同的方式向您显示错误消息
<?php
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Input Validations
if($email == '') {
$errmsg_arr[] = 'Email Missing';
$errflag = true;
}
//Check for Duplicate Email
if($email != '') {
$qry = "SELECT * FROM email WHERE email='$email'";
$result = mysql_query($qry);
if($result) {
if(mysql_num_rows($result) > 0) {
$errmsg_arr[] = 'Email has Already Taken!';
$errflag = true;
}
@mysql_free_result($result);
}
else {
die("Query failed");
}
}
//If there are input validations, redirect back to the form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: form.php");
exit();
}
//Create INSERT Query
$sql="INSERT INTO email(mode) VALUES('$mode')";
$result= @mysql_query($sql);
//Check whether the query was successful or not
if($result) {
header("location: success.php");
exit();
}else {
die("Query failed");
}
?>
可能您需要一个插入。。。在重复键上
construct,但最好使用专用的“创建数据”和“编辑数据”脚本。@MarcB我读过一些关于创建数组的内容,但。。。你能解释清楚吗?我对PHP有点陌生。这不是我想要的。我不需要任何会话,我只想将复选框中的信息添加到db上的现有记录中,然后如果他们想修改他们的选项,则提取相同的信息。
<?php
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Input Validations
if($email == '') {
$errmsg_arr[] = 'Email Missing';
$errflag = true;
}
//Check for Duplicate Email
if($email != '') {
$qry = "SELECT * FROM email WHERE email='$email'";
$result = mysql_query($qry);
if($result) {
if(mysql_num_rows($result) > 0) {
$errmsg_arr[] = 'Email has Already Taken!';
$errflag = true;
}
@mysql_free_result($result);
}
else {
die("Query failed");
}
}
//If there are input validations, redirect back to the form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: form.php");
exit();
}
//Create INSERT Query
$sql="INSERT INTO email(mode) VALUES('$mode')";
$result= @mysql_query($sql);
//Check whether the query was successful or not
if($result) {
header("location: success.php");
exit();
}else {
die("Query failed");
}
?>
<?php
if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
echo '<ul class="err">';
foreach($_SESSION['ERRMSG_ARR'] as $msg) {
echo '<li>',$msg,'</li>';
}
echo '</ul>';
unset($_SESSION['ERRMSG_ARR']);
}
?>
<?php session_start(); ?>