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

时间:2013-10-08 14:40:41

标签: php mysql checkbox save pull

Hello 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)
?> 

谢谢,如果我在某些时候没有解释自己,请保持良好并让我知道,我愿意在评论中编辑或解释。

1 个答案:

答案 0 :(得分:0)

使用此代码可以显示错误消息,如果您在字段中已经使用过电子邮件,那么它也会以不同方式显示该错误消息。

    <?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(); ?>