gpt4 book ai didi

PHP-将表单信息添加到数据库中的现有记录中

转载 作者:行者123 更新时间:2023-11-30 01:13:50 26 4
gpt4 key购买 nike

Stackflow 社区您好!我已经写了3页了,还是没明白。所以我有一个第一页,它显示用户名(创建或检查是否存在),直到一切正常。

<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>

在此之后,正如“操作”所说,我有下一个 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
if(isset($_POST['mode'])) {
$mode = implode(",", $_POST['mode']);
} else {
$mode = "";
}
?>

<?php
$dbhost = 'localhost';
$dbname = 'website';
$dbuser = 'Test';
$dbpass = 'password1';

$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

$sql="INSERT INTO email(mode) VALUES('$mode')";
$result=mysql_query($sql);



if (!mysql_query($sql, $con))
{
die('Error: ' . mysql_error());
}
echo "You info has been submitted.";

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");
}
?>

并使用此代码作为错误消息

<?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']);
}
?>

并且不要开始 session

<?php session_start(); ?>

关于PHP-将表单信息添加到数据库中的现有记录中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19250797/

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