Hi Guys,

Been talking to a friend about my code I have been working on.I could not figure out how for the life of me to check whether an email or code in my database is already in use. So just to cool down for a bit i put conditions in my code to check whether all fields has been filled out, or if they haven't. When the two integer columns have not been filled out (Telephone, Code) I get this error;

Error updating database because: Incorrect integer value: '' for column 'Code' at row 1

My friend says I have tried to jump to stage to 5 and not doing the other stages correctly. Can you have a look at the code and explain to me what i am doing wrong and where to improve?? Thanks guys

<?php
    if($_POST){ 
        mysql_connect ("localhost", "root", "root") or die ('Error: ' . mysql_error());
        mysql_select_db ("wowcher") or die ('Error: ' . mysql_error());
        $First_name = $_POST['First_Name'];
        $Surname = $_POST['Last_Name'];
        $Email = $_POST['Email'];
        $Code = $_POST['Code'];
        $Telephone = $_POST['Phone'];
        $errorMessage = "";
        $id = isset($Telephone) ? trim($Telephone) : " ";
        $query="INSERT INTO wowcher_code(First_name, Last_Name, Email, Code, Telephone)VALUES ('".$First_name."', '".$Surname."', '".$Email."', '".$Code."', '".$Telephone."')";
        mysql_query($query) or die ('Error updating database because: '.mysql_error());



              if(empty($_POST['First_Name'])) 
              {
                $errorMessage .= "<li>You forgot to enter your Name</li>";
              }
              if(empty($_POST['Last_Name'])) 
              {
                $errorMessage .= "<li>You forgot to enter your Surname</li>";
              }
              if(empty($_POST['Email'])) 
              {
                $errorMessage .= "<li>You forgot to enter your Email</li>";
              }
              if(empty($_POST['Code'])) 
              {
                $errorMessage .= "<li>You forgot to enter your Wowcher code you silly person!</li>";
              }
              if(empty($_POST['Phone'])) 
              {
                $errorMessage .= "<li>You forgot to enter your Phone Number</li>";
              }
              if(!empty($errorMessage)) 
              {
                echo("<p>There was an error with your form:</p>\n");
                echo("<ul>" . $errorMessage . "</ul>\n");
              }
              else{
                      if(!empty($_POST['First_Name'])) 
                      {

                      }
                      if(!empty($_POST['Last_Name'])) 
                      {

                      }
                      if(!empty($_POST['Email']) or ) 
                      {

                      }
                      if(!empty($_POST['Code'])) 
                      {

                      }
                      if(!empty($_POST['Phone'])) 
                      {

                      }
                      echo("<p>Congrations details successfully added!</p>\n");
                  }


        mysql_close();
    }



?>

<!DOCTYPE html>
<html>
<head>
<title>Wowcher Code Submission</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
    <div class="container">
        <form method ="post" action="index.php">
            <label>First name:</label><br>
            <input type="text" name="First_Name"><br>
            <label>Last name:</label><br>
            <input type="text" name="Last_Name"><br>
            <label>Email:</label><br>
            <input type="text" name="Email"><br>
            <label>Wowcher Code:</label><br>
            <input type="text" name="Code"><br>
            <label>Telephone Number:</label><br>
            <input type="text" name="Phone"><br>
            <input type="submit" value="Submit">
        </form>
    </div>



</body>
</html>

Recommended Answers

All 2 Replies

Try this, it processes everything before updating the database:

<?php
// Set your variables to empty if they haven't been submitted:
if (isset($_POST['First_Name'])) {
    $First_Name = $_POST['First_Name'];
} else {
    $First_Name = '';
}
if (isset($_POST['Last_Name'])) {
    $Last_Name = $_POST['Last_Name'];
} else {
    $Last_Name = '';
}

if (isset($_POST['Email'])) {
    $Email = $_POST['Email'];
} else {
    $Email = '';
}
if (isset($_POST['Code'])) {
    $Code = $_POST['Code'];
} else {
    $Code = '';
}
if (isset($_POST['Phone'])) {
    $Phone = $_POST['Phone'];
} else {
    $Phone = '';
}
// Create message variables:
$outputMessage = '';
$errorMessage = '';
// Check if submit clicked:
if (isset($_POST['submit'])) {
    // Check fields are filled in:
    if (empty($First_Name)) {
        $errorMessage .= "<li>You forgot to enter your First Name</li>";
    }
    if (empty($Last_Name)) {
        $errorMessage .= "<li>You forgot to enter your Last Name</li>";
    }
    if (empty($Email)) {
        $errorMessage .= "<li>You forgot to enter your Email</li>";
    }
    if (empty($Code)) {
        $errorMessage .= "<li>You forgot to enter your Wowcher code you silly person!</li>";
    }
    if (empty($Phone)) {
        $errorMessage .= "<li>ou forgot to enter your Phone Number</li>";
    }
    // Check for error
    if (empty($errorMessage)) {
        // No error, do your database thing:
        mysql_connect ("localhost", "root", "root") or die ('Error: ' . mysql_error());
        mysql_select_db ("wowcher") or die ('Error: ' . mysql_error());
        $query="INSERT INTO wowcher_code(First_name, Last_Name, Email, Code, Telephone)VALUES ('".$First_name."', '".$Last_Name."', '".$Email."', '".$Code."', '".$Phone."')";
        mysql_query($query) or die ('Error updating database because: '.mysql_error());
        mysql_close();
        // Empty the variables and set output message:
         $First_Name = '';
         $Last_Name = '';
         $Email = '';
         $Code = '';
         $Phone = '';
        $outputMessage .= "<p>Congrations details successfully added!</p>";
    } else {
        // Errors exist, inform browser:
        $errorMessage = "<p>There was an error with your form:</p><ul>$errorMessage</ul>";
        $outputMessage = $errorMessage;
    }
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Wowcher Code Submission</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<div class="container">
<?php echo $outputMessage; ?>
<form method ="post" action="">
<label>First name:</label><br>
<input type="text" name="First_Name" value="<?php echo $First_Name; ?>"><br>
<label>Last name:</label><br>
<input type="text" name="Last_Name" value="<?php echo $Last_Name; ?>"><br>
<label>Email:</label><br>
<input type="email" name="Email" value="<?php echo $Email; ?>"><br>
<label>Wowcher Code:</label><br>
<input type="text" name="Code" value="<?php echo $Code; ?>"><br>
<label>Telephone Number:</label><br>
<input type="text" name="Phone" value="<?php echo $Phone; ?>"><br>
<input type="submit" name="submit" value="Submit">
</form>
</div>
</body>
</html>

As a further measure you could also use 'required' in your inputs thus: <input type="email" name="Email" value="<?php echo $Email; ?>" required>

Hiya Thanks for reply, I got a little help in the end for what I was trying to achieve. Here is my code;

<?php
    if($_POST){ 
        mysql_connect ("localhost", "root", "root") or die ('Error: ' . mysql_error());
        mysql_select_db ("wowcher") or die ('Error: ' . mysql_error());
        $First_Name = $_POST['First_Name'];
        $Surname = $_POST['Last_Name'];
        $Email = $_POST['Email'];
        $Code = $_POST['Code'];
        $Telephone = $_POST['Phone'];
        $errorMessage = "";
        $query="SELECT Code FROM wowcher_code WHERE Code='$Code'";
        $query1 = mysql_query($query) or die ('Error updating database because: '.mysql_error());
        $num_rows = mysql_num_rows($query1);
        if($num_rows > 0)
        {


            $query2="SELECT in_use FROM wowcher_code WHERE Code ='$Code'";
            $query3 = mysql_query($query2) or die ('Error updating database because: '.mysql_error());
            $query4 = mysql_fetch_array($query3);
            $code_in_use = $query4['in_use'];

            if($code_in_use == 1){

                echo"Sorry code in use, Please re-register again!";
            }
            else{

                echo"Successfully registered!";
                $update ="UPDATE wowcher_code SET First_Name ='$First_Name' WHERE Code ='$Code'";
                $update1 = mysql_query($update) or die ('Error updating database because: '.mysql_error());
                $update2 ="UPDATE wowcher_code SET Last_Name ='$Surname' WHERE Code ='$Code'";
                $update3 = mysql_query($update2) or die ('Error updating database because: '.mysql_error());
                $update4 ="UPDATE wowcher_code SET Email ='$Email' WHERE Code ='$Code'";
                $update5 = mysql_query($update4) or die ('Error updating database because: '.mysql_error());
                $update6 ="UPDATE wowcher_code SET Telephone ='$Telephone' WHERE Code ='$Code'";
                $update7 = mysql_query($update6) or die ('Error updating database because: '.mysql_error());
                $update8 ="UPDATE wowcher_code SET in_use ='1' WHERE Code ='$Code'";
                $update9 = mysql_query($update8) or die ('Error updating database because: '.mysql_error());
            }
        }
              if(empty($_POST['First_Name'])) 
              {
                $errorMessage .= "<li>You forgot to enter your Name</li>";
              }
              if(empty($_POST['Last_Name'])) 
              {
                $errorMessage .= "<li>You forgot to enter your Surname</li>";
              }
              if(empty($_POST['Email'])) 
              {
                $errorMessage .= "<li>You forgot to enter your Email</li>";
              }
              if(empty($_POST['Code'])) 
              {
                $errorMessage .= "<li>You forgot to enter your Wowcher code you silly person!</li>";
              }
              if(empty($_POST['Phone'])) 
              {
                $errorMessage .= "<li>You forgot to enter your Phone Number</li>";
              }
              if(!empty($errorMessage)) 
              {
                echo("<p>There was an error with your form:</p>\n");
                echo("<ul>" . $errorMessage . "</ul>\n");
              }
        mysql_close();
    }



?>

<!DOCTYPE html>
<html>
<head>
<title>Wowcher Code Submission</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
    <div class="container">
        <form method ="post" action="lo.php">
            <label>First name:</label><br>
            <input type="text" name="First_Name"><br>
            <label>Last name:</label><br>
            <input type="text" name="Last_Name"><br>
            <label>Email:</label><br>
            <input type="text" name="Email"><br>
            <label>Wowcher Code:</label><br>
            <input type="text" name="Code"><br>
            <label>Telephone Number:</label><br>
            <input type="text" name="Phone"><br>
            <input type="submit" value="Submit">
        </form>
    </div>



</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.