problem uploading 3 values on mysql db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gioscarab
Forum Newbie
Posts: 2
Joined: Wed Feb 24, 2010 10:30 am

problem uploading 3 values on mysql db

Post by gioscarab »

Hi guys, i am entering now in the world of php, i am experimenting on coding only with arduino and c++..
now i am building my site and i have to create a database for users, i am starting to save only 3 values..
name surname and email.... this is my code:

Code: Select all

<?php
    if(isset($_GET['sent'])) {
        
        $rules = array(
            'email' => array('req', 'email' => true),
            'name' => array('req', 'minlength' => 3),
            'surname' => array('req', 'minlenght' => 2),
        );
        
        $errors = array();
        
        foreach($rules as $name => $rule) {
            if((! isset($_POST[$name])) || empty($_POST[$name])) {
                $errors[$name] = 'Obbligatorio';
                continue;
            }
            
            if(isset($rule['email']))
                if(preg_match('/^[a-z0-9][_\.a-z0-9-]+@([a-z0-9][0-9a-z-]+\.)+([a-z]{2,4})$/i', $_POST[$name]) == false) {
                    $errors[$name] = 'Email non valida';
                    continue;
                }
            
            if(isset($rule['minlength']))
                if(strlen($_POST[$name]) < $rule['minlength']) {
                    $errors[$name] = 'Minimo ' . $rule['minlength'] . ' caratteri';
                    continue;
                }
            
            if(isset($rule['length']))
                if(strlen($_POST[$name]) != $rule['length']) {
                    $errors[$name] = 'Inserisci ' . $rule['length'] . ' caratteri';
                    continue;
                }
            
            if(isset($rule['values']))
                if(! in_array($_POST[$name], $rule['values'])) {
                    $errors[$name] = 'Obbligatorio';
                    continue;
                }
        }
        
        if(count($errors) > 0)
            $showform = true;
        else {
            $showform = false;
            
            $dbConn = @mysqli_connect('ipserver', 'nomesql', 'pass', 'Sqlnomedb');
            if(! $dbConn)
                $error = 'Qualcosa non ha funzionato.<br />Per favore, riprova più tardi';
            else {              
            $query = "INSERT INTO concorso (email, name, surname) VALUES('$_POST[email]' , '$_POST[name]' , '$_POST[surname]')";
                
                mysqli_query($dbConn, $query);
            }
        }
    
    }
    else
        $showform = true;
        
    if($showform) {
?>
    <form action="?sent=true" method="post">
                <center><p style="color: white; font-size:15px; margin:7px 0px 5px 20px;"><b>Informazioni personali</b></p></center>
                <label for="email"><p style="color: white; font-size:12px; margin:0px 0px 0px 10px;"><b>Indirizzo email</b><?= isset($errors['email']) ? ' <span class="error">' . $errors['email'] . '</span>' : ''; ?></label><br />
                <input type="text" name="email" id="email" value="<?=$_POST['email'];?>" style="margin:0px 0px 7px 20px;" />
                <label for="name"><p style="color: white; font-size:12px;  margin:0px 0px 0px 10px;"><b>Nome</b><?= isset($errors['name']) ? ' <span class="error">' . $errors['name'] . '</span>' : ''; ?></label><br />
                <input type="text" name="name" id="name" value="<?=$_POST['name'];?>" style=" margin:0px 0px 7px 20px;" />
                <label for="surname"><p style="color: white; font-size:12px;  margin:0px 0px 0px 10px;"><b>Cognome</b><?= isset($errors['surname']) ? ' <span class="error">' . $errors['surname'] . '</span>' : ''; ?></label><br />
                <input type="text" name="surname" id="surname" value="<?=$_POST['surname'];?>" style=" margin:0px 0px 7px 20px;" /></center>
              <!--<center><a href="domanda1.php"><p style="font-size:15px;"><b>Continua!</b></p></a></center>-->
              <input type="image" src="img/inviabtn.png" />
<?
    } elseif(isset($error)) { ?>
    <center><?= $error; ?></center>
<?
    } else { ?>
    <br /><br /><br /><br /><h1><center>Grazie di aver partecipato!<br />Ti invieremo una mail se sarai selezionato tra i vincitori.<center></h1>
<?
    } ?>
the result after compiling correctly the 3 forms is a empty page with this link: data.php?sent=true
nothing saved on db....
Please can you help me?? In italy nobody can give me an advise..
User avatar
Salaria
Forum Commoner
Posts: 34
Joined: Fri Feb 13, 2009 2:50 am
Location: India
Contact:

Re: problem uploading 3 values on mysql db

Post by Salaria »

Hi,

Try to replace your query string with:

Code: Select all

$query = "INSERT INTO concorso (email, name, surname) VALUES('" . $_POST['email'] ."' , '". $_POST['name'] ."' , '" . $_POST[surname] ."')";
Hope it will work for you.
gioscarab
Forum Newbie
Posts: 2
Joined: Wed Feb 24, 2010 10:30 am

Re: problem uploading 3 values on mysql db

Post by gioscarab »

same empty page...
I can't understand why...

Do anyone know a simple tutorial or CMS that could do the work that i need..
like phpns or that things..

i only need to save 3 values entered by user via form
User avatar
Salaria
Forum Commoner
Posts: 34
Joined: Fri Feb 13, 2009 2:50 am
Location: India
Contact:

Re: problem uploading 3 values on mysql db

Post by Salaria »

Hi, Better you add some debugging echo or print statements to check the execution.

check if echo $query; is working or not and where is the problem.
Post Reply