Вопрос

Hello Stackflow community! I have already done 3 pages and still didn't figure it out. So I have a first page where it shows the username (create or check is exists), until there everything is fine.

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

After this, and as the 'action' says, I have the next php where it checks if the record exists or not and pulls a form.

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

Until here everything fine, now it comes my question: after pulling this form I want he infor to save it in the user that is connected to, so if it exists save it on that username, if doesn't (it should be created) create it and save the info in a way that next time shows the checkboxes that they 'checked' checked, and modify that info as they want, and every time pull the info the have 'checked'. And so forth.

So I have another code here for the action of the 2nd form (checkboxes).

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

Thanks, if I didn't explain myself at some point please be nice and let me know, I'm willing to edit or explain it in a comment.

Это было полезно?

Решение

Use this code it can show you error message and if in you field have already used email so it also showing you that error message in differently.

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

And use this code for error message

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

And don't for get Starting Session

<?php session_start(); ?>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top